Skip to content

Commit 37baa14

Browse files
committed
prevent abuse of mock
1 parent 4126b80 commit 37baa14

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

aliyun-java-sdk-core/src/test/java/com/aliyuncs/http/HttpUtilTest.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,32 @@ public void testHttpDebug() {
4343
}
4444

4545
@Test
46-
public void testDebugHttpRequest() throws ClientException {
47-
HttpRequest request = mock(HttpRequest.class);
48-
Mockito.when(request.getSysMethod()).thenReturn(MethodType.GET);
49-
Mockito.when(request.getSysUrl()).thenReturn("http://test.domain");
46+
public void testDebugHttpRequest() {
5047
Map<String, String> requestHeaders = new HashMap<String, String>();
51-
Mockito.when(request.getHttpContentString()).thenReturn("request body");
5248
requestHeaders.put("test1", "test1");
5349
requestHeaders.put("test2", "test2");
54-
Mockito.when(request.getSysHeaders()).thenReturn(requestHeaders);
55-
String exceptString = "> GET HTTP/1.1\n> Host : test.domain\n> test2 : test2\n> test1 : test1\n> "
50+
HttpRequest request = new HttpRequest("http://test.domain", requestHeaders);
51+
request.setSysMethod(MethodType.POST);
52+
request.setSysReadTimeout(0);
53+
request.setSysConnectTimeout(0);
54+
request.setHttpContent("request body".getBytes(), "utf-8", null);
55+
String exceptString = "> POST HTTP/1.1\n> Host : test.domain\n> test2 : test2\n> Content-Length : 12\n"
56+
+ "> test1 : test1\n> Content-MD5 : YhZ08xUv78xtnjbDzSonAQ==\n> "
5657
+ "Request URL : http://test.domain\n> " + "Request string to sign: [null]\n> "
5758
+ "Request isIgnoreSSLCerts : false\n> " + "Request connect timeout : 0\n> "
58-
+ "Request read timeout : 0\n> " + "Encoding : null\n> " + "\n" + "request body";
59+
+ "Request read timeout : 0\n> " + "Encoding : utf-8\n> " + "\n" + "request body";
5960

6061
HttpUtil.setIsHttpDebug(true);
6162
HttpUtil.setIsHttpContentDebug(true);
62-
Assert.assertEquals(HttpUtil.debugHttpRequest(request), exceptString);
63+
Assert.assertEquals(exceptString, HttpUtil.debugHttpRequest(request));
6364

6465
HttpUtil.setIsHttpContentDebug(false);
65-
exceptString = "> GET HTTP/1.1\n> Host : test.domain\n> test2 : test2\n> test1 : test1\n> "
66+
exceptString = "> POST HTTP/1.1\n> Host : test.domain\n> test2 : test2\n> Content-Length : 12\n"
67+
+ "> test1 : test1\n> Content-MD5 : YhZ08xUv78xtnjbDzSonAQ==\n> "
6668
+ "Request URL : http://test.domain\n> " + "Request string to sign: [null]\n> "
6769
+ "Request isIgnoreSSLCerts : false\n> " + "Request connect timeout : 0\n> "
68-
+ "Request read timeout : 0\n> " + "Encoding : null\n> ";
69-
Assert.assertEquals(HttpUtil.debugHttpRequest(request), exceptString);
70+
+ "Request read timeout : 0\n> " + "Encoding : utf-8\n> ";
71+
Assert.assertEquals(exceptString, HttpUtil.debugHttpRequest(request));
7072

7173
HttpUtil.setIsHttpDebug(false);
7274
Assert.assertNull(HttpUtil.debugHttpRequest(request));
@@ -110,7 +112,7 @@ public void testDebugHttpResponse() throws ClientException {
110112

111113
@Test
112114
public void testDebugHttpRquestException() throws ClientException {
113-
HttpRequest request = mock(HttpRequest.class);
115+
HttpRequest request = new HttpRequest("mock url");
114116
Mockito.when(request.getSysMethod()).thenReturn(MethodType.GET);
115117
Mockito.when(request.getSysUrl()).thenReturn("httpss://test.domain/jdj");
116118
Map<String, String> requestHeaders = new HashMap<String, String>();
@@ -163,44 +165,44 @@ public void testDebugHttpResponseException() throws ClientException {
163165

164166
@Test
165167
public void testGetJDKProxyException() throws ClientException {
166-
HttpRequest request = mock(HttpRequest.class);
168+
HttpRequest request = new HttpRequest("mock url");
167169
thrown.expect(ClientException.class);
168170
Proxy proxy = HttpUtil.getJDKProxy("http0://www.aliyun.com", null, request);
169171
Assert.assertNotNull(proxy);
170172
}
171173

172174
@Test
173175
public void testGetJDKProxyEnvProxyHasUserInfo() throws ClientException {
174-
HttpRequest request = mock(HttpRequest.class);
176+
HttpRequest request = new HttpRequest("mock url");
175177
Proxy proxy = HttpUtil.getJDKProxy(null, "http://user:passwd@www.aliyun.com", request);
176178
Assert.assertNotNull(proxy);
177179
}
178180

179181
@Test
180182
public void testGetJDKProxyEnvProxyNoUserInfo() throws ClientException {
181-
HttpRequest request = mock(HttpRequest.class);
183+
HttpRequest request = new HttpRequest("mock url");
182184
Proxy proxy = HttpUtil.getJDKProxy(null, "http://www.aliyun.com:80", request);
183185
Assert.assertNotNull(proxy);
184186
}
185187

186188
@Test
187189
public void testGetApacheProxyException() throws ClientException {
188-
HttpRequest request = mock(HttpRequest.class);
190+
HttpRequest request = new HttpRequest("mock url");
189191
thrown.expect(ClientException.class);
190192
HttpHost proxy = HttpUtil.getApacheProxy("http0://www.aliyun.com", null, request);
191193
Assert.assertNotNull(proxy);
192194
}
193195

194196
@Test
195197
public void testGetApacheProxyEnvProxyHasUserInfo() throws ClientException {
196-
HttpRequest request = mock(HttpRequest.class);
198+
HttpRequest request = new HttpRequest("mock url");
197199
HttpHost proxy = HttpUtil.getApacheProxy(null, "http://user:passwd@www.aliyun.com", request);
198200
Assert.assertNotNull(proxy);
199201
}
200202

201203
@Test
202204
public void testGetApacheProxyEnvProxyNoUserInfo() throws ClientException {
203-
HttpRequest request = mock(HttpRequest.class);
205+
HttpRequest request = new HttpRequest("mock url");
204206
HttpHost proxy = HttpUtil.getApacheProxy(null, "http://www.aliyun.com:80", request);
205207
Assert.assertNotNull(proxy);
206208
}

aliyun-java-sdk-core/src/test/java/com/aliyuncs/http/clients/CompatibleUrlConnClientTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void asyncInvokeTest() throws ClientException, IOException {
7171
when(config.getSslSocketFactory()).thenReturn(sslSocketFactory);
7272
when(config.isIgnoreSSLCerts()).thenReturn(false);
7373
CompatibleUrlConnClient client = new CompatibleUrlConnClient(config);
74-
HttpRequest request = mock(HttpRequest.class);
74+
HttpRequest request = new HttpRequest("mockurl");
7575
CallBack callback = mock(CallBack.class);
7676
client.asyncInvoke(request, callback);
7777
client.close();
@@ -121,7 +121,7 @@ public void buildHttpConnectionNullSysUrlTest() throws Exception {
121121
when(config.isIgnoreSSLCerts()).thenReturn(true);
122122
CompatibleUrlConnClient client = new CompatibleUrlConnClient(config);
123123
thrown.expect(IllegalArgumentException.class);
124-
HttpRequest request = mock(HttpRequest.class);
124+
HttpRequest request = new HttpRequest("mock url");
125125
Whitebox.invokeMethod(client, "buildHttpConnection", request);
126126
}
127127

@@ -133,7 +133,7 @@ public void buildHttpConnectionNullSysMethodTest() throws Exception {
133133
when(config.isIgnoreSSLCerts()).thenReturn(true);
134134
CompatibleUrlConnClient client = new CompatibleUrlConnClient(config);
135135
thrown.expect(IllegalArgumentException.class);
136-
HttpRequest request = mock(HttpRequest.class);
136+
HttpRequest request = new HttpRequest("mock url");
137137
when(request.getSysUrl()).thenReturn("sysUrl");
138138
Whitebox.invokeMethod(client, "buildHttpConnection", request);
139139
}
@@ -148,7 +148,7 @@ public void buildHttpConnectionPOSTMethodAndNullContentAndHttpsTest() throws Exc
148148
CompatibleUrlConnClient client = PowerMockito.spy(client0);
149149
Proxy proxy = Proxy.NO_PROXY;
150150
PowerMockito.doReturn(proxy).when(client, "calcProxy", any(URL.class), any(HttpRequest.class));
151-
HttpRequest request = mock(HttpRequest.class);
151+
HttpRequest request = new HttpRequest("mock url");
152152
when(request.getSysMethod()).thenReturn(MethodType.POST);
153153
when(request.getSysUrl()).thenReturn("https://www.aliyun.com");
154154
when(request.getSysConnectTimeout()).thenReturn(120);
@@ -178,7 +178,7 @@ public void buildHttpConnectionPOSTMethodAndHttpTest() throws Exception {
178178
CompatibleUrlConnClient client = PowerMockito.spy(client0);
179179
Proxy proxy = Proxy.NO_PROXY;
180180
PowerMockito.doReturn(proxy).when(client, "calcProxy", any(URL.class), any(HttpRequest.class));
181-
HttpRequest request = mock(HttpRequest.class);
181+
HttpRequest request = new HttpRequest("mock url");
182182
when(request.getHttpContent()).thenReturn("content".getBytes());
183183
when(request.getSysMethod()).thenReturn(MethodType.POST);
184184
when(request.getSysUrl()).thenReturn("http://www.aliyun.com");
@@ -196,7 +196,7 @@ public void buildHttpConnectionGETMethodAndHttpsTest() throws Exception {
196196
CompatibleUrlConnClient client = PowerMockito.spy(client0);
197197
Proxy proxy = Proxy.NO_PROXY;
198198
PowerMockito.doReturn(proxy).when(client, "calcProxy", any(URL.class), any(HttpRequest.class));
199-
HttpRequest request = mock(HttpRequest.class);
199+
HttpRequest request = new HttpRequest("mock url");
200200
when(request.getHttpContent()).thenReturn("content".getBytes());
201201
when(request.getSysMethod()).thenReturn(MethodType.POST);
202202
when(request.getSysUrl()).thenReturn("https://www.aliyun.com");
@@ -216,7 +216,7 @@ public void buildHttpConnectionGETMethodAndNullContentTest() throws Exception {
216216
CompatibleUrlConnClient client = PowerMockito.spy(client0);
217217
Proxy proxy = Proxy.NO_PROXY;
218218
PowerMockito.doReturn(proxy).when(client, "calcProxy", any(URL.class), any(HttpRequest.class));
219-
HttpRequest request = mock(HttpRequest.class);
219+
HttpRequest request = new HttpRequest("mock url");
220220
when(request.getHttpContent()).thenReturn(null);
221221
when(request.getSysMethod()).thenReturn(MethodType.GET);
222222
when(request.getSysUrl()).thenReturn("http://www.aliyun.com");
@@ -271,7 +271,7 @@ public void syncInvokeIOExceptionTest() throws Exception {
271271
when(config.isIgnoreSSLCerts()).thenReturn(true);
272272
CompatibleUrlConnClient client0 = new CompatibleUrlConnClient(config);
273273
CompatibleUrlConnClient client = PowerMockito.spy(client0);
274-
HttpRequest request = mock(HttpRequest.class);
274+
HttpRequest request = new HttpRequest("mock url");
275275
HttpURLConnection connection = mock(HttpURLConnection.class);
276276
doThrow(new IOException()).when(connection).connect();
277277
PowerMockito.doReturn(connection).when(client, "buildHttpConnection", request);
@@ -292,7 +292,7 @@ public void syncInvokeNormalAndNoneMethodAndContentIsNotEmptyTest() throws Excep
292292
when(config.isIgnoreSSLCerts()).thenReturn(true);
293293
CompatibleUrlConnClient client0 = new CompatibleUrlConnClient(config);
294294
CompatibleUrlConnClient client = PowerMockito.spy(client0);
295-
HttpRequest request = mock(HttpRequest.class);
295+
HttpRequest request = new HttpRequest("mock url");
296296
when(request.getHttpContent()).thenReturn("http content".getBytes());
297297
HttpURLConnection connection = mock(HttpURLConnection.class);
298298
doNothing().when(connection).connect();
@@ -316,7 +316,7 @@ public void syncInvokeNormalAndGetMethodAndContentIsNotEmptyTest() throws Except
316316
when(config.isIgnoreSSLCerts()).thenReturn(true);
317317
CompatibleUrlConnClient client0 = new CompatibleUrlConnClient(config);
318318
CompatibleUrlConnClient client = PowerMockito.spy(client0);
319-
HttpRequest request = mock(HttpRequest.class);
319+
HttpRequest request = new HttpRequest("mock url");
320320
when(request.getSysMethod()).thenReturn(MethodType.GET);
321321
when(request.getHttpContent()).thenReturn("http content".getBytes());
322322
HttpURLConnection connection = mock(HttpURLConnection.class);
@@ -345,7 +345,7 @@ public void syncInvokeNormalAndPostMethodAndContentIsNotEmptyTest() throws Excep
345345
when(config.isIgnoreSSLCerts()).thenReturn(true);
346346
CompatibleUrlConnClient client0 = new CompatibleUrlConnClient(config);
347347
CompatibleUrlConnClient client = PowerMockito.spy(client0);
348-
HttpRequest request = mock(HttpRequest.class);
348+
HttpRequest request = new HttpRequest("mock url");
349349
when(request.getSysMethod()).thenReturn(MethodType.POST);
350350
when(request.getHttpContent()).thenReturn("http content".getBytes());
351351
HttpURLConnection connection = mock(HttpURLConnection.class);
@@ -373,7 +373,7 @@ public void syncInvokeNormalAndContentIsEmptyTest() throws Exception {
373373
when(config.isIgnoreSSLCerts()).thenReturn(true);
374374
CompatibleUrlConnClient client0 = new CompatibleUrlConnClient(config);
375375
CompatibleUrlConnClient client = PowerMockito.spy(client0);
376-
HttpRequest request = mock(HttpRequest.class);
376+
HttpRequest request = new HttpRequest("mock url");
377377
when(request.getSysMethod()).thenReturn(MethodType.GET);
378378
when(request.getHttpContent()).thenReturn("".getBytes());
379379
HttpURLConnection connection = mock(HttpURLConnection.class);

aliyun-java-sdk-core/src/test/java/com/aliyuncs/policy/retry/RetryPolicyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void retryTest() throws InterruptedException {
8282
.build();
8383

8484
String coordinate = "nanhe:test";
85-
HttpRequest request = mock(HttpRequest.class);
85+
HttpRequest request = new HttpRequest("mock url");
8686
RetryPolicyContext context = RetryPolicyContext.builder()
8787
.coordinate(coordinate)
8888
.httpRequest(request)

aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/LogUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void logUnitGetSetTest() throws ClientException {
164164
null, resHeaders);
165165
mockLogUtils();
166166
LogUtils.LogUnit logUnit = new LogUtils.LogUnit(httpRequest, httpResponse);
167-
HttpRequest request = mock(HttpRequest.class);
167+
HttpRequest request = new HttpRequest("mock url");
168168
logUnit.setHttpRequest(request);
169169
Assert.assertEquals(request, logUnit.getHttpRequest());
170170

0 commit comments

Comments
 (0)