Skip to content

Commit c271077

Browse files
committed
添加测试用例
1 parent 2266c48 commit c271077

File tree

12 files changed

+902
-0
lines changed

12 files changed

+902
-0
lines changed

com.qcloud.weapp.test/.classpath

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry combineaccessrules="false" kind="src" path="/com.qcloud.weapp.sdk"/>
6+
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v7.0"/>
7+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
8+
<classpathentry kind="lib" path="lib/mockito-all-1.10.19.jar"/>
9+
<classpathentry kind="lib" path="lib/org.json.jar"/>
10+
<classpathentry kind="output" path="bin"/>
11+
</classpath>

com.qcloud.weapp.test/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

com.qcloud.weapp.test/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>com.qcloud.weapp.test</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8
1.18 MB
Binary file not shown.
47.7 KB
Binary file not shown.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.qcloud.weapp.test;
2+
3+
import static org.mockito.Mockito.*;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.IOException;
7+
import java.io.PrintWriter;
8+
import java.io.StringWriter;
9+
10+
import javax.servlet.ServletInputStream;
11+
import javax.servlet.http.HttpServletRequest;
12+
import javax.servlet.http.HttpServletResponse;
13+
14+
import org.mockito.Matchers;
15+
import org.mockito.invocation.InvocationOnMock;
16+
import org.mockito.stubbing.Answer;
17+
18+
public class HttpMock {
19+
public HttpServletRequest request;
20+
public HttpServletResponse response;
21+
22+
private StringWriter sw;
23+
private PrintWriter pw;
24+
public void setupResponseWriter() {
25+
try {
26+
sw = new StringWriter();
27+
pw = new PrintWriter(sw);
28+
when(response.getWriter()).thenReturn(pw);
29+
} catch (IOException e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
34+
private String responseText = null;
35+
public String getResponseText() {
36+
if (responseText == null) {
37+
pw.flush();
38+
responseText = sw.toString();
39+
}
40+
return responseText;
41+
}
42+
43+
public void setRequestBody(String requestBody) {
44+
try {
45+
// @see http://blog.timmattison.com/archives/2014/12/16/mockito-and-servletinputstreams/
46+
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(requestBody.getBytes("utf-8"));
47+
ServletInputStream mockServletInputStream = mock(ServletInputStream.class);
48+
when(mockServletInputStream.read(Matchers.<byte[]>any(), anyInt(), anyInt())).thenAnswer(new Answer<Integer>() {
49+
@Override
50+
public Integer answer(InvocationOnMock invocationOnMock) throws Throwable {
51+
Object[] args = invocationOnMock.getArguments();
52+
byte[] output = (byte[]) args[0];
53+
int offset = (int) args[1];
54+
int length = (int) args[2];
55+
return byteArrayInputStream.read(output, offset, length);
56+
}
57+
});
58+
when(request.getInputStream()).thenReturn(mockServletInputStream);
59+
} catch (IOException e) {
60+
e.printStackTrace();
61+
}
62+
}
63+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.qcloud.weapp.test;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.IOException;
6+
import java.io.UnsupportedEncodingException;
7+
import java.net.HttpURLConnection;
8+
import java.net.Proxy;
9+
10+
import static org.mockito.Mockito.*;
11+
import com.qcloud.weapp.*;
12+
13+
public class URLConnectionMock implements HttpRequest.ConnectionProvider {
14+
15+
private ByteArrayInputStream responseStream;
16+
private ByteArrayOutputStream requestStream;
17+
18+
public URLConnectionMock() {
19+
requestStream = new ByteArrayOutputStream();
20+
}
21+
22+
public void setResponseBody(String body) {
23+
try {
24+
responseStream = new ByteArrayInputStream(body.getBytes("utf-8"));
25+
} catch (UnsupportedEncodingException e) {
26+
e.printStackTrace();
27+
}
28+
}
29+
30+
public String getRequestBody() {
31+
try {
32+
return new String(requestStream.toByteArray(), "utf-8");
33+
} catch (UnsupportedEncodingException e) {
34+
e.printStackTrace();
35+
return null;
36+
}
37+
}
38+
39+
@Override
40+
public HttpURLConnection getConnection(String url, Proxy proxy) throws IOException {
41+
HttpURLConnection connMock = mock(HttpURLConnection.class);
42+
when(connMock.getInputStream()).thenReturn(responseStream);
43+
when(connMock.getOutputStream()).thenReturn(requestStream);
44+
return connMock;
45+
}
46+
47+
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
package com.qcloud.weapp.test.authorization;
2+
3+
import static org.junit.Assert.*;
4+
import static org.mockito.Mockito.*;
5+
6+
import java.io.IOException;
7+
8+
import org.json.JSONException;
9+
import org.json.JSONObject;
10+
import org.junit.*;
11+
12+
import com.qcloud.weapp.Configuration;
13+
import com.qcloud.weapp.ConfigurationException;
14+
import com.qcloud.weapp.ConfigurationManager;
15+
import com.qcloud.weapp.authorization.*;
16+
import com.qcloud.weapp.test.HttpMock;
17+
18+
@SuppressWarnings("unused")
19+
public class LoginServiceTest {
20+
21+
private LoginServiceTestHelper helper = new LoginServiceTestHelper();
22+
23+
@Before
24+
public void setup() {
25+
Configuration config = new Configuration();
26+
config.setServerHost("test.qcloud.la");
27+
config.setAuthServerUrl("http://127.0.0.1:10086/auth");
28+
config.setTunnelServerUrl("http://127.0.0.1:10086/tunnel");
29+
config.setTunnelSignatureKey("test key");
30+
config.setNetworkTimeout(1);
31+
try {
32+
ConfigurationManager.setup(config);
33+
} catch (ConfigurationException e) {
34+
e.printStackTrace();
35+
}
36+
}
37+
38+
@Test
39+
public void testLoginProcess() {
40+
HttpMock mock = helper.createLoginHttpMock("valid-code", "valid-data");
41+
LoginService service = new LoginService(mock.request, mock.response);
42+
43+
try {
44+
UserInfo userInfo = service.login();
45+
assertNotNull(userInfo);
46+
} catch (IllegalArgumentException | LoginServiceException | ConfigurationException e) {
47+
e.printStackTrace();
48+
fail(e.getMessage());
49+
}
50+
51+
try {
52+
JSONObject body = new JSONObject(mock.getResponseText());
53+
assertTrue(helper.checkBodyHasMagicId(body));
54+
assertTrue(helper.checkBodyHasSession(body));
55+
} catch (JSONException e) {
56+
fail("invalid response body");
57+
}
58+
}
59+
60+
@Test
61+
public void testLoginProcessWithoutCodeOrData() {
62+
testLoginProcessExpectError(null, "valid-data");
63+
testLoginProcessExpectError("valid-code", null);
64+
}
65+
66+
@Test
67+
public void testLoginProcessWithInvalidCodeOrData() {
68+
testLoginProcessExpectError("invalid-code", "valid-data");
69+
testLoginProcessExpectError("valid-code", "invalid-data");
70+
}
71+
72+
@Test
73+
public void testLoginProcessWithServerResponseError() {
74+
testLoginProcessExpectError("expect-valid-json", "valid-data");
75+
}
76+
77+
@Test
78+
public void testLoginProcessWithServer500() {
79+
testLoginProcessExpectError("expect-500", "valid-data");
80+
}
81+
82+
@Test
83+
public void testLoginProcessWithServerTimeout() {
84+
testLoginProcessExpectError("expect-timeout", "valid-data");
85+
}
86+
87+
private LoginServiceException testLoginProcessExpectError(String code, String encryptData) {
88+
HttpMock mock = helper.createLoginHttpMock(code, encryptData);
89+
LoginService service = new LoginService(mock.request, mock.response);
90+
91+
LoginServiceException errorShouldThrow = null;
92+
try {
93+
UserInfo userInfo = service.login();
94+
} catch (LoginServiceException e) {
95+
errorShouldThrow = e;
96+
} catch (ConfigurationException e) {
97+
e.printStackTrace();
98+
}
99+
100+
assertNotNull(errorShouldThrow);
101+
try {
102+
JSONObject body = new JSONObject(mock.getResponseText());
103+
assertTrue(helper.checkBodyHasMagicId(body));
104+
assertNotNull(body.get("error"));
105+
} catch (JSONException e) {
106+
fail("invalid response body");
107+
}
108+
return errorShouldThrow;
109+
}
110+
111+
@Test
112+
public void testCheck() {
113+
HttpMock mock = helper.createCheckHttpMock("valid-id", "valid-key");
114+
LoginService service = new LoginService(mock.request, mock.response);
115+
try {
116+
UserInfo userInfo = service.check();
117+
assertNotNull(userInfo);
118+
} catch (IllegalArgumentException | LoginServiceException | ConfigurationException e) {
119+
fail(e.getMessage());
120+
e.printStackTrace();
121+
}
122+
try {
123+
verify(mock.response, never()).getWriter();
124+
} catch (IOException e) {
125+
e.printStackTrace();
126+
}
127+
}
128+
129+
@Test
130+
public void testCheckWithoutIdOrSkey() {
131+
testCheckExpectError(null, "valid-key", null);
132+
testCheckExpectError("valid-id", null, null);
133+
}
134+
135+
@Test
136+
public void testCheckWithInvalidIdOrSkey() {
137+
testCheckExpectError("invalid-id", "valid-key", false);
138+
testCheckExpectError("valid-id", "invalid-key", false);
139+
}
140+
141+
@Test
142+
public void testCheckWithInvalidSession() {
143+
testCheckExpectError("expect-60011", "valid-key", true);
144+
testCheckExpectError("expect-60012", "valid-key", true);
145+
}
146+
147+
@Test
148+
public void testCheckWithServerInvalidResponse() {
149+
testCheckExpectError("expect-invalid-json", "valid-key", false);
150+
}
151+
152+
@Test
153+
public void testCheckExpectError() {
154+
testCheckExpectError("expect-500", "valid-key", false);
155+
}
156+
157+
@Test
158+
public void testCheckWithServerTimeout() {
159+
testCheckExpectError("expect-timeout", "valid-key", false);
160+
}
161+
162+
private void testCheckExpectError(String id, String skey, Boolean expectInvalidSession) {
163+
HttpMock mock = helper.createCheckHttpMock(id, skey);
164+
LoginService service = new LoginService(mock.request, mock.response);
165+
LoginServiceException errorShouldThrow = null;
166+
167+
try {
168+
UserInfo userInfo = service.check();
169+
} catch (LoginServiceException e) {
170+
errorShouldThrow = e;
171+
} catch (ConfigurationException e) {
172+
e.printStackTrace();
173+
}
174+
175+
JSONObject body = null;
176+
try {
177+
body = new JSONObject(mock.getResponseText());
178+
assertTrue(helper.checkBodyHasMagicId(body));
179+
String error = body.getString("error");
180+
if (expectInvalidSession != null) {
181+
assertEquals(error.equals("ERR_INVALID_SESSION"), expectInvalidSession);
182+
}
183+
} catch (JSONException e) {
184+
fail("响应格式错误::" + e.getMessage());
185+
}
186+
try {
187+
verify(mock.response, times(1)).getWriter();
188+
} catch (IOException e) {
189+
e.printStackTrace();
190+
}
191+
}
192+
}

0 commit comments

Comments
 (0)