diff --git a/core/src/main/java/com/wechat/pay/java/core/http/ApacheHttpClientBuilder.java b/core/src/main/java/com/wechat/pay/java/core/http/ApacheHttpClientBuilder.java index f8ab5508..50782093 100644 --- a/core/src/main/java/com/wechat/pay/java/core/http/ApacheHttpClientBuilder.java +++ b/core/src/main/java/com/wechat/pay/java/core/http/ApacheHttpClientBuilder.java @@ -1,41 +1,28 @@ package com.wechat.pay.java.core.http; -import java.net.Proxy; - import static java.util.Objects.requireNonNull; -import java.util.concurrent.TimeUnit; - -import org.apache.http.HttpHost; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; - import com.wechat.pay.java.core.Config; import com.wechat.pay.java.core.auth.Credential; import com.wechat.pay.java.core.auth.Validator; import com.wechat.pay.java.core.http.apache.ApacheHttpClientAdapter; -import com.wechat.pay.java.core.http.okhttp.OkHttpClientAdapter; import com.wechat.pay.java.core.http.okhttp.OkHttpMultiDomainInterceptor; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; -/** - * 默认HttpClient构造器 - */ -public class ApacheHttpClientBuilder - implements AbstractHttpClientBuilder { +/** 默认HttpClient构造器 */ +public class ApacheHttpClientBuilder implements AbstractHttpClientBuilder { private Credential credential; private Validator validator; - private CloseableHttpClient customizeApacheHttpClient; private static final OkHttpMultiDomainInterceptor multiDomainInterceptor = new OkHttpMultiDomainInterceptor(); static PoolingHttpClientConnectionManager apacheHttpClientConnectionManager = - new PoolingHttpClientConnectionManager(); - + new PoolingHttpClientConnectionManager(); private CloseableHttpClient initDefaultApacheHttpClient() { return HttpClientBuilder.create() @@ -111,11 +98,12 @@ public AbstractHttpClient build() { requireNonNull(validator); CloseableHttpClient httpclient = - customizeApacheHttpClient == null ? initDefaultApacheHttpClient() + customizeApacheHttpClient == null + ? initDefaultApacheHttpClient() : customizeApacheHttpClient; // 每次都会创建一个 httpclient 实例,和 okhttp 不同 // You can customize a shared OkH(ttpClient instance with newBuilder(). This builds a client - // that shares the same connection po)ol, thread pools, and configuration. + // that shares the same connection po)ol, thread pools, and configuration. return new ApacheHttpClientAdapter(credential, validator, httpclient); } } diff --git a/core/src/main/java/com/wechat/pay/java/core/http/apache/ApacheHttpClientAdapter.java b/core/src/main/java/com/wechat/pay/java/core/http/apache/ApacheHttpClientAdapter.java index c01d78d9..bb21ba09 100644 --- a/core/src/main/java/com/wechat/pay/java/core/http/apache/ApacheHttpClientAdapter.java +++ b/core/src/main/java/com/wechat/pay/java/core/http/apache/ApacheHttpClientAdapter.java @@ -3,11 +3,20 @@ import static java.util.Objects.requireNonNull; import static org.apache.http.entity.ContentType.APPLICATION_JSON; +import com.wechat.pay.java.core.auth.Credential; +import com.wechat.pay.java.core.auth.Validator; +import com.wechat.pay.java.core.exception.HttpException; +import com.wechat.pay.java.core.exception.MalformedMessageException; +import com.wechat.pay.java.core.exception.ServiceException; +import com.wechat.pay.java.core.http.AbstractHttpClient; +import com.wechat.pay.java.core.http.FileRequestBody; +import com.wechat.pay.java.core.http.HttpRequest; +import com.wechat.pay.java.core.http.JsonRequestBody; +import com.wechat.pay.java.core.http.OriginalResponse; import java.io.IOException; import java.io.InputStream; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; - import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; @@ -25,17 +34,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.wechat.pay.java.core.auth.Credential; -import com.wechat.pay.java.core.auth.Validator; -import com.wechat.pay.java.core.exception.HttpException; -import com.wechat.pay.java.core.exception.MalformedMessageException; -import com.wechat.pay.java.core.exception.ServiceException; -import com.wechat.pay.java.core.http.AbstractHttpClient; -import com.wechat.pay.java.core.http.FileRequestBody; -import com.wechat.pay.java.core.http.HttpRequest; -import com.wechat.pay.java.core.http.JsonRequestBody; -import com.wechat.pay.java.core.http.OriginalResponse; - public class ApacheHttpClientAdapter extends AbstractHttpClient { private static final Logger logger = LoggerFactory.getLogger(ApacheHttpClientAdapter.class); @@ -44,8 +42,8 @@ public class ApacheHttpClientAdapter extends AbstractHttpClient { private final CloseableHttpClient apacheHttpClient; - public ApacheHttpClientAdapter(Credential credential, Validator validator, - CloseableHttpClient client) { + public ApacheHttpClientAdapter( + Credential credential, Validator validator, CloseableHttpClient client) { super(credential, validator); this.apacheHttpClient = requireNonNull(client); } @@ -58,8 +56,8 @@ protected String getHttpClientInfo() { @Override public OriginalResponse innerExecute(HttpRequest wechatPayRequest) { try { - CloseableHttpResponse apacheHttpResponse = apacheHttpClient.execute( - buildApacheHttpRequest(wechatPayRequest)); + CloseableHttpResponse apacheHttpResponse = + apacheHttpClient.execute(buildApacheHttpRequest(wechatPayRequest)); return assembleOriginalResponse(wechatPayRequest, apacheHttpResponse); } catch (IOException e) { throw new HttpException(wechatPayRequest, e); @@ -72,27 +70,28 @@ private org.apache.http.client.methods.HttpUriRequest buildApacheHttpRequest( org.apache.http.client.methods.HttpUriRequest apacheHttpRequest; switch (wechatPayRequest.getHttpMethod().name()) { - case "GET": - apacheHttpRequest = new HttpGet(url); - break; - case "POST": - apacheHttpRequest = new HttpPost(url); - ((HttpPost) apacheHttpRequest).setEntity(buildApacheHttpEntity(wechatPayRequest.getBody())); - break; - case "PUT": - apacheHttpRequest = new HttpPut(url); - ((HttpPut) apacheHttpRequest).setEntity(buildApacheHttpEntity(wechatPayRequest.getBody())); - break; - case "PATCH": - apacheHttpRequest = new HttpPatch(url); - ((HttpPatch) apacheHttpRequest).setEntity(buildApacheHttpEntity(wechatPayRequest.getBody())); - break; - case "DELETE": - apacheHttpRequest = new HttpDelete(url); - break; - default: - throw new IllegalArgumentException( - "Unsupported HTTP method: " + wechatPayRequest.getHttpMethod().name()); + case "GET": + apacheHttpRequest = new HttpGet(url); + break; + case "POST": + apacheHttpRequest = new HttpPost(url); + ((HttpPost) apacheHttpRequest).setEntity(buildApacheHttpEntity(wechatPayRequest.getBody())); + break; + case "PUT": + apacheHttpRequest = new HttpPut(url); + ((HttpPut) apacheHttpRequest).setEntity(buildApacheHttpEntity(wechatPayRequest.getBody())); + break; + case "PATCH": + apacheHttpRequest = new HttpPatch(url); + ((HttpPatch) apacheHttpRequest) + .setEntity(buildApacheHttpEntity(wechatPayRequest.getBody())); + break; + case "DELETE": + apacheHttpRequest = new HttpDelete(url); + break; + default: + throw new IllegalArgumentException( + "Unsupported HTTP method: " + wechatPayRequest.getHttpMethod().name()); } Map headers = wechatPayRequest.getHeaders().getHeaders(); headers.forEach(apacheHttpRequest::addHeader); @@ -116,20 +115,23 @@ private HttpEntity buildApacheHttpEntity( MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); entityBuilder.setMode(HttpMultipartMode.RFC6532); entityBuilder.addTextBody(META_NAME, fileRequestBody.getMeta(), APPLICATION_JSON); - entityBuilder.addBinaryBody(FILE_NAME, fileRequestBody.getFile(), - ContentType.create(fileRequestBody.getContentType()), fileRequestBody.getFileName()); + entityBuilder.addBinaryBody( + FILE_NAME, + fileRequestBody.getFile(), + ContentType.create(fileRequestBody.getContentType()), + fileRequestBody.getFileName()); return entityBuilder.build(); } logger.error( - "When an http request is sent and the apache request body is constructed, the requestBody" - + " parameter" + "When an http request is sent and the apache request body is constructed, the requestBody" + + " parameter" + " type cannot be found,requestBody class name[{}]", wechatPayRequestBody.getClass().getName()); return null; } - private OriginalResponse assembleOriginalResponse(HttpRequest wechatPayRequest, - CloseableHttpResponse apacheHttpResponse) throws IOException { + private OriginalResponse assembleOriginalResponse( + HttpRequest wechatPayRequest, CloseableHttpResponse apacheHttpResponse) throws IOException { Map responseHeaders = assembleResponseHeader(apacheHttpResponse); HttpEntity entity = apacheHttpResponse.getEntity(); try { @@ -161,11 +163,11 @@ private Map assembleResponseHeader(CloseableHttpResponse apacheH @Override protected InputStream innerDownload(HttpRequest httpRequest) { try { - CloseableHttpResponse apacheHttpResponse = apacheHttpClient.execute( - buildApacheHttpRequest(httpRequest)); + CloseableHttpResponse apacheHttpResponse = + apacheHttpClient.execute(buildApacheHttpRequest(httpRequest)); if (isInvalidHttpCode(apacheHttpResponse.getStatusLine().getStatusCode())) { - throw new ServiceException(httpRequest, apacheHttpResponse.getStatusLine().getStatusCode(), - ""); + throw new ServiceException( + httpRequest, apacheHttpResponse.getStatusLine().getStatusCode(), ""); } InputStream responseBodyStream = null; if (apacheHttpResponse.getEntity() != null) { diff --git a/core/src/test/java/com/wechat/pay/java/core/http/ApacheHttpClientAdapterTest.java b/core/src/test/java/com/wechat/pay/java/core/http/ApacheHttpClientAdapterTest.java index d0fe7cff..f201f6fb 100644 --- a/core/src/test/java/com/wechat/pay/java/core/http/ApacheHttpClientAdapterTest.java +++ b/core/src/test/java/com/wechat/pay/java/core/http/ApacheHttpClientAdapterTest.java @@ -1,8 +1,22 @@ package com.wechat.pay.java.core.http; +import static com.wechat.pay.java.core.http.Constant.AUTHORIZATION; +import static com.wechat.pay.java.core.http.Constant.OS; +import static com.wechat.pay.java.core.http.Constant.USER_AGENT_FORMAT; +import static com.wechat.pay.java.core.http.Constant.VERSION; +import static com.wechat.pay.java.core.model.TestConfig.MERCHANT_ID; +import static com.wechat.pay.java.core.model.TestConfig.RESOURCES_DIR; + +import com.wechat.pay.java.core.auth.Credential; +import com.wechat.pay.java.core.auth.Validator; +import com.wechat.pay.java.core.http.okhttp.OkHttpClientAdapter; +import com.wechat.pay.java.core.model.TestServiceResponse; +import com.wechat.pay.java.core.util.IOUtil; import java.io.IOException; +import java.io.InputStream; import java.net.URI; - +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpEntityEnclosingRequest; @@ -13,34 +27,17 @@ import org.apache.http.ProtocolVersion; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; +import org.apache.http.entity.mime.HttpMultipartMode; +import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicStatusLine; import org.apache.http.protocol.HttpContext; -import org.apache.http.entity.mime.HttpMultipartMode; -import org.apache.http.entity.mime.MultipartEntityBuilder; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -import com.wechat.pay.java.core.auth.Credential; -import com.wechat.pay.java.core.auth.Validator; -import static com.wechat.pay.java.core.http.Constant.AUTHORIZATION; -import static com.wechat.pay.java.core.http.Constant.OS; -import static com.wechat.pay.java.core.http.Constant.USER_AGENT_FORMAT; -import static com.wechat.pay.java.core.http.Constant.VERSION; -import com.wechat.pay.java.core.http.okhttp.OkHttpClientAdapter; -import static com.wechat.pay.java.core.model.TestConfig.MERCHANT_ID; -import static com.wechat.pay.java.core.model.TestConfig.RESOURCES_DIR; -import com.wechat.pay.java.core.model.TestServiceResponse; - -import java.nio.file.Files; -import java.nio.file.Paths; -import java.io.InputStream; -import com.wechat.pay.java.core.util.IOUtil; - - public class ApacheHttpClientAdapterTest { private static final String APACHE_HTTP_CLIENT_MOCK_DATA_DIR = RESOURCES_DIR + "/http"; @@ -119,9 +116,11 @@ public String getSerialNumber() { executeSendGetCredential.getClass().getSimpleName(), executeSendGetRequestValidator.getClass().getSimpleName(), "apachehttp/" + getClass().getPackage().getImplementationVersion()); - HttpRequestInterceptor requestInterceptor = new HttpRequestInterceptor() { - @Override - public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException { + HttpRequestInterceptor requestInterceptor = + new HttpRequestInterceptor() { + @Override + public void process(org.apache.http.HttpRequest request, HttpContext context) + throws HttpException, IOException { String executeSendGetMethod = request.getRequestLine().getMethod(); Assert.assertEquals(executeSendGetMethod, HttpMethod.GET.name()); String host = context.getAttribute("http.target_host").toString(); @@ -131,36 +130,43 @@ public void process(org.apache.http.HttpRequest request, HttpContext context) th Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); Assert.assertEquals(FAKE_AUTHORIZATION, getHeaderValue(headers, AUTHORIZATION)); Assert.assertEquals( - executeSendGetCredential.getAuthorization(URI.create(URL), executeSendGetMethod, ""), + executeSendGetCredential.getAuthorization( + URI.create(URL), executeSendGetMethod, ""), getHeaderValue(headers, AUTHORIZATION)); - } + } - private String getHeaderValue(Header[] headers, String name) { + private String getHeaderValue(Header[] headers, String name) { for (Header header : headers) { - if (header.getName().equalsIgnoreCase(name)) { - return header.getValue(); - } + if (header.getName().equalsIgnoreCase(name)) { + return header.getValue(); + } } return null; - } - }; + } + }; - HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() { - @Override - public void process(org.apache.http.HttpResponse response, HttpContext context) throws HttpException, IOException { + HttpResponseInterceptor responseInterceptor = + new HttpResponseInterceptor() { + @Override + public void process(org.apache.http.HttpResponse response, HttpContext context) + throws HttpException, IOException { Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); - response.setHeaders(new Header[]{header}); + response.setHeaders(new Header[] {header}); ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); int statusCode = HttpStatus.SC_OK; String reasonPhrase = "OK"; - BasicStatusLine statusLine = new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); + BasicStatusLine statusLine = + new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); response.setStatusLine(statusLine); response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); - } - }; + } + }; CloseableHttpClient httpclient = - HttpClients.custom().addInterceptorFirst(requestInterceptor).addInterceptorFirst(responseInterceptor).build(); + HttpClients.custom() + .addInterceptorFirst(requestInterceptor) + .addInterceptorFirst(responseInterceptor) + .build(); HttpClient executeSendGetHttpClient = new ApacheHttpClientBuilder() .credential(executeSendGetCredential) @@ -191,1347 +197,1415 @@ public void process(org.apache.http.HttpResponse response, HttpContext context) Assert.assertTrue(executeSendGetResponse.getBody() instanceof JsonResponseBody); Assert.assertEquals( RESPONSE_JSON, ((JsonResponseBody) executeSendGetResponse.getBody()).getBody()); - Assert.assertEquals(RESPONSE_JSON_VALUE, executeSendGetResponse.getServiceResponse().getRequestId()); + Assert.assertEquals( + RESPONSE_JSON_VALUE, executeSendGetResponse.getServiceResponse().getRequestId()); } - @Test - public void testExecuteSendPostReqWithJsonBody() throws IOException { - Credential executeSendPostCredential = new Credential() { - @Override - public String getSchema() { - return "fake-schema"; - } + @Test + public void testExecuteSendPostReqWithJsonBody() throws IOException { + Credential executeSendPostCredential = + new Credential() { + @Override + public String getSchema() { + return "fake-schema"; + } - @Override - public String getMerchantId() { - return MERCHANT_ID; - } + @Override + public String getMerchantId() { + return MERCHANT_ID; + } - @Override - public String getAuthorization(URI uri, String httpMethod, String signBody) { - Assert.assertEquals(URI.create(URL), uri); - Assert.assertEquals(HttpMethod.POST.name(), httpMethod); - Assert.assertEquals(JSON_REQUEST_BODY.getBody(), signBody); - return FAKE_AUTHORIZATION; - } + @Override + public String getAuthorization(URI uri, String httpMethod, String signBody) { + Assert.assertEquals(URI.create(URL), uri); + Assert.assertEquals(HttpMethod.POST.name(), httpMethod); + Assert.assertEquals(JSON_REQUEST_BODY.getBody(), signBody); + return FAKE_AUTHORIZATION; + } }; - Validator executeSendPostValidator = new Validator() { - @Override - public boolean validate(HttpHeaders responseHeaders, String body) { - Assert.assertNotNull(responseHeaders); - Assert.assertEquals(1, responseHeaders.getHeaders().size()); - Assert.assertEquals(RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); - Assert.assertEquals(RESPONSE_JSON, body); - return true; - } + Validator executeSendPostValidator = + new Validator() { + @Override + public boolean validate(HttpHeaders responseHeaders, String body) { + Assert.assertNotNull(responseHeaders); + Assert.assertEquals(1, responseHeaders.getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); + Assert.assertEquals(RESPONSE_JSON, body); + return true; + } - @Override - public String getSerialNumber() { - return ""; - } + @Override + public String getSerialNumber() { + return ""; + } }; - HttpRequestInterceptor interceptor = new HttpRequestInterceptor() { - @Override - public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException { - Assert.assertEquals(HttpMethod.POST.name(), request.getRequestLine().getMethod()); - String host = context.getAttribute("http.target_host").toString(); - Assert.assertEquals(URL, host + request.getRequestLine().getUri()); - Header[] headers = request.getAllHeaders(); - Assert.assertEquals(4, headers.length); - Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); - Assert.assertEquals( - executeSendPostCredential.getAuthorization( - URI.create(URL), - request.getRequestLine().getMethod(), - JSON_REQUEST_BODY.getBody()), - getHeaderValue(headers, AUTHORIZATION)); - - Assert.assertTrue(request instanceof HttpEntityEnclosingRequest); - HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; - HttpEntity reqEntity = entityRequest.getEntity(); - HttpEntity entity = new StringEntity( + HttpRequestInterceptor interceptor = + new HttpRequestInterceptor() { + @Override + public void process(org.apache.http.HttpRequest request, HttpContext context) + throws HttpException, IOException { + Assert.assertEquals(HttpMethod.POST.name(), request.getRequestLine().getMethod()); + String host = context.getAttribute("http.target_host").toString(); + Assert.assertEquals(URL, host + request.getRequestLine().getUri()); + Header[] headers = request.getAllHeaders(); + Assert.assertEquals(4, headers.length); + Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); + Assert.assertEquals( + executeSendPostCredential.getAuthorization( + URI.create(URL), + request.getRequestLine().getMethod(), + JSON_REQUEST_BODY.getBody()), + getHeaderValue(headers, AUTHORIZATION)); + + Assert.assertTrue(request instanceof HttpEntityEnclosingRequest); + HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; + HttpEntity reqEntity = entityRequest.getEntity(); + HttpEntity entity = + new StringEntity( JSON_REQUEST_BODY.getBody(), ContentType.create(JSON_REQUEST_BODY.getContentType())); - Assert.assertEquals(entity.getContentType().getValue(), reqEntity.getContentType().getValue()); - Assert.assertEquals(entity.getContentLength(), reqEntity.getContentLength()); - } + Assert.assertEquals( + entity.getContentType().getValue(), reqEntity.getContentType().getValue()); + Assert.assertEquals(entity.getContentLength(), reqEntity.getContentLength()); + } - private String getHeaderValue(Header[] headers, String name) { - for (Header header : headers) { - if (header.getName().equalsIgnoreCase(name)) { - return header.getValue(); - } - } - return null; + private String getHeaderValue(Header[] headers, String name) { + for (Header header : headers) { + if (header.getName().equalsIgnoreCase(name)) { + return header.getValue(); + } } + return null; + } }; - HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() { - @Override - public void process(org.apache.http.HttpResponse response, HttpContext context) throws HttpException, IOException { - Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); - response.setHeaders(new Header[]{header}); - ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); - int statusCode = HttpStatus.SC_OK; - String reasonPhrase = "OK"; - BasicStatusLine statusLine = new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); - response.setStatusLine(statusLine); - response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); - } + HttpResponseInterceptor responseInterceptor = + new HttpResponseInterceptor() { + @Override + public void process(org.apache.http.HttpResponse response, HttpContext context) + throws HttpException, IOException { + Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); + response.setHeaders(new Header[] {header}); + ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); + int statusCode = HttpStatus.SC_OK; + String reasonPhrase = "OK"; + BasicStatusLine statusLine = + new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); + response.setStatusLine(statusLine); + response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); + } }; - CloseableHttpClient httpClient = HttpClients.custom(). - addInterceptorFirst(interceptor). - addInterceptorLast(responseInterceptor).build(); + CloseableHttpClient httpClient = + HttpClients.custom() + .addInterceptorFirst(interceptor) + .addInterceptorLast(responseInterceptor) + .build(); - HttpClient executeSendPostHttpClient = new ApacheHttpClientBuilder() + HttpClient executeSendPostHttpClient = + new ApacheHttpClientBuilder() .credential(executeSendPostCredential) .validator(executeSendPostValidator) .apacheHttpClient(httpClient) .build(); + HttpRequest executeSendPostHttpRequest = + new HttpRequest.Builder() + .httpMethod(HttpMethod.POST) + .url(URL) + .headers(requestHeaders) + .body(JSON_REQUEST_BODY) + .build(); - HttpRequest executeSendPostHttpRequest = - new HttpRequest.Builder() - .httpMethod(HttpMethod.POST) - .url(URL) - .headers(requestHeaders) - .body(JSON_REQUEST_BODY) - .build(); - - - HttpResponse executeSendPostResponse = + HttpResponse executeSendPostResponse = executeSendPostHttpClient.execute(executeSendPostHttpRequest, TestServiceResponse.class); - Assert.assertEquals(4, executeSendPostResponse.getRequest().getHeaders().getHeaders().size()); - Assert.assertEquals( - REQUEST_HEADER_VALUE, - executeSendPostResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); - Assert.assertEquals( - FAKE_AUTHORIZATION, - executeSendPostResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); - Assert.assertEquals(1, executeSendPostResponse.getHeaders().getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, executeSendPostResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); - Assert.assertTrue(executeSendPostResponse.getBody() instanceof JsonResponseBody); - Assert.assertEquals( - RESPONSE_JSON, ((JsonResponseBody) executeSendPostResponse.getBody()).getBody()); - Assert.assertEquals(RESPONSE_JSON_VALUE, executeSendPostResponse.getServiceResponse().getRequestId()); - } - - @Test - public void testExecuteSendPostReqWithFileBody() throws IOException { - String imageName = "test_image.jpg"; - String imagePath = APACHE_HTTP_CLIENT_MOCK_DATA_DIR + "/" + imageName; - InputStream inputStream = Files.newInputStream(Paths.get(imagePath)); - FileRequestBody requestBody = + Assert.assertEquals(4, executeSendPostResponse.getRequest().getHeaders().getHeaders().size()); + Assert.assertEquals( + REQUEST_HEADER_VALUE, + executeSendPostResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); + Assert.assertEquals( + FAKE_AUTHORIZATION, + executeSendPostResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); + Assert.assertEquals(1, executeSendPostResponse.getHeaders().getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, executeSendPostResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); + Assert.assertTrue(executeSendPostResponse.getBody() instanceof JsonResponseBody); + Assert.assertEquals( + RESPONSE_JSON, ((JsonResponseBody) executeSendPostResponse.getBody()).getBody()); + Assert.assertEquals( + RESPONSE_JSON_VALUE, executeSendPostResponse.getServiceResponse().getRequestId()); + } + + @Test + public void testExecuteSendPostReqWithFileBody() throws IOException { + String imageName = "test_image.jpg"; + String imagePath = APACHE_HTTP_CLIENT_MOCK_DATA_DIR + "/" + imageName; + InputStream inputStream = Files.newInputStream(Paths.get(imagePath)); + FileRequestBody requestBody = new FileRequestBody.Builder() .meta("meta") .fileName(imageName) .file(IOUtil.toByteArray(inputStream)) .build(); - MultipartEntityBuilder builder = MultipartEntityBuilder.create(); - builder.setMode(HttpMultipartMode.RFC6532); - builder.addTextBody("meta", "meta"); - builder.addBinaryBody("file", requestBody.getFile(), ContentType.create(requestBody.getContentType()), imageName); - HttpEntity multipartEntity = builder.build(); - Credential executeSendPostWithFileCredential = - new Credential() { - @Override - public String getSchema() { - return "fake-schema"; - } + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.RFC6532); + builder.addTextBody("meta", "meta"); + builder.addBinaryBody( + "file", requestBody.getFile(), ContentType.create(requestBody.getContentType()), imageName); + HttpEntity multipartEntity = builder.build(); + Credential executeSendPostWithFileCredential = + new Credential() { + @Override + public String getSchema() { + return "fake-schema"; + } - @Override - public String getMerchantId() { - return MERCHANT_ID; - } + @Override + public String getMerchantId() { + return MERCHANT_ID; + } - @Override - public String getAuthorization(URI uri, String httpMethod, String signBody) { - Assert.assertEquals(URI.create(URL), uri); - Assert.assertEquals(HttpMethod.POST.name(), httpMethod); - System.out.println("signBody=" + signBody); - Assert.assertEquals("meta", signBody); - return FAKE_AUTHORIZATION; - } - }; - Validator executeSendPostWithFileValidator = - new Validator() { - @Override - public boolean validate(HttpHeaders responseHeaders, String body) { - Assert.assertNotNull(responseHeaders); - Assert.assertEquals(1, responseHeaders.getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); - Assert.assertEquals(RESPONSE_JSON, body); - return true; - } - - @Override - public String getSerialNumber() { - return ""; - } - }; - String userAgent = - String.format( - USER_AGENT_FORMAT, - OkHttpClientAdapter.class.getPackage().getImplementationVersion(), - OS, - VERSION == null ? "Unknown" : VERSION, - executeSendPostWithFileCredential.getClass().getSimpleName(), - executeSendPostWithFileValidator.getClass().getSimpleName(), - "apachehttp/" + getClass().getPackage().getImplementationVersion()); - HttpRequestInterceptor interceptor = new HttpRequestInterceptor() { - @Override - public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException { - Assert.assertEquals(HttpMethod.POST.name(), request.getRequestLine().getMethod()); - String host = context.getAttribute("http.target_host").toString(); - Assert.assertEquals(URL, host + request.getRequestLine().getUri()); - Header[] headers = request.getAllHeaders(); - Assert.assertEquals(4, headers.length); - Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); - Assert.assertEquals( - executeSendPostWithFileCredential.getAuthorization( - URI.create(URL), - request.getRequestLine().getMethod(), - "meta"), - getHeaderValue(headers, AUTHORIZATION)); - - Assert.assertTrue(request instanceof HttpEntityEnclosingRequest); - HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; - HttpEntity reqEntity = entityRequest.getEntity(); - // apache multipart entity content length 每次创建不一样,不做校验 - // Assert.assertEquals(multipartEntity.getContentLength(), reqEntity.getContentLength()); - } + @Override + public String getAuthorization(URI uri, String httpMethod, String signBody) { + Assert.assertEquals(URI.create(URL), uri); + Assert.assertEquals(HttpMethod.POST.name(), httpMethod); + System.out.println("signBody=" + signBody); + Assert.assertEquals("meta", signBody); + return FAKE_AUTHORIZATION; + } + }; + Validator executeSendPostWithFileValidator = + new Validator() { + @Override + public boolean validate(HttpHeaders responseHeaders, String body) { + Assert.assertNotNull(responseHeaders); + Assert.assertEquals(1, responseHeaders.getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); + Assert.assertEquals(RESPONSE_JSON, body); + return true; + } - private String getHeaderValue(Header[] headers, String name) { - for (Header header : headers) { - if (header.getName().equalsIgnoreCase(name)) { - return header.getValue(); - } - } - return null; - } + @Override + public String getSerialNumber() { + return ""; + } }; + String userAgent = + String.format( + USER_AGENT_FORMAT, + OkHttpClientAdapter.class.getPackage().getImplementationVersion(), + OS, + VERSION == null ? "Unknown" : VERSION, + executeSendPostWithFileCredential.getClass().getSimpleName(), + executeSendPostWithFileValidator.getClass().getSimpleName(), + "apachehttp/" + getClass().getPackage().getImplementationVersion()); + HttpRequestInterceptor interceptor = + new HttpRequestInterceptor() { + @Override + public void process(org.apache.http.HttpRequest request, HttpContext context) + throws HttpException, IOException { + Assert.assertEquals(HttpMethod.POST.name(), request.getRequestLine().getMethod()); + String host = context.getAttribute("http.target_host").toString(); + Assert.assertEquals(URL, host + request.getRequestLine().getUri()); + Header[] headers = request.getAllHeaders(); + Assert.assertEquals(4, headers.length); + Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); + Assert.assertEquals( + executeSendPostWithFileCredential.getAuthorization( + URI.create(URL), request.getRequestLine().getMethod(), "meta"), + getHeaderValue(headers, AUTHORIZATION)); + + Assert.assertTrue(request instanceof HttpEntityEnclosingRequest); + HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; + HttpEntity reqEntity = entityRequest.getEntity(); + // apache multipart entity content length 每次创建不一样,不做校验 + // Assert.assertEquals(multipartEntity.getContentLength(), + // reqEntity.getContentLength()); + } - HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() { - @Override - public void process(org.apache.http.HttpResponse response, HttpContext context) throws HttpException, IOException { - System.out.println("process response: " + response); - Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); - response.setHeaders(new Header[]{header}); - - ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); - int statusCode = HttpStatus.SC_OK; - String reasonPhrase = "OK"; - BasicStatusLine statusLine = new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); - response.setStatusLine(statusLine); - response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); + private String getHeaderValue(Header[] headers, String name) { + for (Header header : headers) { + if (header.getName().equalsIgnoreCase(name)) { + return header.getValue(); + } } + return null; + } + }; + + HttpResponseInterceptor responseInterceptor = + new HttpResponseInterceptor() { + @Override + public void process(org.apache.http.HttpResponse response, HttpContext context) + throws HttpException, IOException { + System.out.println("process response: " + response); + Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); + response.setHeaders(new Header[] {header}); + + ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); + int statusCode = HttpStatus.SC_OK; + String reasonPhrase = "OK"; + BasicStatusLine statusLine = + new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); + response.setStatusLine(statusLine); + response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); + } }; - CloseableHttpClient httpClient = HttpClients.custom(). - addInterceptorFirst(interceptor). - addInterceptorLast(responseInterceptor).build(); + CloseableHttpClient httpClient = + HttpClients.custom() + .addInterceptorFirst(interceptor) + .addInterceptorLast(responseInterceptor) + .build(); - HttpClient executeSendPostWithFileHttpClient = new ApacheHttpClientBuilder() + HttpClient executeSendPostWithFileHttpClient = + new ApacheHttpClientBuilder() .credential(executeSendPostWithFileCredential) .validator(executeSendPostWithFileValidator) .apacheHttpClient(httpClient) .build(); + HttpRequest executeSendPostHttpRequest = + new HttpRequest.Builder() + .httpMethod(HttpMethod.POST) + .url(URL) + .headers(requestHeaders) + .body(JSON_REQUEST_BODY) + .build(); - HttpRequest executeSendPostHttpRequest = - new HttpRequest.Builder() - .httpMethod(HttpMethod.POST) - .url(URL) - .headers(requestHeaders) - .body(JSON_REQUEST_BODY) - .build(); - - HttpResponse executeSendPostWithFileResponse = - executeSendPostWithFileHttpClient.post( + HttpResponse executeSendPostWithFileResponse = + executeSendPostWithFileHttpClient.post( requestHeaders, URL, requestBody, TestServiceResponse.class); + Assert.assertEquals( + 4, executeSendPostWithFileResponse.getRequest().getHeaders().getHeaders().size()); + Assert.assertEquals( + REQUEST_HEADER_VALUE, + executeSendPostWithFileResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); + Assert.assertEquals( + FAKE_AUTHORIZATION, + executeSendPostWithFileResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); + Assert.assertEquals( + HttpMethod.POST, executeSendPostWithFileResponse.getRequest().getHttpMethod()); + Assert.assertEquals(requestBody, executeSendPostWithFileResponse.getRequest().getBody()); + Assert.assertEquals(URL, executeSendPostWithFileResponse.getRequest().getUrl().toString()); + Assert.assertEquals(1, executeSendPostWithFileResponse.getHeaders().getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, + executeSendPostWithFileResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); + Assert.assertTrue(executeSendPostWithFileResponse.getBody() instanceof JsonResponseBody); + Assert.assertEquals( + RESPONSE_JSON, ((JsonResponseBody) executeSendPostWithFileResponse.getBody()).getBody()); + Assert.assertEquals( + RESPONSE_JSON_VALUE, executeSendPostWithFileResponse.getServiceResponse().getRequestId()); + } - Assert.assertEquals( - 4, executeSendPostWithFileResponse.getRequest().getHeaders().getHeaders().size()); - Assert.assertEquals( - REQUEST_HEADER_VALUE, - executeSendPostWithFileResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); - Assert.assertEquals( - FAKE_AUTHORIZATION, - executeSendPostWithFileResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); - Assert.assertEquals( - HttpMethod.POST, executeSendPostWithFileResponse.getRequest().getHttpMethod()); - Assert.assertEquals(requestBody, executeSendPostWithFileResponse.getRequest().getBody()); - Assert.assertEquals(URL, executeSendPostWithFileResponse.getRequest().getUrl().toString()); - Assert.assertEquals(1, executeSendPostWithFileResponse.getHeaders().getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, - executeSendPostWithFileResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); - Assert.assertTrue(executeSendPostWithFileResponse.getBody() instanceof JsonResponseBody); - Assert.assertEquals( - RESPONSE_JSON, ((JsonResponseBody) executeSendPostWithFileResponse.getBody()).getBody()); - Assert.assertEquals(RESPONSE_JSON_VALUE, executeSendPostWithFileResponse.getServiceResponse().getRequestId()); - } - - @Test - public void testExecuteSendPutRequest() throws IOException { - Credential executeSendPutCredential = - new Credential() { - @Override - public String getSchema() { - return "fake-schema"; - } - - @Override - public String getMerchantId() { - return MERCHANT_ID; - } + @Test + public void testExecuteSendPutRequest() throws IOException { + Credential executeSendPutCredential = + new Credential() { + @Override + public String getSchema() { + return "fake-schema"; + } - @Override - public String getAuthorization(URI uri, String httpMethod, String signBody) { - Assert.assertEquals(URI.create(URL), uri); - Assert.assertEquals(HttpMethod.PUT.name(), httpMethod); - Assert.assertEquals(JSON_REQUEST_BODY.getBody(), signBody); - return FAKE_AUTHORIZATION; - } - }; - Validator executeSendPutValidator = - new Validator() { - @Override - public boolean validate(HttpHeaders responseHeaders, String body) { - Assert.assertNotNull(responseHeaders); - Assert.assertEquals(1, responseHeaders.getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); - Assert.assertEquals(RESPONSE_JSON, body); - return true; - } - - @Override - public String getSerialNumber() { - return ""; - } - }; - String userAgent = - String.format( - USER_AGENT_FORMAT, - OkHttpClientAdapter.class.getPackage().getImplementationVersion(), - OS, - VERSION == null ? "Unknown" : VERSION, - executeSendPutCredential.getClass().getSimpleName(), - executeSendPutValidator.getClass().getSimpleName(), - "apachehttp/" + getClass().getPackage().getImplementationVersion()); - HttpRequestInterceptor interceptor = new HttpRequestInterceptor() { - @Override - public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException { - Assert.assertEquals(HttpMethod.PUT.name(), request.getRequestLine().getMethod()); - String host = context.getAttribute("http.target_host").toString(); - Assert.assertEquals(URL, host + request.getRequestLine().getUri()); - Header[] headers = request.getAllHeaders(); - Assert.assertEquals(4, headers.length); - Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); - Assert.assertEquals( - executeSendPutCredential.getAuthorization( - URI.create(URL), - request.getRequestLine().getMethod(), - JSON_REQUEST_BODY.getBody()), - getHeaderValue(headers, AUTHORIZATION)); - - Assert.assertTrue(request instanceof HttpEntityEnclosingRequest); - HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; - HttpEntity reqEntity = entityRequest.getEntity(); - HttpEntity entity = new StringEntity( - JSON_REQUEST_BODY.getBody(), - ContentType.create(JSON_REQUEST_BODY.getContentType())); - Assert.assertEquals(entity.getContentLength(), reqEntity.getContentLength()); - } + @Override + public String getMerchantId() { + return MERCHANT_ID; + } - private String getHeaderValue(Header[] headers, String name) { - for (Header header : headers) { - if (header.getName().equalsIgnoreCase(name)) { - return header.getValue(); - } - } - return null; - } + @Override + public String getAuthorization(URI uri, String httpMethod, String signBody) { + Assert.assertEquals(URI.create(URL), uri); + Assert.assertEquals(HttpMethod.PUT.name(), httpMethod); + Assert.assertEquals(JSON_REQUEST_BODY.getBody(), signBody); + return FAKE_AUTHORIZATION; + } }; + Validator executeSendPutValidator = + new Validator() { + @Override + public boolean validate(HttpHeaders responseHeaders, String body) { + Assert.assertNotNull(responseHeaders); + Assert.assertEquals(1, responseHeaders.getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); + Assert.assertEquals(RESPONSE_JSON, body); + return true; + } - HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() { - @Override - public void process(org.apache.http.HttpResponse response, HttpContext context) throws HttpException, IOException { - Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); - response.setHeaders(new Header[]{header}); - - ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); - int statusCode = HttpStatus.SC_OK; - String reasonPhrase = "OK"; - BasicStatusLine statusLine = new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); - response.setStatusLine(statusLine); - response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); - } + @Override + public String getSerialNumber() { + return ""; + } }; + String userAgent = + String.format( + USER_AGENT_FORMAT, + OkHttpClientAdapter.class.getPackage().getImplementationVersion(), + OS, + VERSION == null ? "Unknown" : VERSION, + executeSendPutCredential.getClass().getSimpleName(), + executeSendPutValidator.getClass().getSimpleName(), + "apachehttp/" + getClass().getPackage().getImplementationVersion()); + HttpRequestInterceptor interceptor = + new HttpRequestInterceptor() { + @Override + public void process(org.apache.http.HttpRequest request, HttpContext context) + throws HttpException, IOException { + Assert.assertEquals(HttpMethod.PUT.name(), request.getRequestLine().getMethod()); + String host = context.getAttribute("http.target_host").toString(); + Assert.assertEquals(URL, host + request.getRequestLine().getUri()); + Header[] headers = request.getAllHeaders(); + Assert.assertEquals(4, headers.length); + Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); + Assert.assertEquals( + executeSendPutCredential.getAuthorization( + URI.create(URL), + request.getRequestLine().getMethod(), + JSON_REQUEST_BODY.getBody()), + getHeaderValue(headers, AUTHORIZATION)); - CloseableHttpClient httpClient = HttpClients.custom(). - addInterceptorFirst(interceptor). - addInterceptorLast(responseInterceptor).build(); - - HttpClient executeSendPutHttpClient = new ApacheHttpClientBuilder() + Assert.assertTrue(request instanceof HttpEntityEnclosingRequest); + HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; + HttpEntity reqEntity = entityRequest.getEntity(); + HttpEntity entity = + new StringEntity( + JSON_REQUEST_BODY.getBody(), + ContentType.create(JSON_REQUEST_BODY.getContentType())); + Assert.assertEquals(entity.getContentLength(), reqEntity.getContentLength()); + } + + private String getHeaderValue(Header[] headers, String name) { + for (Header header : headers) { + if (header.getName().equalsIgnoreCase(name)) { + return header.getValue(); + } + } + return null; + } + }; + + HttpResponseInterceptor responseInterceptor = + new HttpResponseInterceptor() { + @Override + public void process(org.apache.http.HttpResponse response, HttpContext context) + throws HttpException, IOException { + Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); + response.setHeaders(new Header[] {header}); + + ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); + int statusCode = HttpStatus.SC_OK; + String reasonPhrase = "OK"; + BasicStatusLine statusLine = + new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); + response.setStatusLine(statusLine); + response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); + } + }; + + CloseableHttpClient httpClient = + HttpClients.custom() + .addInterceptorFirst(interceptor) + .addInterceptorLast(responseInterceptor) + .build(); + + HttpClient executeSendPutHttpClient = + new ApacheHttpClientBuilder() .credential(executeSendPutCredential) .validator(executeSendPutValidator) .apacheHttpClient(httpClient) .build(); + HttpRequest httpRequest = + new HttpRequest.Builder() + .httpMethod(HttpMethod.PUT) + .url(URL) + .headers(requestHeaders) + .body(JSON_REQUEST_BODY) + .build(); - HttpRequest httpRequest = - new HttpRequest.Builder() - .httpMethod(HttpMethod.PUT) - .url(URL) - .headers(requestHeaders) - .body(JSON_REQUEST_BODY) - .build(); - - HttpResponse executeSendPutResponse = - executeSendPutHttpClient.execute(httpRequest, TestServiceResponse.class); - - - Assert.assertEquals( - 4, executeSendPutResponse.getRequest().getHeaders().getHeaders().size()); - Assert.assertEquals( - REQUEST_HEADER_VALUE, - executeSendPutResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); - Assert.assertEquals( - FAKE_AUTHORIZATION, - executeSendPutResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); - Assert.assertEquals( - HttpMethod.PUT, executeSendPutResponse.getRequest().getHttpMethod()); - Assert.assertEquals(URL, executeSendPutResponse.getRequest().getUrl().toString()); - Assert.assertEquals(1, executeSendPutResponse.getHeaders().getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, - executeSendPutResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); - Assert.assertTrue(executeSendPutResponse.getBody() instanceof JsonResponseBody); - Assert.assertEquals( - RESPONSE_JSON, ((JsonResponseBody) executeSendPutResponse.getBody()).getBody()); - Assert.assertEquals(RESPONSE_JSON_VALUE, executeSendPutResponse.getServiceResponse().getRequestId()); - } - - @Test - public void testExecuteSendPatchRequest() throws IOException { - Credential executeSendPatchCredential = - new Credential() { - @Override - public String getSchema() { - return "fake-schema"; - } + HttpResponse executeSendPutResponse = + executeSendPutHttpClient.execute(httpRequest, TestServiceResponse.class); - @Override - public String getMerchantId() { - return MERCHANT_ID; - } + Assert.assertEquals(4, executeSendPutResponse.getRequest().getHeaders().getHeaders().size()); + Assert.assertEquals( + REQUEST_HEADER_VALUE, + executeSendPutResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); + Assert.assertEquals( + FAKE_AUTHORIZATION, + executeSendPutResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); + Assert.assertEquals(HttpMethod.PUT, executeSendPutResponse.getRequest().getHttpMethod()); + Assert.assertEquals(URL, executeSendPutResponse.getRequest().getUrl().toString()); + Assert.assertEquals(1, executeSendPutResponse.getHeaders().getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, executeSendPutResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); + Assert.assertTrue(executeSendPutResponse.getBody() instanceof JsonResponseBody); + Assert.assertEquals( + RESPONSE_JSON, ((JsonResponseBody) executeSendPutResponse.getBody()).getBody()); + Assert.assertEquals( + RESPONSE_JSON_VALUE, executeSendPutResponse.getServiceResponse().getRequestId()); + } - @Override - public String getAuthorization(URI uri, String httpMethod, String signBody) { - Assert.assertEquals(URI.create(URL), uri); - Assert.assertEquals(HttpMethod.PATCH.name(), httpMethod); - Assert.assertEquals(JSON_REQUEST_BODY.getBody(), signBody); - return FAKE_AUTHORIZATION; - } - }; - Validator executeSendPatchValidator = - new Validator() { - @Override - public boolean validate(HttpHeaders responseHeaders, String body) { - Assert.assertNotNull(responseHeaders); - Assert.assertEquals(1, responseHeaders.getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); - Assert.assertEquals(RESPONSE_JSON, body); - return true; - } - - @Override - public String getSerialNumber() { - return ""; - } - }; - String userAgent = - String.format( - USER_AGENT_FORMAT, - OkHttpClientAdapter.class.getPackage().getImplementationVersion(), - OS, - VERSION == null ? "Unknown" : VERSION, - executeSendPatchCredential.getClass().getSimpleName(), - executeSendPatchValidator.getClass().getSimpleName(), - "apachehttp/" + getClass().getPackage().getImplementationVersion()); - HttpRequestInterceptor interceptor = new HttpRequestInterceptor() { - @Override - public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException { - Assert.assertEquals(HttpMethod.PATCH.name(), request.getRequestLine().getMethod()); - String host = context.getAttribute("http.target_host").toString(); - Assert.assertEquals(URL, host + request.getRequestLine().getUri()); - Header[] headers = request.getAllHeaders(); - Assert.assertEquals(4, headers.length); - Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); - Assert.assertEquals( - executeSendPatchCredential.getAuthorization( - URI.create(URL), - request.getRequestLine().getMethod(), - JSON_REQUEST_BODY.getBody()), - getHeaderValue(headers, AUTHORIZATION)); - - - Assert.assertTrue(request instanceof HttpEntityEnclosingRequest); - HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; - HttpEntity reqEntity = entityRequest.getEntity(); - HttpEntity entity = new StringEntity( + @Test + public void testExecuteSendPatchRequest() throws IOException { + Credential executeSendPatchCredential = + new Credential() { + @Override + public String getSchema() { + return "fake-schema"; + } + + @Override + public String getMerchantId() { + return MERCHANT_ID; + } + + @Override + public String getAuthorization(URI uri, String httpMethod, String signBody) { + Assert.assertEquals(URI.create(URL), uri); + Assert.assertEquals(HttpMethod.PATCH.name(), httpMethod); + Assert.assertEquals(JSON_REQUEST_BODY.getBody(), signBody); + return FAKE_AUTHORIZATION; + } + }; + Validator executeSendPatchValidator = + new Validator() { + @Override + public boolean validate(HttpHeaders responseHeaders, String body) { + Assert.assertNotNull(responseHeaders); + Assert.assertEquals(1, responseHeaders.getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); + Assert.assertEquals(RESPONSE_JSON, body); + return true; + } + + @Override + public String getSerialNumber() { + return ""; + } + }; + String userAgent = + String.format( + USER_AGENT_FORMAT, + OkHttpClientAdapter.class.getPackage().getImplementationVersion(), + OS, + VERSION == null ? "Unknown" : VERSION, + executeSendPatchCredential.getClass().getSimpleName(), + executeSendPatchValidator.getClass().getSimpleName(), + "apachehttp/" + getClass().getPackage().getImplementationVersion()); + HttpRequestInterceptor interceptor = + new HttpRequestInterceptor() { + @Override + public void process(org.apache.http.HttpRequest request, HttpContext context) + throws HttpException, IOException { + Assert.assertEquals(HttpMethod.PATCH.name(), request.getRequestLine().getMethod()); + String host = context.getAttribute("http.target_host").toString(); + Assert.assertEquals(URL, host + request.getRequestLine().getUri()); + Header[] headers = request.getAllHeaders(); + Assert.assertEquals(4, headers.length); + Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); + Assert.assertEquals( + executeSendPatchCredential.getAuthorization( + URI.create(URL), + request.getRequestLine().getMethod(), + JSON_REQUEST_BODY.getBody()), + getHeaderValue(headers, AUTHORIZATION)); + + Assert.assertTrue(request instanceof HttpEntityEnclosingRequest); + HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; + HttpEntity reqEntity = entityRequest.getEntity(); + HttpEntity entity = + new StringEntity( JSON_REQUEST_BODY.getBody(), ContentType.create(JSON_REQUEST_BODY.getContentType())); - Assert.assertEquals(entity.getContentLength(), reqEntity.getContentLength()); - } + Assert.assertEquals(entity.getContentLength(), reqEntity.getContentLength()); + } - private String getHeaderValue(Header[] headers, String name) { - for (Header header : headers) { - if (header.getName().equalsIgnoreCase(name)) { - return header.getValue(); - } - } - return null; + private String getHeaderValue(Header[] headers, String name) { + for (Header header : headers) { + if (header.getName().equalsIgnoreCase(name)) { + return header.getValue(); + } } + return null; + } }; - HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() { - @Override - public void process(org.apache.http.HttpResponse response, HttpContext context) throws HttpException, IOException { - Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); - response.setHeaders(new Header[]{header}); - - ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); - int statusCode = HttpStatus.SC_OK; - String reasonPhrase = "OK"; - BasicStatusLine statusLine = new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); - response.setStatusLine(statusLine); - response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); - } + HttpResponseInterceptor responseInterceptor = + new HttpResponseInterceptor() { + @Override + public void process(org.apache.http.HttpResponse response, HttpContext context) + throws HttpException, IOException { + Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); + response.setHeaders(new Header[] {header}); + + ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); + int statusCode = HttpStatus.SC_OK; + String reasonPhrase = "OK"; + BasicStatusLine statusLine = + new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); + response.setStatusLine(statusLine); + response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); + } }; - CloseableHttpClient httpClient = HttpClients.custom(). - addInterceptorFirst(interceptor). - addInterceptorLast(responseInterceptor).build(); + CloseableHttpClient httpClient = + HttpClients.custom() + .addInterceptorFirst(interceptor) + .addInterceptorLast(responseInterceptor) + .build(); - HttpClient executeSendPutHttpClient = new ApacheHttpClientBuilder() + HttpClient executeSendPutHttpClient = + new ApacheHttpClientBuilder() .credential(executeSendPatchCredential) .validator(executeSendPatchValidator) .apacheHttpClient(httpClient) .build(); + HttpRequest httpRequest = + new HttpRequest.Builder() + .httpMethod(HttpMethod.PATCH) + .url(URL) + .headers(requestHeaders) + .body(JSON_REQUEST_BODY) + .build(); - HttpRequest httpRequest = - new HttpRequest.Builder() - .httpMethod(HttpMethod.PATCH) - .url(URL) - .headers(requestHeaders) - .body(JSON_REQUEST_BODY) - .build(); - - HttpResponse executeSendPutResponse = - executeSendPutHttpClient.execute(httpRequest, TestServiceResponse.class); - - - Assert.assertEquals( - 4, executeSendPutResponse.getRequest().getHeaders().getHeaders().size()); - Assert.assertEquals( - REQUEST_HEADER_VALUE, - executeSendPutResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); - Assert.assertEquals( - FAKE_AUTHORIZATION, - executeSendPutResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); - Assert.assertEquals( - HttpMethod.PATCH, executeSendPutResponse.getRequest().getHttpMethod()); - Assert.assertEquals(URL, executeSendPutResponse.getRequest().getUrl().toString()); - Assert.assertEquals(1, executeSendPutResponse.getHeaders().getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, - executeSendPutResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); - Assert.assertTrue(executeSendPutResponse.getBody() instanceof JsonResponseBody); - Assert.assertEquals( - RESPONSE_JSON, ((JsonResponseBody) executeSendPutResponse.getBody()).getBody()); - Assert.assertEquals(RESPONSE_JSON_VALUE, executeSendPutResponse.getServiceResponse().getRequestId()); - } - - @Test - public void testExecuteSendDeleteRequest() throws IOException { - Credential executeSendDeleteCredential = - new Credential() { - @Override - public String getSchema() { - return "fake-schema"; - } + HttpResponse executeSendPutResponse = + executeSendPutHttpClient.execute(httpRequest, TestServiceResponse.class); - @Override - public String getMerchantId() { - return MERCHANT_ID; - } + Assert.assertEquals(4, executeSendPutResponse.getRequest().getHeaders().getHeaders().size()); + Assert.assertEquals( + REQUEST_HEADER_VALUE, + executeSendPutResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); + Assert.assertEquals( + FAKE_AUTHORIZATION, + executeSendPutResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); + Assert.assertEquals(HttpMethod.PATCH, executeSendPutResponse.getRequest().getHttpMethod()); + Assert.assertEquals(URL, executeSendPutResponse.getRequest().getUrl().toString()); + Assert.assertEquals(1, executeSendPutResponse.getHeaders().getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, executeSendPutResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); + Assert.assertTrue(executeSendPutResponse.getBody() instanceof JsonResponseBody); + Assert.assertEquals( + RESPONSE_JSON, ((JsonResponseBody) executeSendPutResponse.getBody()).getBody()); + Assert.assertEquals( + RESPONSE_JSON_VALUE, executeSendPutResponse.getServiceResponse().getRequestId()); + } - @Override - public String getAuthorization(URI uri, String httpMethod, String signBody) { - Assert.assertEquals(URI.create(URL), uri); - Assert.assertEquals(HttpMethod.DELETE.name(), httpMethod); - Assert.assertEquals(JSON_REQUEST_BODY.getBody(), signBody); - return FAKE_AUTHORIZATION; - } - }; - Validator executeSendDeleteValidator = - new Validator() { - @Override - public boolean validate(HttpHeaders responseHeaders, String body) { - Assert.assertNotNull(responseHeaders); - Assert.assertEquals(1, responseHeaders.getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); - Assert.assertEquals(RESPONSE_JSON, body); - return true; - } - - @Override - public String getSerialNumber() { - return ""; - } - }; - String userAgent = - String.format( - USER_AGENT_FORMAT, - OkHttpClientAdapter.class.getPackage().getImplementationVersion(), - OS, - VERSION == null ? "Unknown" : VERSION, - executeSendDeleteCredential.getClass().getSimpleName(), - executeSendDeleteValidator.getClass().getSimpleName(), - "apachehttp/" + getClass().getPackage().getImplementationVersion()); - HttpRequestInterceptor interceptor = new HttpRequestInterceptor() { - @Override - public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException { - Assert.assertEquals(HttpMethod.DELETE.name(), request.getRequestLine().getMethod()); - String host = context.getAttribute("http.target_host").toString(); - Assert.assertEquals(URL, host + request.getRequestLine().getUri()); - Header[] headers = request.getAllHeaders(); - Assert.assertEquals(4, headers.length); - Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); - Assert.assertEquals( - executeSendDeleteCredential.getAuthorization( - URI.create(URL), - request.getRequestLine().getMethod(), - JSON_REQUEST_BODY.getBody()), - getHeaderValue(headers, AUTHORIZATION)); - } + @Test + public void testExecuteSendDeleteRequest() throws IOException { + Credential executeSendDeleteCredential = + new Credential() { + @Override + public String getSchema() { + return "fake-schema"; + } - private String getHeaderValue(Header[] headers, String name) { - for (Header header : headers) { - if (header.getName().equalsIgnoreCase(name)) { - return header.getValue(); - } - } - return null; - } + @Override + public String getMerchantId() { + return MERCHANT_ID; + } + + @Override + public String getAuthorization(URI uri, String httpMethod, String signBody) { + Assert.assertEquals(URI.create(URL), uri); + Assert.assertEquals(HttpMethod.DELETE.name(), httpMethod); + Assert.assertEquals(JSON_REQUEST_BODY.getBody(), signBody); + return FAKE_AUTHORIZATION; + } + }; + Validator executeSendDeleteValidator = + new Validator() { + @Override + public boolean validate(HttpHeaders responseHeaders, String body) { + Assert.assertNotNull(responseHeaders); + Assert.assertEquals(1, responseHeaders.getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); + Assert.assertEquals(RESPONSE_JSON, body); + return true; + } + + @Override + public String getSerialNumber() { + return ""; + } }; + String userAgent = + String.format( + USER_AGENT_FORMAT, + OkHttpClientAdapter.class.getPackage().getImplementationVersion(), + OS, + VERSION == null ? "Unknown" : VERSION, + executeSendDeleteCredential.getClass().getSimpleName(), + executeSendDeleteValidator.getClass().getSimpleName(), + "apachehttp/" + getClass().getPackage().getImplementationVersion()); + HttpRequestInterceptor interceptor = + new HttpRequestInterceptor() { + @Override + public void process(org.apache.http.HttpRequest request, HttpContext context) + throws HttpException, IOException { + Assert.assertEquals(HttpMethod.DELETE.name(), request.getRequestLine().getMethod()); + String host = context.getAttribute("http.target_host").toString(); + Assert.assertEquals(URL, host + request.getRequestLine().getUri()); + Header[] headers = request.getAllHeaders(); + Assert.assertEquals(4, headers.length); + Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); + Assert.assertEquals( + executeSendDeleteCredential.getAuthorization( + URI.create(URL), + request.getRequestLine().getMethod(), + JSON_REQUEST_BODY.getBody()), + getHeaderValue(headers, AUTHORIZATION)); + } - HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() { - @Override - public void process(org.apache.http.HttpResponse response, HttpContext context) throws HttpException, IOException { - Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); - response.setHeaders(new Header[]{header}); - - ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); - int statusCode = HttpStatus.SC_OK; - String reasonPhrase = "OK"; - BasicStatusLine statusLine = new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); - response.setStatusLine(statusLine); - response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); + private String getHeaderValue(Header[] headers, String name) { + for (Header header : headers) { + if (header.getName().equalsIgnoreCase(name)) { + return header.getValue(); + } } + return null; + } + }; + + HttpResponseInterceptor responseInterceptor = + new HttpResponseInterceptor() { + @Override + public void process(org.apache.http.HttpResponse response, HttpContext context) + throws HttpException, IOException { + Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); + response.setHeaders(new Header[] {header}); + + ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); + int statusCode = HttpStatus.SC_OK; + String reasonPhrase = "OK"; + BasicStatusLine statusLine = + new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); + response.setStatusLine(statusLine); + response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); + } }; - CloseableHttpClient httpClient = HttpClients.custom(). - addInterceptorFirst(interceptor). - addInterceptorLast(responseInterceptor).build(); + CloseableHttpClient httpClient = + HttpClients.custom() + .addInterceptorFirst(interceptor) + .addInterceptorLast(responseInterceptor) + .build(); - HttpClient executeSendPutHttpClient = new ApacheHttpClientBuilder() + HttpClient executeSendPutHttpClient = + new ApacheHttpClientBuilder() .credential(executeSendDeleteCredential) .validator(executeSendDeleteValidator) .apacheHttpClient(httpClient) .build(); + HttpRequest httpRequest = + new HttpRequest.Builder() + .httpMethod(HttpMethod.DELETE) + .url(URL) + .headers(requestHeaders) + .body(JSON_REQUEST_BODY) + .build(); - HttpRequest httpRequest = - new HttpRequest.Builder() - .httpMethod(HttpMethod.DELETE) - .url(URL) - .headers(requestHeaders) - .body(JSON_REQUEST_BODY) - .build(); - - HttpResponse executeSendPutResponse = - executeSendPutHttpClient.execute(httpRequest, TestServiceResponse.class); - - - Assert.assertEquals( - 4, executeSendPutResponse.getRequest().getHeaders().getHeaders().size()); - Assert.assertEquals( - REQUEST_HEADER_VALUE, - executeSendPutResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); - Assert.assertEquals( - FAKE_AUTHORIZATION, - executeSendPutResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); - Assert.assertEquals( - HttpMethod.DELETE, executeSendPutResponse.getRequest().getHttpMethod()); - Assert.assertEquals(URL, executeSendPutResponse.getRequest().getUrl().toString()); - Assert.assertEquals(1, executeSendPutResponse.getHeaders().getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, - executeSendPutResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); - Assert.assertTrue(executeSendPutResponse.getBody() instanceof JsonResponseBody); - Assert.assertEquals( - RESPONSE_JSON, ((JsonResponseBody) executeSendPutResponse.getBody()).getBody()); - Assert.assertEquals(RESPONSE_JSON_VALUE, executeSendPutResponse.getServiceResponse().getRequestId()); - } - - - @Test - public void testGet() { - Credential getCredential = - new Credential() { - @Override - public String getSchema() { - return "fake-schema"; - } + HttpResponse executeSendPutResponse = + executeSendPutHttpClient.execute(httpRequest, TestServiceResponse.class); - @Override - public String getMerchantId() { - return MERCHANT_ID; - } + Assert.assertEquals(4, executeSendPutResponse.getRequest().getHeaders().getHeaders().size()); + Assert.assertEquals( + REQUEST_HEADER_VALUE, + executeSendPutResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); + Assert.assertEquals( + FAKE_AUTHORIZATION, + executeSendPutResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); + Assert.assertEquals(HttpMethod.DELETE, executeSendPutResponse.getRequest().getHttpMethod()); + Assert.assertEquals(URL, executeSendPutResponse.getRequest().getUrl().toString()); + Assert.assertEquals(1, executeSendPutResponse.getHeaders().getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, executeSendPutResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); + Assert.assertTrue(executeSendPutResponse.getBody() instanceof JsonResponseBody); + Assert.assertEquals( + RESPONSE_JSON, ((JsonResponseBody) executeSendPutResponse.getBody()).getBody()); + Assert.assertEquals( + RESPONSE_JSON_VALUE, executeSendPutResponse.getServiceResponse().getRequestId()); + } - @Override - public String getAuthorization(URI uri, String httpMethod, String signBody) { - Assert.assertEquals(URI.create(URL), uri); - Assert.assertEquals(HttpMethod.GET.name(), httpMethod); - Assert.assertEquals("", signBody); - return FAKE_AUTHORIZATION; - } - }; - Validator getHttpValidator = - new Validator() { - @Override - public boolean validate(HttpHeaders responseHeaders, String body) { - Assert.assertNotNull(responseHeaders); - Assert.assertEquals(1, responseHeaders.getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); - Assert.assertEquals(RESPONSE_JSON, body); - return true; - } - - @Override - public String getSerialNumber() { - return ""; - } - }; - String userAgent = - String.format( - USER_AGENT_FORMAT, - OkHttpClientAdapter.class.getPackage().getImplementationVersion(), - OS, - VERSION == null ? "Unknown" : VERSION, - getCredential.getClass().getSimpleName(), - getHttpValidator.getClass().getSimpleName(), - "apachehttp/" + getClass().getPackage().getImplementationVersion()); - HttpRequestInterceptor requestInterceptor = new HttpRequestInterceptor() { - @Override - public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException { - String getMethod = request.getRequestLine().getMethod(); - Assert.assertEquals(getMethod, HttpMethod.GET.name()); - String host = context.getAttribute("http.target_host").toString(); - Assert.assertEquals(URL, host + request.getRequestLine().getUri()); - Header[] headers = request.getAllHeaders(); - Assert.assertEquals(4, headers.length); - Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); - Assert.assertEquals(FAKE_AUTHORIZATION, getHeaderValue(headers, AUTHORIZATION)); - Assert.assertEquals( - getCredential.getAuthorization(URI.create(URL), getMethod, ""), - getHeaderValue(headers, AUTHORIZATION)); - } + @Test + public void testGet() { + Credential getCredential = + new Credential() { + @Override + public String getSchema() { + return "fake-schema"; + } - private String getHeaderValue(Header[] headers, String name) { - for (Header header : headers) { - if (header.getName().equalsIgnoreCase(name)) { - return header.getValue(); - } - } - return null; - } + @Override + public String getMerchantId() { + return MERCHANT_ID; + } + + @Override + public String getAuthorization(URI uri, String httpMethod, String signBody) { + Assert.assertEquals(URI.create(URL), uri); + Assert.assertEquals(HttpMethod.GET.name(), httpMethod); + Assert.assertEquals("", signBody); + return FAKE_AUTHORIZATION; + } }; + Validator getHttpValidator = + new Validator() { + @Override + public boolean validate(HttpHeaders responseHeaders, String body) { + Assert.assertNotNull(responseHeaders); + Assert.assertEquals(1, responseHeaders.getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); + Assert.assertEquals(RESPONSE_JSON, body); + return true; + } - HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() { - @Override - public void process(org.apache.http.HttpResponse response, HttpContext context) throws HttpException, IOException { - Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); - response.setHeaders(new Header[]{header}); - - ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); - int statusCode = HttpStatus.SC_OK; - String reasonPhrase = "OK"; - BasicStatusLine statusLine = new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); - response.setStatusLine(statusLine); - response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); - } + @Override + public String getSerialNumber() { + return ""; + } }; - CloseableHttpClient httpclient = - HttpClients.custom().addInterceptorFirst(requestInterceptor).addInterceptorFirst(responseInterceptor).build(); - - HttpClient getHttpClient = - new ApacheHttpClientBuilder() - .credential(getCredential) - .validator(getHttpValidator) - .apacheHttpClient(httpclient) - .build(); - - com.wechat.pay.java.core.http.HttpResponse executeSendGetResponse = - getHttpClient.get(requestHeaders, URL, TestServiceResponse.class); - - Assert.assertEquals(4, executeSendGetResponse.getRequest().getHeaders().getHeaders().size()); - Assert.assertEquals( - REQUEST_HEADER_VALUE, - executeSendGetResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); - Assert.assertEquals( - FAKE_AUTHORIZATION, - executeSendGetResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); - Assert.assertEquals(1, executeSendGetResponse.getHeaders().getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, executeSendGetResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); - Assert.assertTrue(executeSendGetResponse.getBody() instanceof JsonResponseBody); - Assert.assertEquals( - RESPONSE_JSON, ((JsonResponseBody) executeSendGetResponse.getBody()).getBody()); - Assert.assertEquals(RESPONSE_JSON_VALUE, executeSendGetResponse.getServiceResponse().getRequestId()); - } - - @Test - public void testPost() throws IOException { - Credential postCredential = new Credential() { - @Override - public String getSchema() { - return "fake-schema"; - } + String userAgent = + String.format( + USER_AGENT_FORMAT, + OkHttpClientAdapter.class.getPackage().getImplementationVersion(), + OS, + VERSION == null ? "Unknown" : VERSION, + getCredential.getClass().getSimpleName(), + getHttpValidator.getClass().getSimpleName(), + "apachehttp/" + getClass().getPackage().getImplementationVersion()); + HttpRequestInterceptor requestInterceptor = + new HttpRequestInterceptor() { + @Override + public void process(org.apache.http.HttpRequest request, HttpContext context) + throws HttpException, IOException { + String getMethod = request.getRequestLine().getMethod(); + Assert.assertEquals(getMethod, HttpMethod.GET.name()); + String host = context.getAttribute("http.target_host").toString(); + Assert.assertEquals(URL, host + request.getRequestLine().getUri()); + Header[] headers = request.getAllHeaders(); + Assert.assertEquals(4, headers.length); + Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); + Assert.assertEquals(FAKE_AUTHORIZATION, getHeaderValue(headers, AUTHORIZATION)); + Assert.assertEquals( + getCredential.getAuthorization(URI.create(URL), getMethod, ""), + getHeaderValue(headers, AUTHORIZATION)); + } - @Override - public String getMerchantId() { - return MERCHANT_ID; + private String getHeaderValue(Header[] headers, String name) { + for (Header header : headers) { + if (header.getName().equalsIgnoreCase(name)) { + return header.getValue(); + } } + return null; + } + }; - @Override - public String getAuthorization(URI uri, String httpMethod, String signBody) { - Assert.assertEquals(URI.create(URL), uri); - Assert.assertEquals(HttpMethod.POST.name(), httpMethod); - Assert.assertEquals(JSON_REQUEST_BODY.getBody(), signBody); - return FAKE_AUTHORIZATION; - } + HttpResponseInterceptor responseInterceptor = + new HttpResponseInterceptor() { + @Override + public void process(org.apache.http.HttpResponse response, HttpContext context) + throws HttpException, IOException { + Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); + response.setHeaders(new Header[] {header}); + + ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); + int statusCode = HttpStatus.SC_OK; + String reasonPhrase = "OK"; + BasicStatusLine statusLine = + new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); + response.setStatusLine(statusLine); + response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); + } }; - Validator postValidator = new Validator() { - @Override - public boolean validate(HttpHeaders responseHeaders, String body) { - Assert.assertNotNull(responseHeaders); - Assert.assertEquals(1, responseHeaders.getHeaders().size()); - Assert.assertEquals(RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); - Assert.assertEquals(RESPONSE_JSON, body); - return true; - } + CloseableHttpClient httpclient = + HttpClients.custom() + .addInterceptorFirst(requestInterceptor) + .addInterceptorFirst(responseInterceptor) + .build(); - @Override - public String getSerialNumber() { - return ""; - } + HttpClient getHttpClient = + new ApacheHttpClientBuilder() + .credential(getCredential) + .validator(getHttpValidator) + .apacheHttpClient(httpclient) + .build(); + + com.wechat.pay.java.core.http.HttpResponse executeSendGetResponse = + getHttpClient.get(requestHeaders, URL, TestServiceResponse.class); + + Assert.assertEquals(4, executeSendGetResponse.getRequest().getHeaders().getHeaders().size()); + Assert.assertEquals( + REQUEST_HEADER_VALUE, + executeSendGetResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); + Assert.assertEquals( + FAKE_AUTHORIZATION, + executeSendGetResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); + Assert.assertEquals(1, executeSendGetResponse.getHeaders().getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, executeSendGetResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); + Assert.assertTrue(executeSendGetResponse.getBody() instanceof JsonResponseBody); + Assert.assertEquals( + RESPONSE_JSON, ((JsonResponseBody) executeSendGetResponse.getBody()).getBody()); + Assert.assertEquals( + RESPONSE_JSON_VALUE, executeSendGetResponse.getServiceResponse().getRequestId()); + } + + @Test + public void testPost() throws IOException { + Credential postCredential = + new Credential() { + @Override + public String getSchema() { + return "fake-schema"; + } + + @Override + public String getMerchantId() { + return MERCHANT_ID; + } + + @Override + public String getAuthorization(URI uri, String httpMethod, String signBody) { + Assert.assertEquals(URI.create(URL), uri); + Assert.assertEquals(HttpMethod.POST.name(), httpMethod); + Assert.assertEquals(JSON_REQUEST_BODY.getBody(), signBody); + return FAKE_AUTHORIZATION; + } + }; + Validator postValidator = + new Validator() { + @Override + public boolean validate(HttpHeaders responseHeaders, String body) { + Assert.assertNotNull(responseHeaders); + Assert.assertEquals(1, responseHeaders.getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); + Assert.assertEquals(RESPONSE_JSON, body); + return true; + } + + @Override + public String getSerialNumber() { + return ""; + } }; - HttpRequestInterceptor interceptor = new HttpRequestInterceptor() { - @Override - public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException { - Assert.assertEquals(HttpMethod.POST.name(), request.getRequestLine().getMethod()); - String host = context.getAttribute("http.target_host").toString(); - Assert.assertEquals(URL, host + request.getRequestLine().getUri()); - Header[] headers = request.getAllHeaders(); - Assert.assertEquals(4, headers.length); - Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); - Assert.assertEquals( - postCredential.getAuthorization( - URI.create(URL), - request.getRequestLine().getMethod(), - JSON_REQUEST_BODY.getBody()), - getHeaderValue(headers, AUTHORIZATION)); - - Assert.assertTrue(request instanceof HttpEntityEnclosingRequest); - HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; - HttpEntity reqEntity = entityRequest.getEntity(); - HttpEntity entity = new StringEntity( + HttpRequestInterceptor interceptor = + new HttpRequestInterceptor() { + @Override + public void process(org.apache.http.HttpRequest request, HttpContext context) + throws HttpException, IOException { + Assert.assertEquals(HttpMethod.POST.name(), request.getRequestLine().getMethod()); + String host = context.getAttribute("http.target_host").toString(); + Assert.assertEquals(URL, host + request.getRequestLine().getUri()); + Header[] headers = request.getAllHeaders(); + Assert.assertEquals(4, headers.length); + Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); + Assert.assertEquals( + postCredential.getAuthorization( + URI.create(URL), + request.getRequestLine().getMethod(), + JSON_REQUEST_BODY.getBody()), + getHeaderValue(headers, AUTHORIZATION)); + + Assert.assertTrue(request instanceof HttpEntityEnclosingRequest); + HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; + HttpEntity reqEntity = entityRequest.getEntity(); + HttpEntity entity = + new StringEntity( JSON_REQUEST_BODY.getBody(), ContentType.create(JSON_REQUEST_BODY.getContentType())); - Assert.assertEquals(entity.getContentType().getValue(), reqEntity.getContentType().getValue()); - Assert.assertEquals(entity.getContentLength(), reqEntity.getContentLength()); - } + Assert.assertEquals( + entity.getContentType().getValue(), reqEntity.getContentType().getValue()); + Assert.assertEquals(entity.getContentLength(), reqEntity.getContentLength()); + } - private String getHeaderValue(Header[] headers, String name) { - for (Header header : headers) { - if (header.getName().equalsIgnoreCase(name)) { - return header.getValue(); - } - } - return null; + private String getHeaderValue(Header[] headers, String name) { + for (Header header : headers) { + if (header.getName().equalsIgnoreCase(name)) { + return header.getValue(); + } } + return null; + } }; - HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() { - @Override - public void process(org.apache.http.HttpResponse response, HttpContext context) throws HttpException, IOException { - Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); - response.setHeaders(new Header[]{header}); - - ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); - int statusCode = HttpStatus.SC_OK; - String reasonPhrase = "OK"; - BasicStatusLine statusLine = new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); - response.setStatusLine(statusLine); - response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); - } + HttpResponseInterceptor responseInterceptor = + new HttpResponseInterceptor() { + @Override + public void process(org.apache.http.HttpResponse response, HttpContext context) + throws HttpException, IOException { + Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); + response.setHeaders(new Header[] {header}); + + ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); + int statusCode = HttpStatus.SC_OK; + String reasonPhrase = "OK"; + BasicStatusLine statusLine = + new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); + response.setStatusLine(statusLine); + response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); + } }; - CloseableHttpClient httpClient = HttpClients.custom(). - addInterceptorFirst(interceptor). - addInterceptorLast(responseInterceptor).build(); + CloseableHttpClient httpClient = + HttpClients.custom() + .addInterceptorFirst(interceptor) + .addInterceptorLast(responseInterceptor) + .build(); - HttpClient postHttpClient = new ApacheHttpClientBuilder() + HttpClient postHttpClient = + new ApacheHttpClientBuilder() .credential(postCredential) .validator(postValidator) .apacheHttpClient(httpClient) .build(); - HttpResponse executeSendPostResponse = - postHttpClient.post(requestHeaders, URL, JSON_REQUEST_BODY, TestServiceResponse.class); - Assert.assertEquals(4, executeSendPostResponse.getRequest().getHeaders().getHeaders().size()); - Assert.assertEquals( - REQUEST_HEADER_VALUE, - executeSendPostResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); - Assert.assertEquals( - FAKE_AUTHORIZATION, - executeSendPostResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); - Assert.assertEquals(1, executeSendPostResponse.getHeaders().getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, executeSendPostResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); - Assert.assertTrue(executeSendPostResponse.getBody() instanceof JsonResponseBody); - Assert.assertEquals( - RESPONSE_JSON, ((JsonResponseBody) executeSendPostResponse.getBody()).getBody()); - Assert.assertEquals(RESPONSE_JSON_VALUE, executeSendPostResponse.getServiceResponse().getRequestId()); - } - - @Test - public void testPatch() throws IOException { - Credential patchCredential = - new Credential() { - @Override - public String getSchema() { - return "fake-schema"; - } + HttpResponse executeSendPostResponse = + postHttpClient.post(requestHeaders, URL, JSON_REQUEST_BODY, TestServiceResponse.class); + Assert.assertEquals(4, executeSendPostResponse.getRequest().getHeaders().getHeaders().size()); + Assert.assertEquals( + REQUEST_HEADER_VALUE, + executeSendPostResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); + Assert.assertEquals( + FAKE_AUTHORIZATION, + executeSendPostResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); + Assert.assertEquals(1, executeSendPostResponse.getHeaders().getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, executeSendPostResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); + Assert.assertTrue(executeSendPostResponse.getBody() instanceof JsonResponseBody); + Assert.assertEquals( + RESPONSE_JSON, ((JsonResponseBody) executeSendPostResponse.getBody()).getBody()); + Assert.assertEquals( + RESPONSE_JSON_VALUE, executeSendPostResponse.getServiceResponse().getRequestId()); + } - @Override - public String getMerchantId() { - return MERCHANT_ID; - } + @Test + public void testPatch() throws IOException { + Credential patchCredential = + new Credential() { + @Override + public String getSchema() { + return "fake-schema"; + } - @Override - public String getAuthorization(URI uri, String httpMethod, String signBody) { - Assert.assertEquals(URI.create(URL), uri); - Assert.assertEquals(HttpMethod.PATCH.name(), httpMethod); - Assert.assertEquals(JSON_REQUEST_BODY.getBody(), signBody); - return FAKE_AUTHORIZATION; - } - }; - Validator patchValidator = - new Validator() { - @Override - public boolean validate(HttpHeaders responseHeaders, String body) { - Assert.assertNotNull(responseHeaders); - Assert.assertEquals(1, responseHeaders.getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); - Assert.assertEquals(RESPONSE_JSON, body); - return true; - } - - @Override - public String getSerialNumber() { - return ""; - } - }; - String userAgent = - String.format( - USER_AGENT_FORMAT, - OkHttpClientAdapter.class.getPackage().getImplementationVersion(), - OS, - VERSION == null ? "Unknown" : VERSION, - patchCredential.getClass().getSimpleName(), - patchValidator.getClass().getSimpleName(), - "apachehttp/" + getClass().getPackage().getImplementationVersion()); - HttpRequestInterceptor interceptor = new HttpRequestInterceptor() { - @Override - public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException { - Assert.assertEquals(HttpMethod.PATCH.name(), request.getRequestLine().getMethod()); - String host = context.getAttribute("http.target_host").toString(); - Assert.assertEquals(URL, host + request.getRequestLine().getUri()); - Header[] headers = request.getAllHeaders(); - Assert.assertEquals(4, headers.length); - Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); - Assert.assertEquals( - patchCredential.getAuthorization( - URI.create(URL), - request.getRequestLine().getMethod(), - JSON_REQUEST_BODY.getBody()), - getHeaderValue(headers, AUTHORIZATION)); - - Assert.assertTrue(request instanceof HttpEntityEnclosingRequest); - HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; - HttpEntity reqEntity = entityRequest.getEntity(); - HttpEntity entity = new StringEntity( + @Override + public String getMerchantId() { + return MERCHANT_ID; + } + + @Override + public String getAuthorization(URI uri, String httpMethod, String signBody) { + Assert.assertEquals(URI.create(URL), uri); + Assert.assertEquals(HttpMethod.PATCH.name(), httpMethod); + Assert.assertEquals(JSON_REQUEST_BODY.getBody(), signBody); + return FAKE_AUTHORIZATION; + } + }; + Validator patchValidator = + new Validator() { + @Override + public boolean validate(HttpHeaders responseHeaders, String body) { + Assert.assertNotNull(responseHeaders); + Assert.assertEquals(1, responseHeaders.getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); + Assert.assertEquals(RESPONSE_JSON, body); + return true; + } + + @Override + public String getSerialNumber() { + return ""; + } + }; + String userAgent = + String.format( + USER_AGENT_FORMAT, + OkHttpClientAdapter.class.getPackage().getImplementationVersion(), + OS, + VERSION == null ? "Unknown" : VERSION, + patchCredential.getClass().getSimpleName(), + patchValidator.getClass().getSimpleName(), + "apachehttp/" + getClass().getPackage().getImplementationVersion()); + HttpRequestInterceptor interceptor = + new HttpRequestInterceptor() { + @Override + public void process(org.apache.http.HttpRequest request, HttpContext context) + throws HttpException, IOException { + Assert.assertEquals(HttpMethod.PATCH.name(), request.getRequestLine().getMethod()); + String host = context.getAttribute("http.target_host").toString(); + Assert.assertEquals(URL, host + request.getRequestLine().getUri()); + Header[] headers = request.getAllHeaders(); + Assert.assertEquals(4, headers.length); + Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); + Assert.assertEquals( + patchCredential.getAuthorization( + URI.create(URL), + request.getRequestLine().getMethod(), + JSON_REQUEST_BODY.getBody()), + getHeaderValue(headers, AUTHORIZATION)); + + Assert.assertTrue(request instanceof HttpEntityEnclosingRequest); + HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; + HttpEntity reqEntity = entityRequest.getEntity(); + HttpEntity entity = + new StringEntity( JSON_REQUEST_BODY.getBody(), ContentType.create(JSON_REQUEST_BODY.getContentType())); - Assert.assertEquals(entity.getContentLength(), reqEntity.getContentLength()); - } + Assert.assertEquals(entity.getContentLength(), reqEntity.getContentLength()); + } - private String getHeaderValue(Header[] headers, String name) { - for (Header header : headers) { - if (header.getName().equalsIgnoreCase(name)) { - return header.getValue(); - } - } - return null; + private String getHeaderValue(Header[] headers, String name) { + for (Header header : headers) { + if (header.getName().equalsIgnoreCase(name)) { + return header.getValue(); + } } + return null; + } }; - HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() { - @Override - public void process(org.apache.http.HttpResponse response, HttpContext context) throws HttpException, IOException { - Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); - response.setHeaders(new Header[]{header}); - - ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); - int statusCode = HttpStatus.SC_OK; - String reasonPhrase = "OK"; - BasicStatusLine statusLine = new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); - response.setStatusLine(statusLine); - response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); - } + HttpResponseInterceptor responseInterceptor = + new HttpResponseInterceptor() { + @Override + public void process(org.apache.http.HttpResponse response, HttpContext context) + throws HttpException, IOException { + Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); + response.setHeaders(new Header[] {header}); + + ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); + int statusCode = HttpStatus.SC_OK; + String reasonPhrase = "OK"; + BasicStatusLine statusLine = + new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); + response.setStatusLine(statusLine); + response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); + } }; - CloseableHttpClient httpClient = HttpClients.custom(). - addInterceptorFirst(interceptor). - addInterceptorLast(responseInterceptor).build(); + CloseableHttpClient httpClient = + HttpClients.custom() + .addInterceptorFirst(interceptor) + .addInterceptorLast(responseInterceptor) + .build(); - HttpClient patchHttpClient = new ApacheHttpClientBuilder() + HttpClient patchHttpClient = + new ApacheHttpClientBuilder() .credential(patchCredential) .validator(patchValidator) .apacheHttpClient(httpClient) .build(); - HttpResponse executeSendPutResponse = - patchHttpClient.patch(requestHeaders, URL, JSON_REQUEST_BODY, TestServiceResponse.class); - - - Assert.assertEquals( - 4, executeSendPutResponse.getRequest().getHeaders().getHeaders().size()); - Assert.assertEquals( - REQUEST_HEADER_VALUE, - executeSendPutResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); - Assert.assertEquals( - FAKE_AUTHORIZATION, - executeSendPutResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); - Assert.assertEquals( - HttpMethod.PATCH, executeSendPutResponse.getRequest().getHttpMethod()); - Assert.assertEquals(URL, executeSendPutResponse.getRequest().getUrl().toString()); - Assert.assertEquals(1, executeSendPutResponse.getHeaders().getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, - executeSendPutResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); - Assert.assertTrue(executeSendPutResponse.getBody() instanceof JsonResponseBody); - Assert.assertEquals( - RESPONSE_JSON, ((JsonResponseBody) executeSendPutResponse.getBody()).getBody()); - Assert.assertEquals(RESPONSE_JSON_VALUE, executeSendPutResponse.getServiceResponse().getRequestId()); - } - - - @Test - public void testPut() throws IOException { - Credential putCredential = - new Credential() { - @Override - public String getSchema() { - return "fake-schema"; - } + HttpResponse executeSendPutResponse = + patchHttpClient.patch(requestHeaders, URL, JSON_REQUEST_BODY, TestServiceResponse.class); - @Override - public String getMerchantId() { - return MERCHANT_ID; - } + Assert.assertEquals(4, executeSendPutResponse.getRequest().getHeaders().getHeaders().size()); + Assert.assertEquals( + REQUEST_HEADER_VALUE, + executeSendPutResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); + Assert.assertEquals( + FAKE_AUTHORIZATION, + executeSendPutResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); + Assert.assertEquals(HttpMethod.PATCH, executeSendPutResponse.getRequest().getHttpMethod()); + Assert.assertEquals(URL, executeSendPutResponse.getRequest().getUrl().toString()); + Assert.assertEquals(1, executeSendPutResponse.getHeaders().getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, executeSendPutResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); + Assert.assertTrue(executeSendPutResponse.getBody() instanceof JsonResponseBody); + Assert.assertEquals( + RESPONSE_JSON, ((JsonResponseBody) executeSendPutResponse.getBody()).getBody()); + Assert.assertEquals( + RESPONSE_JSON_VALUE, executeSendPutResponse.getServiceResponse().getRequestId()); + } - @Override - public String getAuthorization(URI uri, String httpMethod, String signBody) { - Assert.assertEquals(URI.create(URL), uri); - Assert.assertEquals(HttpMethod.PUT.name(), httpMethod); - Assert.assertEquals(JSON_REQUEST_BODY.getBody(), signBody); - return FAKE_AUTHORIZATION; - } - }; - Validator putValidator = - new Validator() { - @Override - public boolean validate(HttpHeaders responseHeaders, String body) { - Assert.assertNotNull(responseHeaders); - Assert.assertEquals(1, responseHeaders.getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); - Assert.assertEquals(RESPONSE_JSON, body); - return true; - } - - @Override - public String getSerialNumber() { - return ""; - } - }; - String userAgent = - String.format( - USER_AGENT_FORMAT, - OkHttpClientAdapter.class.getPackage().getImplementationVersion(), - OS, - VERSION == null ? "Unknown" : VERSION, - putCredential.getClass().getSimpleName(), - putValidator.getClass().getSimpleName(), - "apachehttp/" + getClass().getPackage().getImplementationVersion()); - HttpRequestInterceptor interceptor = new HttpRequestInterceptor() { - @Override - public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException { - Assert.assertEquals(HttpMethod.PUT.name(), request.getRequestLine().getMethod()); - String host = context.getAttribute("http.target_host").toString(); - Assert.assertEquals(URL, host + request.getRequestLine().getUri()); - Header[] headers = request.getAllHeaders(); - Assert.assertEquals(4, headers.length); - Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); - Assert.assertEquals( - putCredential.getAuthorization( - URI.create(URL), - request.getRequestLine().getMethod(), - JSON_REQUEST_BODY.getBody()), - getHeaderValue(headers, AUTHORIZATION)); - - Assert.assertTrue(request instanceof HttpEntityEnclosingRequest); - HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; - HttpEntity reqEntity = entityRequest.getEntity(); - HttpEntity entity = new StringEntity( + @Test + public void testPut() throws IOException { + Credential putCredential = + new Credential() { + @Override + public String getSchema() { + return "fake-schema"; + } + + @Override + public String getMerchantId() { + return MERCHANT_ID; + } + + @Override + public String getAuthorization(URI uri, String httpMethod, String signBody) { + Assert.assertEquals(URI.create(URL), uri); + Assert.assertEquals(HttpMethod.PUT.name(), httpMethod); + Assert.assertEquals(JSON_REQUEST_BODY.getBody(), signBody); + return FAKE_AUTHORIZATION; + } + }; + Validator putValidator = + new Validator() { + @Override + public boolean validate(HttpHeaders responseHeaders, String body) { + Assert.assertNotNull(responseHeaders); + Assert.assertEquals(1, responseHeaders.getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); + Assert.assertEquals(RESPONSE_JSON, body); + return true; + } + + @Override + public String getSerialNumber() { + return ""; + } + }; + String userAgent = + String.format( + USER_AGENT_FORMAT, + OkHttpClientAdapter.class.getPackage().getImplementationVersion(), + OS, + VERSION == null ? "Unknown" : VERSION, + putCredential.getClass().getSimpleName(), + putValidator.getClass().getSimpleName(), + "apachehttp/" + getClass().getPackage().getImplementationVersion()); + HttpRequestInterceptor interceptor = + new HttpRequestInterceptor() { + @Override + public void process(org.apache.http.HttpRequest request, HttpContext context) + throws HttpException, IOException { + Assert.assertEquals(HttpMethod.PUT.name(), request.getRequestLine().getMethod()); + String host = context.getAttribute("http.target_host").toString(); + Assert.assertEquals(URL, host + request.getRequestLine().getUri()); + Header[] headers = request.getAllHeaders(); + Assert.assertEquals(4, headers.length); + Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); + Assert.assertEquals( + putCredential.getAuthorization( + URI.create(URL), + request.getRequestLine().getMethod(), + JSON_REQUEST_BODY.getBody()), + getHeaderValue(headers, AUTHORIZATION)); + + Assert.assertTrue(request instanceof HttpEntityEnclosingRequest); + HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; + HttpEntity reqEntity = entityRequest.getEntity(); + HttpEntity entity = + new StringEntity( JSON_REQUEST_BODY.getBody(), ContentType.create(JSON_REQUEST_BODY.getContentType())); - Assert.assertEquals(entity.getContentLength(), reqEntity.getContentLength()); - } + Assert.assertEquals(entity.getContentLength(), reqEntity.getContentLength()); + } - private String getHeaderValue(Header[] headers, String name) { - for (Header header : headers) { - if (header.getName().equalsIgnoreCase(name)) { - return header.getValue(); - } - } - return null; + private String getHeaderValue(Header[] headers, String name) { + for (Header header : headers) { + if (header.getName().equalsIgnoreCase(name)) { + return header.getValue(); + } } + return null; + } }; - HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() { - @Override - public void process(org.apache.http.HttpResponse response, HttpContext context) throws HttpException, IOException { - Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); - response.setHeaders(new Header[]{header}); - - ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); - int statusCode = HttpStatus.SC_OK; - String reasonPhrase = "OK"; - BasicStatusLine statusLine = new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); - response.setStatusLine(statusLine); - response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); - } + HttpResponseInterceptor responseInterceptor = + new HttpResponseInterceptor() { + @Override + public void process(org.apache.http.HttpResponse response, HttpContext context) + throws HttpException, IOException { + Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); + response.setHeaders(new Header[] {header}); + + ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); + int statusCode = HttpStatus.SC_OK; + String reasonPhrase = "OK"; + BasicStatusLine statusLine = + new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); + response.setStatusLine(statusLine); + response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); + } }; - CloseableHttpClient httpClient = HttpClients.custom(). - addInterceptorFirst(interceptor). - addInterceptorLast(responseInterceptor).build(); + CloseableHttpClient httpClient = + HttpClients.custom() + .addInterceptorFirst(interceptor) + .addInterceptorLast(responseInterceptor) + .build(); - HttpClient putHttpClient = new ApacheHttpClientBuilder() + HttpClient putHttpClient = + new ApacheHttpClientBuilder() .credential(putCredential) .validator(putValidator) .apacheHttpClient(httpClient) .build(); - HttpResponse executeSendPutResponse = - putHttpClient.put(requestHeaders, URL, JSON_REQUEST_BODY, TestServiceResponse.class); - - - Assert.assertEquals( - 4, executeSendPutResponse.getRequest().getHeaders().getHeaders().size()); - Assert.assertEquals( - REQUEST_HEADER_VALUE, - executeSendPutResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); - Assert.assertEquals( - FAKE_AUTHORIZATION, - executeSendPutResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); - Assert.assertEquals( - HttpMethod.PUT, executeSendPutResponse.getRequest().getHttpMethod()); - Assert.assertEquals(URL, executeSendPutResponse.getRequest().getUrl().toString()); - Assert.assertEquals(1, executeSendPutResponse.getHeaders().getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, - executeSendPutResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); - Assert.assertTrue(executeSendPutResponse.getBody() instanceof JsonResponseBody); - Assert.assertEquals( - RESPONSE_JSON, ((JsonResponseBody) executeSendPutResponse.getBody()).getBody()); - Assert.assertEquals(RESPONSE_JSON_VALUE, executeSendPutResponse.getServiceResponse().getRequestId()); - } - - - @Test - public void testDelete() throws IOException { - Credential executeSendDeleteCredential = - new Credential() { - @Override - public String getSchema() { - return "fake-schema"; - } + HttpResponse executeSendPutResponse = + putHttpClient.put(requestHeaders, URL, JSON_REQUEST_BODY, TestServiceResponse.class); - @Override - public String getMerchantId() { - return MERCHANT_ID; - } + Assert.assertEquals(4, executeSendPutResponse.getRequest().getHeaders().getHeaders().size()); + Assert.assertEquals( + REQUEST_HEADER_VALUE, + executeSendPutResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); + Assert.assertEquals( + FAKE_AUTHORIZATION, + executeSendPutResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); + Assert.assertEquals(HttpMethod.PUT, executeSendPutResponse.getRequest().getHttpMethod()); + Assert.assertEquals(URL, executeSendPutResponse.getRequest().getUrl().toString()); + Assert.assertEquals(1, executeSendPutResponse.getHeaders().getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, executeSendPutResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); + Assert.assertTrue(executeSendPutResponse.getBody() instanceof JsonResponseBody); + Assert.assertEquals( + RESPONSE_JSON, ((JsonResponseBody) executeSendPutResponse.getBody()).getBody()); + Assert.assertEquals( + RESPONSE_JSON_VALUE, executeSendPutResponse.getServiceResponse().getRequestId()); + } - @Override - public String getAuthorization(URI uri, String httpMethod, String signBody) { - Assert.assertEquals(URI.create(URL), uri); - Assert.assertEquals(HttpMethod.DELETE.name(), httpMethod); - Assert.assertEquals(JSON_REQUEST_BODY.getBody(), signBody); - return FAKE_AUTHORIZATION; - } - }; - Validator executeSendDeleteValidator = - new Validator() { - @Override - public boolean validate(HttpHeaders responseHeaders, String body) { - Assert.assertNotNull(responseHeaders); - Assert.assertEquals(1, responseHeaders.getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); - Assert.assertEquals(RESPONSE_JSON, body); - return true; - } - - @Override - public String getSerialNumber() { - return ""; - } - }; - String userAgent = - String.format( - USER_AGENT_FORMAT, - OkHttpClientAdapter.class.getPackage().getImplementationVersion(), - OS, - VERSION == null ? "Unknown" : VERSION, - executeSendDeleteCredential.getClass().getSimpleName(), - executeSendDeleteValidator.getClass().getSimpleName(), - "apachehttp/" + getClass().getPackage().getImplementationVersion()); - HttpRequestInterceptor interceptor = new HttpRequestInterceptor() { - @Override - public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException { - Assert.assertEquals(HttpMethod.DELETE.name(), request.getRequestLine().getMethod()); - String host = context.getAttribute("http.target_host").toString(); - Assert.assertEquals(URL, host + request.getRequestLine().getUri()); - Header[] headers = request.getAllHeaders(); - Assert.assertEquals(4, headers.length); - Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); - Assert.assertEquals( - executeSendDeleteCredential.getAuthorization( - URI.create(URL), - request.getRequestLine().getMethod(), - JSON_REQUEST_BODY.getBody()), - getHeaderValue(headers, AUTHORIZATION)); - } + @Test + public void testDelete() throws IOException { + Credential executeSendDeleteCredential = + new Credential() { + @Override + public String getSchema() { + return "fake-schema"; + } - private String getHeaderValue(Header[] headers, String name) { - for (Header header : headers) { - if (header.getName().equalsIgnoreCase(name)) { - return header.getValue(); - } - } - return null; - } + @Override + public String getMerchantId() { + return MERCHANT_ID; + } + + @Override + public String getAuthorization(URI uri, String httpMethod, String signBody) { + Assert.assertEquals(URI.create(URL), uri); + Assert.assertEquals(HttpMethod.DELETE.name(), httpMethod); + Assert.assertEquals(JSON_REQUEST_BODY.getBody(), signBody); + return FAKE_AUTHORIZATION; + } + }; + Validator executeSendDeleteValidator = + new Validator() { + @Override + public boolean validate(HttpHeaders responseHeaders, String body) { + Assert.assertNotNull(responseHeaders); + Assert.assertEquals(1, responseHeaders.getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, responseHeaders.getHeader(RESPONSE_HEADER_KEY)); + Assert.assertEquals(RESPONSE_JSON, body); + return true; + } + + @Override + public String getSerialNumber() { + return ""; + } }; + String userAgent = + String.format( + USER_AGENT_FORMAT, + OkHttpClientAdapter.class.getPackage().getImplementationVersion(), + OS, + VERSION == null ? "Unknown" : VERSION, + executeSendDeleteCredential.getClass().getSimpleName(), + executeSendDeleteValidator.getClass().getSimpleName(), + "apachehttp/" + getClass().getPackage().getImplementationVersion()); + HttpRequestInterceptor interceptor = + new HttpRequestInterceptor() { + @Override + public void process(org.apache.http.HttpRequest request, HttpContext context) + throws HttpException, IOException { + Assert.assertEquals(HttpMethod.DELETE.name(), request.getRequestLine().getMethod()); + String host = context.getAttribute("http.target_host").toString(); + Assert.assertEquals(URL, host + request.getRequestLine().getUri()); + Header[] headers = request.getAllHeaders(); + Assert.assertEquals(4, headers.length); + Assert.assertEquals(REQUEST_HEADER_VALUE, getHeaderValue(headers, REQUEST_HEADER_KEY)); + Assert.assertEquals( + executeSendDeleteCredential.getAuthorization( + URI.create(URL), + request.getRequestLine().getMethod(), + JSON_REQUEST_BODY.getBody()), + getHeaderValue(headers, AUTHORIZATION)); + } - HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() { - @Override - public void process(org.apache.http.HttpResponse response, HttpContext context) throws HttpException, IOException { - Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); - response.setHeaders(new Header[]{header}); - - ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); - int statusCode = HttpStatus.SC_OK; - String reasonPhrase = "OK"; - BasicStatusLine statusLine = new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); - response.setStatusLine(statusLine); - response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); + private String getHeaderValue(Header[] headers, String name) { + for (Header header : headers) { + if (header.getName().equalsIgnoreCase(name)) { + return header.getValue(); + } } + return null; + } + }; + + HttpResponseInterceptor responseInterceptor = + new HttpResponseInterceptor() { + @Override + public void process(org.apache.http.HttpResponse response, HttpContext context) + throws HttpException, IOException { + Header header = new BasicHeader(RESPONSE_HEADER_KEY, RESPONSE_HEADER_VALUE); + response.setHeaders(new Header[] {header}); + + ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); + int statusCode = HttpStatus.SC_OK; + String reasonPhrase = "OK"; + BasicStatusLine statusLine = + new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); + response.setStatusLine(statusLine); + response.setEntity(new StringEntity(RESPONSE_JSON, ContentType.APPLICATION_JSON)); + } }; - CloseableHttpClient httpClient = HttpClients.custom(). - addInterceptorFirst(interceptor). - addInterceptorLast(responseInterceptor).build(); + CloseableHttpClient httpClient = + HttpClients.custom() + .addInterceptorFirst(interceptor) + .addInterceptorLast(responseInterceptor) + .build(); - HttpClient executeSendPutHttpClient = new ApacheHttpClientBuilder() + HttpClient executeSendPutHttpClient = + new ApacheHttpClientBuilder() .credential(executeSendDeleteCredential) .validator(executeSendDeleteValidator) .apacheHttpClient(httpClient) .build(); + HttpRequest httpRequest = + new HttpRequest.Builder() + .httpMethod(HttpMethod.DELETE) + .url(URL) + .headers(requestHeaders) + .body(JSON_REQUEST_BODY) + .build(); - HttpRequest httpRequest = - new HttpRequest.Builder() - .httpMethod(HttpMethod.DELETE) - .url(URL) - .headers(requestHeaders) - .body(JSON_REQUEST_BODY) - .build(); - - HttpResponse executeSendPutResponse = - executeSendPutHttpClient.execute(httpRequest, TestServiceResponse.class); - - - Assert.assertEquals( - 4, executeSendPutResponse.getRequest().getHeaders().getHeaders().size()); - Assert.assertEquals( - REQUEST_HEADER_VALUE, - executeSendPutResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); - Assert.assertEquals( - FAKE_AUTHORIZATION, - executeSendPutResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); - Assert.assertEquals( - HttpMethod.DELETE, executeSendPutResponse.getRequest().getHttpMethod()); - Assert.assertEquals(URL, executeSendPutResponse.getRequest().getUrl().toString()); - Assert.assertEquals(1, executeSendPutResponse.getHeaders().getHeaders().size()); - Assert.assertEquals( - RESPONSE_HEADER_VALUE, - executeSendPutResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); - Assert.assertTrue(executeSendPutResponse.getBody() instanceof JsonResponseBody); - Assert.assertEquals( - RESPONSE_JSON, ((JsonResponseBody) executeSendPutResponse.getBody()).getBody()); - Assert.assertEquals(RESPONSE_JSON_VALUE, executeSendPutResponse.getServiceResponse().getRequestId()); - } + HttpResponse executeSendPutResponse = + executeSendPutHttpClient.execute(httpRequest, TestServiceResponse.class); + Assert.assertEquals(4, executeSendPutResponse.getRequest().getHeaders().getHeaders().size()); + Assert.assertEquals( + REQUEST_HEADER_VALUE, + executeSendPutResponse.getRequest().getHeaders().getHeader(REQUEST_HEADER_KEY)); + Assert.assertEquals( + FAKE_AUTHORIZATION, + executeSendPutResponse.getRequest().getHeaders().getHeader(AUTHORIZATION)); + Assert.assertEquals(HttpMethod.DELETE, executeSendPutResponse.getRequest().getHttpMethod()); + Assert.assertEquals(URL, executeSendPutResponse.getRequest().getUrl().toString()); + Assert.assertEquals(1, executeSendPutResponse.getHeaders().getHeaders().size()); + Assert.assertEquals( + RESPONSE_HEADER_VALUE, executeSendPutResponse.getHeaders().getHeader(RESPONSE_HEADER_KEY)); + Assert.assertTrue(executeSendPutResponse.getBody() instanceof JsonResponseBody); + Assert.assertEquals( + RESPONSE_JSON, ((JsonResponseBody) executeSendPutResponse.getBody()).getBody()); + Assert.assertEquals( + RESPONSE_JSON_VALUE, executeSendPutResponse.getServiceResponse().getRequestId()); + } } diff --git a/core/src/test/java/com/wechat/pay/java/core/http/ApacheHttpClientBuilderTest.java b/core/src/test/java/com/wechat/pay/java/core/http/ApacheHttpClientBuilderTest.java index b86462a9..0a2175c9 100644 --- a/core/src/test/java/com/wechat/pay/java/core/http/ApacheHttpClientBuilderTest.java +++ b/core/src/test/java/com/wechat/pay/java/core/http/ApacheHttpClientBuilderTest.java @@ -1,23 +1,18 @@ package com.wechat.pay.java.core.http; +import static org.junit.jupiter.api.Assertions.assertNotNull; + import com.wechat.pay.java.core.Config; import com.wechat.pay.java.core.auth.Credential; import com.wechat.pay.java.core.auth.Validator; import com.wechat.pay.java.core.cipher.PrivacyDecryptor; import com.wechat.pay.java.core.cipher.PrivacyEncryptor; import com.wechat.pay.java.core.cipher.Signer; -import okhttp3.OkHttpClient; +import java.net.URI; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.junit.jupiter.api.Test; -import java.net.InetSocketAddress; -import java.net.Proxy; -import java.net.Proxy.Type; -import java.net.URI; - -import static org.junit.jupiter.api.Assertions.assertNotNull; - class ApacheHttpClientBuilderTest { Credential credential = @@ -93,8 +88,7 @@ public Signer createSigner() { return null; } }; - HttpClient httpClient = - new ApacheHttpClientBuilder().config(config).build(); + HttpClient httpClient = new ApacheHttpClientBuilder().config(config).build(); assertNotNull(httpClient); } }