Skip to content

Commit

Permalink
prevent abuse of mock
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Sep 27, 2024
1 parent 4126b80 commit c9a6b0f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void testHttpDebug() {

@Test
public void testDebugHttpRequest() throws ClientException {
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
Mockito.when(request.getSysMethod()).thenReturn(MethodType.GET);
Mockito.when(request.getSysUrl()).thenReturn("http://test.domain");
Map<String, String> requestHeaders = new HashMap<String, String>();
Expand Down Expand Up @@ -110,7 +110,7 @@ public void testDebugHttpResponse() throws ClientException {

@Test
public void testDebugHttpRquestException() throws ClientException {
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
Mockito.when(request.getSysMethod()).thenReturn(MethodType.GET);
Mockito.when(request.getSysUrl()).thenReturn("httpss://test.domain/jdj");
Map<String, String> requestHeaders = new HashMap<String, String>();
Expand Down Expand Up @@ -163,44 +163,44 @@ public void testDebugHttpResponseException() throws ClientException {

@Test
public void testGetJDKProxyException() throws ClientException {
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
thrown.expect(ClientException.class);
Proxy proxy = HttpUtil.getJDKProxy("http0://www.aliyun.com", null, request);
Assert.assertNotNull(proxy);
}

@Test
public void testGetJDKProxyEnvProxyHasUserInfo() throws ClientException {
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
Proxy proxy = HttpUtil.getJDKProxy(null, "http://user:[email protected]", request);
Assert.assertNotNull(proxy);
}

@Test
public void testGetJDKProxyEnvProxyNoUserInfo() throws ClientException {
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
Proxy proxy = HttpUtil.getJDKProxy(null, "http://www.aliyun.com:80", request);
Assert.assertNotNull(proxy);
}

@Test
public void testGetApacheProxyException() throws ClientException {
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
thrown.expect(ClientException.class);
HttpHost proxy = HttpUtil.getApacheProxy("http0://www.aliyun.com", null, request);
Assert.assertNotNull(proxy);
}

@Test
public void testGetApacheProxyEnvProxyHasUserInfo() throws ClientException {
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
HttpHost proxy = HttpUtil.getApacheProxy(null, "http://user:[email protected]", request);
Assert.assertNotNull(proxy);
}

@Test
public void testGetApacheProxyEnvProxyNoUserInfo() throws ClientException {
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
HttpHost proxy = HttpUtil.getApacheProxy(null, "http://www.aliyun.com:80", request);
Assert.assertNotNull(proxy);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void asyncInvokeTest() throws ClientException, IOException {
when(config.getSslSocketFactory()).thenReturn(sslSocketFactory);
when(config.isIgnoreSSLCerts()).thenReturn(false);
CompatibleUrlConnClient client = new CompatibleUrlConnClient(config);
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mockurl");
CallBack callback = mock(CallBack.class);
client.asyncInvoke(request, callback);
client.close();
Expand Down Expand Up @@ -121,7 +121,7 @@ public void buildHttpConnectionNullSysUrlTest() throws Exception {
when(config.isIgnoreSSLCerts()).thenReturn(true);
CompatibleUrlConnClient client = new CompatibleUrlConnClient(config);
thrown.expect(IllegalArgumentException.class);
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
Whitebox.invokeMethod(client, "buildHttpConnection", request);
}

Expand All @@ -133,7 +133,7 @@ public void buildHttpConnectionNullSysMethodTest() throws Exception {
when(config.isIgnoreSSLCerts()).thenReturn(true);
CompatibleUrlConnClient client = new CompatibleUrlConnClient(config);
thrown.expect(IllegalArgumentException.class);
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
when(request.getSysUrl()).thenReturn("sysUrl");
Whitebox.invokeMethod(client, "buildHttpConnection", request);
}
Expand All @@ -148,7 +148,7 @@ public void buildHttpConnectionPOSTMethodAndNullContentAndHttpsTest() throws Exc
CompatibleUrlConnClient client = PowerMockito.spy(client0);
Proxy proxy = Proxy.NO_PROXY;
PowerMockito.doReturn(proxy).when(client, "calcProxy", any(URL.class), any(HttpRequest.class));
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
when(request.getSysMethod()).thenReturn(MethodType.POST);
when(request.getSysUrl()).thenReturn("https://www.aliyun.com");
when(request.getSysConnectTimeout()).thenReturn(120);
Expand Down Expand Up @@ -178,7 +178,7 @@ public void buildHttpConnectionPOSTMethodAndHttpTest() throws Exception {
CompatibleUrlConnClient client = PowerMockito.spy(client0);
Proxy proxy = Proxy.NO_PROXY;
PowerMockito.doReturn(proxy).when(client, "calcProxy", any(URL.class), any(HttpRequest.class));
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
when(request.getHttpContent()).thenReturn("content".getBytes());
when(request.getSysMethod()).thenReturn(MethodType.POST);
when(request.getSysUrl()).thenReturn("http://www.aliyun.com");
Expand All @@ -196,7 +196,7 @@ public void buildHttpConnectionGETMethodAndHttpsTest() throws Exception {
CompatibleUrlConnClient client = PowerMockito.spy(client0);
Proxy proxy = Proxy.NO_PROXY;
PowerMockito.doReturn(proxy).when(client, "calcProxy", any(URL.class), any(HttpRequest.class));
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
when(request.getHttpContent()).thenReturn("content".getBytes());
when(request.getSysMethod()).thenReturn(MethodType.POST);
when(request.getSysUrl()).thenReturn("https://www.aliyun.com");
Expand All @@ -216,7 +216,7 @@ public void buildHttpConnectionGETMethodAndNullContentTest() throws Exception {
CompatibleUrlConnClient client = PowerMockito.spy(client0);
Proxy proxy = Proxy.NO_PROXY;
PowerMockito.doReturn(proxy).when(client, "calcProxy", any(URL.class), any(HttpRequest.class));
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
when(request.getHttpContent()).thenReturn(null);
when(request.getSysMethod()).thenReturn(MethodType.GET);
when(request.getSysUrl()).thenReturn("http://www.aliyun.com");
Expand Down Expand Up @@ -271,7 +271,7 @@ public void syncInvokeIOExceptionTest() throws Exception {
when(config.isIgnoreSSLCerts()).thenReturn(true);
CompatibleUrlConnClient client0 = new CompatibleUrlConnClient(config);
CompatibleUrlConnClient client = PowerMockito.spy(client0);
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
HttpURLConnection connection = mock(HttpURLConnection.class);
doThrow(new IOException()).when(connection).connect();
PowerMockito.doReturn(connection).when(client, "buildHttpConnection", request);
Expand All @@ -292,7 +292,7 @@ public void syncInvokeNormalAndNoneMethodAndContentIsNotEmptyTest() throws Excep
when(config.isIgnoreSSLCerts()).thenReturn(true);
CompatibleUrlConnClient client0 = new CompatibleUrlConnClient(config);
CompatibleUrlConnClient client = PowerMockito.spy(client0);
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
when(request.getHttpContent()).thenReturn("http content".getBytes());
HttpURLConnection connection = mock(HttpURLConnection.class);
doNothing().when(connection).connect();
Expand All @@ -316,7 +316,7 @@ public void syncInvokeNormalAndGetMethodAndContentIsNotEmptyTest() throws Except
when(config.isIgnoreSSLCerts()).thenReturn(true);
CompatibleUrlConnClient client0 = new CompatibleUrlConnClient(config);
CompatibleUrlConnClient client = PowerMockito.spy(client0);
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
when(request.getSysMethod()).thenReturn(MethodType.GET);
when(request.getHttpContent()).thenReturn("http content".getBytes());
HttpURLConnection connection = mock(HttpURLConnection.class);
Expand Down Expand Up @@ -345,7 +345,7 @@ public void syncInvokeNormalAndPostMethodAndContentIsNotEmptyTest() throws Excep
when(config.isIgnoreSSLCerts()).thenReturn(true);
CompatibleUrlConnClient client0 = new CompatibleUrlConnClient(config);
CompatibleUrlConnClient client = PowerMockito.spy(client0);
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
when(request.getSysMethod()).thenReturn(MethodType.POST);
when(request.getHttpContent()).thenReturn("http content".getBytes());
HttpURLConnection connection = mock(HttpURLConnection.class);
Expand Down Expand Up @@ -373,7 +373,7 @@ public void syncInvokeNormalAndContentIsEmptyTest() throws Exception {
when(config.isIgnoreSSLCerts()).thenReturn(true);
CompatibleUrlConnClient client0 = new CompatibleUrlConnClient(config);
CompatibleUrlConnClient client = PowerMockito.spy(client0);
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
when(request.getSysMethod()).thenReturn(MethodType.GET);
when(request.getHttpContent()).thenReturn("".getBytes());
HttpURLConnection connection = mock(HttpURLConnection.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void retryTest() throws InterruptedException {
.build();

String coordinate = "nanhe:test";
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
RetryPolicyContext context = RetryPolicyContext.builder()
.coordinate(coordinate)
.httpRequest(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void logUnitGetSetTest() throws ClientException {
null, resHeaders);
mockLogUtils();
LogUtils.LogUnit logUnit = new LogUtils.LogUnit(httpRequest, httpResponse);
HttpRequest request = mock(HttpRequest.class);
HttpRequest request = new HttpRequest("mock url");
logUnit.setHttpRequest(request);
Assert.assertEquals(request, logUnit.getHttpRequest());

Expand Down

0 comments on commit c9a6b0f

Please sign in to comment.