Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ public TankHttpClient3() {
httpclient.setState(new HttpState());
}

/**
* constructor for test
*/
public TankHttpClient3(HttpClient httpclient) {
this.httpclient = httpclient;
}

public Object createHttpClient() { return null; }

public void setHttpClient(Object httpClient) {}
Expand Down Expand Up @@ -434,19 +441,14 @@ private void processResponse(byte[] bResponse, long waitTime, BaseRequest reques
@SuppressWarnings("rawtypes")
private void setHeaders(BaseRequest request, HttpMethod method, HashMap<String, String> headerInformation) {
try {
Set set = headerInformation.entrySet();

for (Object aSet : set) {
Map.Entry mapEntry = (Map.Entry) aSet;
method.setRequestHeader((String) mapEntry.getKey(), (String) mapEntry.getValue());
}
headerInformation.entrySet().forEach(entry -> {
method.setRequestHeader((String) ((Map.Entry) entry).getKey(), (String) ((Map.Entry) entry).getValue());
});
} catch (Exception ex) {
LOG.warn(request.getLogUtil().getLogMessage("Unable to set header: " + ex.getMessage(), LogEventType.System));
}
}



private List<Part> buildParts(BaseRequest request) {
List<Part> parts = new ArrayList<Part>();
for (PartHolder h : TankHttpUtil.getPartsFromBody(request)) {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,12 @@
"method" : "POST"
},
"response" : {
"status" : 200
"status" : 200,
"body" : "{\n \"key\" : \"value\"\n}",
"headers": {
"Content-Type": "application/json",
"x-envoy-upstream-service-time": "NumberFormatException"
}
},
"uuid" : "62f1251b-62e1-453f-86ae-3568c80feaa6",
"persistent" : true,
Expand Down Expand Up @@ -1524,7 +1529,12 @@
"method" : "GET"
},
"response" : {
"status" : 200
"status" : 200,
"body" : "{ \"message\": \"The request's query parameters.\"}",
"headers": {
"Content-Type": "application/json",
"x-envoy-upstream-service-time": "123456"
}
},
"uuid" : "eb4cd338-b7d5-411b-9c59-56d8a53b8e42",
"persistent" : true,
Expand Down Expand Up @@ -2664,5 +2674,29 @@
}
},
"insertionIndex" : 109
}, {
"id" : "eb4cd338-b7d5-411b-9c59-56d8a53b8e43",
"name" : "The request's options. - 204",
"request" : {
"urlPath" : "/options",
"method" : "OPTIONS"
},
"response" : {
"status" : 204
},
"uuid" : "eb4cd338-b7d5-411b-9c59-56d8a53b8e43",
"persistent" : true,
"metadata" : {
"mocklab" : {
"created" : {
"at" : "2023-03-28T09:23:52.398583Z",
"via" : "OAS3_IMPORT"
},
"oas" : {
"operationId" : "(none)"
}
}
},
"insertionIndex" : 110
} ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,10 @@ public void setCookie(TankCookie tankCookie) {

@Override
public void setProxy(String proxyhost, int proxyport) {
if (StringUtils.isNotBlank(proxyhost)) {
HttpHost proxy = new HttpHost(proxyhost, proxyport);
RequestConfig requestConfig =
context.getRequestConfig().custom().setProxy(proxy).build();
context.setRequestConfig(requestConfig);
} else {
RequestConfig requestConfig =
context.getRequestConfig().custom().setProxy(null).build();
context.setRequestConfig(requestConfig);
}
RequestConfig requestConfig = (StringUtils.isNotBlank(proxyhost))
? context.getRequestConfig().custom().setProxy(new HttpHost(proxyhost, proxyport)).build()
: context.getRequestConfig().custom().setProxy(null).build();
context.setRequestConfig(requestConfig);
}

private void sendRequest(BaseRequest request, @Nonnull HttpRequestBase method, String requestBody) {
Expand Down Expand Up @@ -453,18 +447,14 @@ private void processResponse(byte[] bResponse, long waitTime, BaseRequest reques
@SuppressWarnings("rawtypes")
private void setHeaders(BaseRequest request, HttpRequestBase method, HashMap<String, String> headerInformation) {
try {
Set set = headerInformation.entrySet();

for (Object aSet : set) {
Map.Entry mapEntry = (Map.Entry) aSet;
method.setHeader((String) mapEntry.getKey(), (String) mapEntry.getValue());
}
headerInformation.entrySet().forEach(entry -> {
method.setHeader((String) ((Map.Entry) entry).getKey(), (String) ((Map.Entry) entry).getValue());
});
} catch (Exception ex) {
LOG.warn(request.getLogUtil().getLogMessage("Unable to set header: " + ex.getMessage(), LogEventType.System));
}
}


private HttpEntity buildParts(BaseRequest request) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
String contentType = request.getContentType();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.intuit.tank.httpclient4;

import java.io.IOException;
import java.net.SocketException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.net.UnknownHostException;
import java.util.Base64;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;

Expand All @@ -17,13 +20,17 @@
import com.intuit.tank.http.TankCookie;
import com.intuit.tank.http.TankHttpClient;
import com.intuit.tank.test.TestGroups;
import org.apache.http.impl.client.CloseableHttpClient;
import org.junit.jupiter.api.*;

import static org.junit.jupiter.api.Assertions.*;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.mockito.Mockito.when;

public class TankHttpClient4Test {

Expand All @@ -46,7 +53,7 @@ public void teardown() {
@Test
@Tag(TestGroups.FUNCTIONAL)
public void testBasicAuth() {
BaseRequest request = getRequest(new TankHttpClient4(), wireMockServer.baseUrl() + "/basic-auth/test/test_pass");
BaseRequest request = getRequest(new TankHttpClient4(), null, wireMockServer.baseUrl() + "/basic-auth/test/test_pass");
request.getHttpclient().addAuth(AuthCredentials.builder().withUserName("test").withPassword("test_pass").withRealm("bogus").withScheme(AuthScheme.Basic).build());
request.addHeader("X-Test-Mode", "Fail");

Expand All @@ -55,7 +62,7 @@ public void testBasicAuth() {
assertNotNull(response);
assertEquals(401, response.getHttpCode());

request = getRequest(new TankHttpClient4(), wireMockServer.baseUrl() + "/basic-auth/test/test_pass");
request = getRequest(new TankHttpClient4(), null, wireMockServer.baseUrl() + "/basic-auth/test/test_pass");
request.getHttpclient().addAuth(AuthCredentials.builder().withUserName("test").withPassword("test_pass").withHost("localhost").withRealm("Fake Realm").withScheme(AuthScheme.Basic).build());
request.addHeader("X-Test-Mode", "Pass");

Expand All @@ -70,7 +77,7 @@ public void testBasicAuth() {
@Test
@Tag(TestGroups.FUNCTIONAL)
public void testDigestAuth() {
BaseRequest request = getRequest(new TankHttpClient4(), wireMockServer.baseUrl() + "/digest-auth/auth/test/test_pass");
BaseRequest request = getRequest(new TankHttpClient4(), null, wireMockServer.baseUrl() + "/digest-auth/auth/test/test_pass");
request.getHttpclient().addAuth(AuthCredentials.builder().withUserName("test").withPassword("test_pass").withRealm("bogus").withScheme(AuthScheme.Digest).build());
request.addHeader("X-Test-Mode", "Fail");

Expand All @@ -79,7 +86,7 @@ public void testDigestAuth() {
assertNotNull(response);
assertEquals(401, response.getHttpCode());

request = getRequest(new TankHttpClient4(), wireMockServer.baseUrl() + "/digest-auth/auth/test/test_pass");
request = getRequest(new TankHttpClient4(), null, wireMockServer.baseUrl() + "/digest-auth/auth/test/test_pass");
request.getHttpclient().addAuth(AuthCredentials.builder().withUserName("test").withPassword("test_pass").withHost("httpbin.org").withScheme(AuthScheme.Digest).build());
request.addHeader("X-Test-Mode", "Pass");

Expand All @@ -95,7 +102,8 @@ public void testDigestAuth() {
@Test
@Tag(TestGroups.FUNCTIONAL)
public void doDelete() {
BaseRequest request = getRequest(new TankHttpClient4(), wireMockServer.baseUrl() + "/delete");
BaseRequest request = getRequest(new TankHttpClient4(), null, wireMockServer.baseUrl() + "/delete");
request.getLogUtil().getAgentConfig().getLogPostResponse();
request.doDelete(null);
BaseResponse response = request.getResponse();
verify(exactly(1), deleteRequestedFor(urlEqualTo("/delete")));
Expand All @@ -106,7 +114,7 @@ public void doDelete() {
@Test
@Tag(TestGroups.FUNCTIONAL)
public void doGet() {
BaseRequest request = getRequest(new TankHttpClient4(), wireMockServer.baseUrl() + "/get");
BaseRequest request = getRequest(new TankHttpClient4(), null, wireMockServer.baseUrl() + "/get");
request.doGet(null);
BaseResponse response = request.getResponse();
verify(exactly(1), getRequestedFor(urlEqualTo("/get")));
Expand All @@ -115,10 +123,45 @@ public void doGet() {
assertNotNull(response.getBody());
}

@Test
@Tag(TestGroups.FUNCTIONAL)
public void doGetException() throws IOException {
CloseableHttpClient mockHttpClient = Mockito.mock(CloseableHttpClient.class);
when(mockHttpClient
.execute(ArgumentMatchers.any(HttpRequestBase.class), ArgumentMatchers.any(HttpClientContext.class)))
.thenThrow(new UnknownHostException("Mocked UnknownHost exception"));
BaseRequest request = getRequest(new TankHttpClient4(), mockHttpClient, wireMockServer.baseUrl() + "/get");
request.doGet(null);
assertNull(request.getResponse());

when(mockHttpClient
.execute(ArgumentMatchers.any(HttpRequestBase.class), ArgumentMatchers.any(HttpClientContext.class)))
.thenThrow(new SocketException("Mocked Socket Exception"));
request.doGet(null);
assertNull(request.getResponse());

when(mockHttpClient
.execute(ArgumentMatchers.any(HttpRequestBase.class), ArgumentMatchers.any(HttpClientContext.class)))
.thenThrow(new RuntimeException("Mocked Exception"));
assertThrows(RuntimeException.class, () -> request.doGet(null));
}

@Test
@Tag(TestGroups.FUNCTIONAL)
public void doOptions() {
BaseRequest request = getRequest(new TankHttpClient4(), null,wireMockServer.baseUrl() + "/options");
request.doOptions(null);
BaseResponse response = request.getResponse();
verify(exactly(1), optionsRequestedFor(urlEqualTo("/options")));
assertNotNull(response);
assertEquals(204, response.getHttpCode());
assertNotNull(response.getBody());
}

@Test
@Tag(TestGroups.FUNCTIONAL)
public void doPost() {
BaseRequest request = getRequest(new TankHttpClient4(), wireMockServer.baseUrl() + "/post");
BaseRequest request = getRequest(new TankHttpClient4(), null,wireMockServer.baseUrl() + "/post");
request.setBody("{\"title\":\"Direct deposit with Credit Karma Money™ checking account¹\"}");
request.setContentType(ContentType.APPLICATION_JSON.getMimeType());
request.doPost(null);
Expand All @@ -131,10 +174,22 @@ public void doPost() {
assertNotNull(response.getBody());
}

@Test
@Tag(TestGroups.FUNCTIONAL)
public void doPatch() {
BaseRequest request = getRequest(new TankHttpClient4(), null,wireMockServer.baseUrl() + "/patch");
request.doPatch(null);
BaseResponse response = request.getResponse();
verify(exactly(1), patchRequestedFor(urlEqualTo("/patch")));
assertNotNull(response);
assertEquals(200, response.getHttpCode());
assertNotNull(response.getBody());
}

@Test
@Tag(TestGroups.FUNCTIONAL)
public void doPut() {
BaseRequest request = getRequest(new TankHttpClient4(), wireMockServer.baseUrl() + "/put");
BaseRequest request = getRequest(new TankHttpClient4(), null,wireMockServer.baseUrl() + "/put");
request.setBody("{\"title\":\"Direct deposit with Credit Karma Money™ checking account¹\"}");
request.setContentType(ContentType.APPLICATION_JSON.getMimeType());
request.doPut(null);
Expand Down Expand Up @@ -165,7 +220,7 @@ public void clearSession() {
.atPriority(2)
);

BaseRequest request = getRequest(new TankHttpClient4(), wireMockServer.baseUrl() + "/cookies");
BaseRequest request = getRequest(new TankHttpClient4(),null, wireMockServer.baseUrl() + "/cookies");
request.getHttpclient().setCookie(TankCookie.builder().withName("test-cookie").withValue("test-value").withDomain("localhost").withPath("/").build());
request.doGet(null);
BaseResponse response = request.getResponse();
Expand All @@ -192,7 +247,7 @@ public void setCookie() {
.atPriority(1)
);

BaseRequest request = getRequest(new TankHttpClient4(), wireMockServer.baseUrl() + "/cookies");
BaseRequest request = getRequest(new TankHttpClient4(), null,wireMockServer.baseUrl() + "/cookies");
request.getHttpclient().setCookie(TankCookie.builder().withName("test-cookie").withValue("test-value").withDomain("localhost").withPath("/").build());
request.doGet(null);
BaseResponse response = request.getResponse();
Expand All @@ -205,8 +260,7 @@ public void setCookie() {
@Disabled
@Tag(TestGroups.FUNCTIONAL)
public void setProxy() {
BaseRequest request = getRequest(new TankHttpClient4(),
"http://httpbin.org/ip");
BaseRequest request = getRequest(new TankHttpClient4(), null, wireMockServer.baseUrl() + "/get");
request.getHttpclient().setProxy("168.9.128.152", 8080);
request.doGet(null);
BaseResponse response = request.getResponse();
Expand All @@ -232,7 +286,7 @@ public void setProxy() {
@Test
@Tag(TestGroups.MANUAL)
public void testSSL() {
BaseRequest request = getRequest(new TankHttpClient4(), "https://turbotax.intuit.com/");
BaseRequest request = getRequest(new TankHttpClient4(), null,"https://turbotax.intuit.com/");
request.doGet(null);
BaseResponse response = request.getResponse();
assertNotNull(response);
Expand All @@ -243,7 +297,7 @@ public void testSSL() {
@Disabled
@Tag(TestGroups.FUNCTIONAL)
public void doPostMultipart() throws IOException {
BaseRequest request = getRequest(new TankHttpClient4(), "http://httpbin.org/post");
BaseRequest request = getRequest(new TankHttpClient4(), null, "http://httpbin.org/post");
request.setContentType(BaseRequest.CONTENT_TYPE_MULTIPART);
request.setBody(createMultiPartBody());
request.doPost(null);
Expand All @@ -256,7 +310,7 @@ public void doPostMultipart() throws IOException {
@Test
@Tag(TestGroups.FUNCTIONAL)
public void doPostMultipartwithFile() throws IOException {
BaseRequest request = getRequest(new TankHttpClient4(), wireMockServer.baseUrl() + "/post");
BaseRequest request = getRequest(new TankHttpClient4(), null, wireMockServer.baseUrl() + "/post");
request.setContentType(BaseRequest.CONTENT_TYPE_MULTIPART);
request.setBody(
"LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0xNzI2MTE1MzQ5Mjk4MjYNCkNvbnRlbnQtRGlzcG9zaXRpb246IGZvcm0tZGF0YTsgbmFtZT0iY3JlYXRlTmV3"
Expand Down Expand Up @@ -324,18 +378,13 @@ private String createMultiPartBody() throws IOException {
HttpEntity entity = MultipartEntityBuilder.create().addTextBody("textPart", "<xml>here is sample xml</xml>", ContentType.APPLICATION_XML).build();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
entity.writeTo(byteArrayOutputStream);
String ret = new String(byteArrayOutputStream.toByteArray());

System.out.println(ret);
ret = toBase64(byteArrayOutputStream.toByteArray());
System.out.println(ret);
return ret;
return Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray()).trim();
}

private BaseRequest getRequest(TankHttpClient client, String url) {
private BaseRequest getRequest(TankHttpClient client, CloseableHttpClient httpclient, String url) {
try {
URL u = new URL(url);
client.setHttpClient(null);
client.setHttpClient(httpclient);
BaseRequest request = new MockBaseRequest(client);
request.setHost(u.getHost());
request.setPath(u.getPath());
Expand All @@ -347,19 +396,4 @@ private BaseRequest getRequest(TankHttpClient client, String url) {
throw new RuntimeException(e);
}
}

/**
* Returns a string's base64 encoding
*
* @param bytes
* @return base64 string
*/
public String toBase64(byte[] bytes) {
try {
return Base64.getEncoder().encodeToString(bytes).trim();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}
Loading