diff --git a/LICENSE b/LICENSE index 3154774..d703157 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (C) 2023, Twilio SendGrid, Inc. +Copyright (C) 2024, Twilio SendGrid, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/pom.xml b/pom.xml index efcc8d7..24ac741 100644 --- a/pom.xml +++ b/pom.xml @@ -117,14 +117,14 @@ test - org.apache.httpcomponents - httpcore - 4.4.15 + org.apache.httpcomponents.core5 + httpcore5 + 5.2.4 - org.apache.httpcomponents - httpclient - 4.5.13 + org.apache.httpcomponents.client5 + httpclient5 + 5.3.1 @@ -216,4 +216,4 @@ - \ No newline at end of file + diff --git a/src/main/java/com/sendgrid/Client.java b/src/main/java/com/sendgrid/Client.java index d0865fd..a7138af 100644 --- a/src/main/java/com/sendgrid/Client.java +++ b/src/main/java/com/sendgrid/Client.java @@ -6,45 +6,28 @@ import java.io.StringWriter; import java.net.URI; import java.net.URISyntaxException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; -import java.util.Arrays; -import java.util.List; - -import org.apache.http.Header; -import org.apache.http.HttpMessage; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPatch; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.client.methods.HttpRequestBase; -import org.apache.http.client.utils.URIBuilder; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.Header; +import org.apache.hc.core5.http.HttpException; +import org.apache.hc.core5.http.HttpMessage; +import org.apache.hc.core5.http.io.HttpClientResponseHandler; +import org.apache.hc.core5.http.io.entity.StringEntity; +import org.apache.hc.core5.http.message.BasicClassicHttpRequest; +import org.apache.hc.core5.net.URIBuilder; -/** - * Hack to get DELETE to accept a request body. - */ -class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase { - public static final String METHOD_NAME = "DELETE"; +import org.apache.hc.client5.http.ClientProtocolException; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.classic.methods.HttpPatch; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.classic.methods.HttpPut; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; - @Override - public String getMethod() { - return METHOD_NAME; - } - - public HttpDeleteWithBody(final String uri) { - super(); - setURI(URI.create(uri)); - } -} +import com.sun.jndi.toolkit.url.Uri; /** @@ -52,9 +35,8 @@ public HttpDeleteWithBody(final String uri) { */ public class Client implements Closeable { - private CloseableHttpClient httpClient; - private Boolean test; - private boolean createdHttpClient; + private final CloseableHttpClient httpClient; + private final Boolean test; /** @@ -63,7 +45,6 @@ public class Client implements Closeable { public Client() { this.httpClient = HttpClients.createDefault(); this.test = false; - this.createdHttpClient = true; } @@ -101,7 +82,6 @@ public Client(Boolean test) { public Client(CloseableHttpClient httpClient, Boolean test) { this.httpClient = httpClient; this.test = test; - this.createdHttpClient = true; } @@ -119,9 +99,8 @@ public Client(CloseableHttpClient httpClient, Boolean test) { */ public URI buildUri(String baseUri, String endpoint, Map queryParams) throws URISyntaxException { URIBuilder builder = new URIBuilder(); - URI uri = null; - if (this.test == true) { + if (this.test) { builder.setScheme("http"); } else { builder.setScheme("https"); @@ -136,8 +115,8 @@ public URI buildUri(String baseUri, String endpoint, Map queryPa for (Map.Entry entry : queryParams.entrySet()) { String value = entry.getValue(); - if (value.indexOf(multiValueDelimiter) != -1) { - List values = Arrays.asList(value.split(multiValueDelimiter)); + if (value.contains(multiValueDelimiter)) { + String[] values = value.split(multiValueDelimiter); for (String val : values) { builder.addParameter(entry.getKey(), val); } @@ -146,14 +125,7 @@ public URI buildUri(String baseUri, String endpoint, Map queryPa } } } - - try { - uri = builder.build(); - } catch (URISyntaxException ex) { - throw ex; - } - - return uri; + return builder.build(); } @@ -166,14 +138,14 @@ public URI buildUri(String baseUri, String endpoint, Map queryPa * in case of a network error * @return the response object */ - public Response getResponse(CloseableHttpResponse response) throws IOException { - ResponseHandler handler = new SendGridResponseHandler(); + public Response getResponse(ClassicHttpResponse response) throws IOException, HttpException{ + HttpClientResponseHandler handler = new SendGridResponseHandler(); String responseBody = handler.handleResponse(response); - int statusCode = response.getStatusLine().getStatusCode(); + int statusCode = response.getCode(); - Header[] headers = response.getAllHeaders(); - Map responseHeaders = new HashMap(); + Header[] headers = response.getHeaders(); + Map responseHeaders = new HashMap<>(); for (Header h : headers) { responseHeaders.put(h.getName(), h.getValue()); } @@ -185,8 +157,8 @@ public Response getResponse(CloseableHttpResponse response) throws IOException { /** * Make a GET request and provide the status code, response body and * response headers. - * - * @param request + * + * @param request * the request object * @throws URISyntaxException * in case of a URI syntax error @@ -195,15 +167,8 @@ public Response getResponse(CloseableHttpResponse response) throws IOException { * @return the response object */ public Response get(Request request) throws URISyntaxException, IOException { - URI uri = null; - HttpGet httpGet = null; - - try { - uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams()); - httpGet = new HttpGet(uri.toString()); - } catch (URISyntaxException ex) { - throw ex; - } + URI uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams()); + HttpGet httpGet = new HttpGet(uri.toString()); if (request.getHeaders() != null) { for (Map.Entry entry : request.getHeaders().entrySet()) { @@ -218,7 +183,7 @@ public Response get(Request request) throws URISyntaxException, IOException { * Make a POST request and provide the status code, response body and * response headers. * - * @param request + * @param request * the request object * @throws URISyntaxException * in case of a URI syntax error @@ -227,15 +192,8 @@ public Response get(Request request) throws URISyntaxException, IOException { * @return the response object */ public Response post(Request request) throws URISyntaxException, IOException { - URI uri = null; - HttpPost httpPost = null; - - try { - uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams()); - httpPost = new HttpPost(uri.toString()); - } catch (URISyntaxException ex) { - throw ex; - } + URI uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams()); + HttpPost httpPost = new HttpPost(uri.toString()); if (request.getHeaders() != null) { for (Map.Entry entry : request.getHeaders().entrySet()) { @@ -243,7 +201,7 @@ public Response post(Request request) throws URISyntaxException, IOException { } } - httpPost.setEntity(new StringEntity(request.getBody(), Charset.forName("UTF-8"))); + httpPost.setEntity(new StringEntity(request.getBody(), StandardCharsets.UTF_8)); writeContentTypeIfNeeded(request, httpPost); return executeApiCall(httpPost); @@ -254,7 +212,7 @@ public Response post(Request request) throws URISyntaxException, IOException { * Make a PATCH request and provide the status code, response body and * response headers. * - * @param request + * @param request * the request object * @throws URISyntaxException * in case of a URI syntax error @@ -263,15 +221,8 @@ public Response post(Request request) throws URISyntaxException, IOException { * @return the response object */ public Response patch(Request request) throws URISyntaxException, IOException { - URI uri = null; - HttpPatch httpPatch = null; - - try { - uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams()); - httpPatch = new HttpPatch(uri.toString()); - } catch (URISyntaxException ex) { - throw ex; - } + URI uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams()); + HttpPatch httpPatch = new HttpPatch(uri.toString()); if (request.getHeaders() != null) { for (Map.Entry entry : request.getHeaders().entrySet()) { @@ -279,7 +230,7 @@ public Response patch(Request request) throws URISyntaxException, IOException { } } - httpPatch.setEntity(new StringEntity(request.getBody(), Charset.forName("UTF-8"))); + httpPatch.setEntity(new StringEntity(request.getBody(), StandardCharsets.UTF_8)); writeContentTypeIfNeeded(request, httpPatch); return executeApiCall(httpPatch); @@ -290,7 +241,7 @@ public Response patch(Request request) throws URISyntaxException, IOException { * Make a PUT request and provide the status code, response body and * response headers. * - * @param request + * @param request * the request object * @throws URISyntaxException * in case of a URI syntax error @@ -299,15 +250,8 @@ public Response patch(Request request) throws URISyntaxException, IOException { * @return the response object */ public Response put(Request request) throws URISyntaxException, IOException { - URI uri = null; - HttpPut httpPut = null; - - try { - uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams()); - httpPut = new HttpPut(uri.toString()); - } catch (URISyntaxException ex) { - throw ex; - } + URI uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams()); + HttpPut httpPut = new HttpPut(uri.toString()); if (request.getHeaders() != null) { for (Map.Entry entry : request.getHeaders().entrySet()) { @@ -315,7 +259,7 @@ public Response put(Request request) throws URISyntaxException, IOException { } } - httpPut.setEntity(new StringEntity(request.getBody(), Charset.forName("UTF-8"))); + httpPut.setEntity(new StringEntity(request.getBody(), StandardCharsets.UTF_8)); writeContentTypeIfNeeded(request, httpPut); return executeApiCall(httpPut); @@ -325,7 +269,7 @@ public Response put(Request request) throws URISyntaxException, IOException { /** * Make a DELETE request and provide the status code and response headers. * - * @param request + * @param request * the request object * @throws URISyntaxException * in case of a URI syntax error @@ -334,15 +278,8 @@ public Response put(Request request) throws URISyntaxException, IOException { * @return the response object */ public Response delete(Request request) throws URISyntaxException, IOException { - URI uri = null; - HttpDeleteWithBody httpDelete = null; - - try { - uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams()); - httpDelete = new HttpDeleteWithBody(uri.toString()); - } catch (URISyntaxException ex) { - throw ex; - } + URI uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams()); + BasicClassicHttpRequest httpDelete = new BasicClassicHttpRequest("DELETE", uri.toString()); if (request.getHeaders() != null) { for (Map.Entry entry : request.getHeaders().entrySet()) { @@ -350,7 +287,7 @@ public Response delete(Request request) throws URISyntaxException, IOException { } } - httpDelete.setEntity(new StringEntity(request.getBody(), Charset.forName("UTF-8"))); + httpDelete.setEntity(new StringEntity(request.getBody(), StandardCharsets.UTF_8)); writeContentTypeIfNeeded(request, httpDelete); return executeApiCall(httpDelete); @@ -366,21 +303,16 @@ private void writeContentTypeIfNeeded(Request request, HttpMessage httpMessage) /** * Makes a call to the client API. * - * @param httpPost + * @param httpPost * the request method object * @throws IOException * in case of a network error * @return the response object */ - private Response executeApiCall(HttpRequestBase httpPost) throws IOException { + private Response executeApiCall(BasicClassicHttpRequest httpPost) throws IOException { try { - CloseableHttpResponse serverResponse = httpClient.execute(httpPost); - try { - return getResponse(serverResponse); - } finally { - serverResponse.close(); - } - } catch(ClientProtocolException e) { + return getResponse(httpClient.execute(httpPost, response -> response)); + } catch(ClientProtocolException | HttpException e) { throw new IOException(e.getMessage()); } } @@ -389,7 +321,7 @@ private Response executeApiCall(HttpRequestBase httpPost) throws IOException { /** * A thin wrapper around the HTTP methods. * - * @param request + * @param request * the request object * @throws IOException * in case of a network error @@ -414,9 +346,7 @@ public Response api(Request request) throws IOException { default: throw new IOException("We only support GET, PUT, PATCH, POST and DELETE."); } - } catch (IOException ex) { - throw ex; - } catch (URISyntaxException ex) { + }catch (URISyntaxException ex) { StringWriter errors = new StringWriter(); ex.printStackTrace(new PrintWriter(errors)); throw new IOException(errors.toString()); @@ -429,27 +359,10 @@ public Response api(Request request) throws IOException { * * @throws IOException * in case of a network error - */ + */ @Override public void close() throws IOException { - this.httpClient.close(); + this.httpClient.close(); } - - /** - * Closes and finalizes the http client. - * - * @throws Throwable - * in case of an error - */ - @Override - public void finalize() throws Throwable { - try { - close(); - } catch(IOException e) { - throw new Throwable(e.getMessage()); - } finally { - super.finalize(); - } - } } diff --git a/src/main/java/com/sendgrid/SendGridResponseHandler.java b/src/main/java/com/sendgrid/SendGridResponseHandler.java index 109b704..ea0d241 100644 --- a/src/main/java/com/sendgrid/SendGridResponseHandler.java +++ b/src/main/java/com/sendgrid/SendGridResponseHandler.java @@ -2,41 +2,48 @@ import java.io.IOException; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpResponseException; -import org.apache.http.impl.client.AbstractResponseHandler; -import org.apache.http.util.EntityUtils; +import org.apache.hc.client5.http.impl.classic.AbstractHttpClientResponseHandler; +import org.apache.hc.core5.http.ClassicHttpRequest; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.ParseException; +import org.apache.hc.core5.http.io.HttpClientResponseHandler; +import org.apache.hc.core5.http.io.entity.EntityUtils; import java.nio.charset.StandardCharsets; /** - * A {@link org.apache.http.client.ResponseHandler} that returns the response body as a String - * for all responses. + * A {@link org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler} that returns the response body as a + * String + * for all responses. *

* If this is used with - * {@link org.apache.http.client.HttpClient#execute( - * org.apache.http.client.methods.HttpUriRequest, org.apache.http.client.ResponseHandler)}, + * {@link org.apache.hc.client5.http.classic.HttpClient#execute(ClassicHttpRequest, HttpClientResponseHandler)} * HttpClient may handle redirects (3xx responses) internally. + * *

* */ -public class SendGridResponseHandler extends AbstractResponseHandler{ +public class SendGridResponseHandler extends AbstractHttpClientResponseHandler{ /** * Read the entity from the response body and pass it to the entity handler * method if the response was successful (a 2xx status code). If no response * body exists, this returns null. If the response was unsuccessful (>= 500 - * status code), throws an {@link HttpResponseException}. + * status code), throws an {@link IOException}. */ @Override - public String handleResponse(final HttpResponse response) - throws HttpResponseException, IOException { + public String handleResponse(final ClassicHttpResponse response) + throws IOException { final HttpEntity entity = response.getEntity(); return entity == null ? null : handleEntity(entity); } - + @Override - public String handleEntity(HttpEntity entity) throws IOException { - return EntityUtils.toString(entity, StandardCharsets.UTF_8); + public String handleEntity(HttpEntity entity) throws IOException{ + try{ + return EntityUtils.toString(entity, StandardCharsets.UTF_8); + }catch(ParseException e){ + throw new RuntimeException(e); + } } } diff --git a/src/test/java/com/sendgrid/ClientTest.java b/src/test/java/com/sendgrid/ClientTest.java index 0425199..fa33041 100644 --- a/src/test/java/com/sendgrid/ClientTest.java +++ b/src/test/java/com/sendgrid/ClientTest.java @@ -1,32 +1,18 @@ package com.sendgrid; -import org.apache.http.Header; -import org.apache.http.HttpEntity; -import org.apache.http.HttpStatus; -import org.apache.http.HttpVersion; -import org.apache.http.StatusLine; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpDelete; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPatch; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.client.methods.HttpUriRequest; -import org.apache.http.client.utils.URIBuilder; -import org.apache.http.entity.InputStreamEntity; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.BasicResponseHandler; -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.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.core5.http.ClassicHttpRequest; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.HttpException; +import org.apache.hc.core5.http.io.HttpClientResponseHandler; +import org.apache.hc.core5.http.io.entity.InputStreamEntity; +import org.apache.hc.core5.http.message.BasicHeader; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.mockito.Matchers; import org.mockito.Mockito; import java.io.ByteArrayInputStream; @@ -42,26 +28,23 @@ public class ClientTest extends Mockito { - private CloseableHttpClient httpClient; - private CloseableHttpResponse response; - private HttpEntity entity; - private StatusLine statusline; + private CloseableHttpClient mockHttpClient; + private ClassicHttpResponse mockResponse; + @Before - public void setUp() throws Exception { - this.httpClient = mock(CloseableHttpClient.class); - this.response = mock(CloseableHttpResponse.class); - this.entity = mock(HttpEntity.class); - this.statusline = mock(StatusLine.class); + public void setUp() { + this.mockHttpClient = mock(CloseableHttpClient.class); + this.mockResponse = mock(ClassicHttpResponse.class); } @Test - public void testbuildUri() { + public void testBuildUri() { Client client = new Client(); String baseUri = "api.test.com"; String endpoint = "/endpoint"; URI uri = null; - Map queryParams = new HashMap(); + Map queryParams = new HashMap<>(); queryParams.put("test1", "1"); queryParams.put("test2", "2"); queryParams.put("test3", "3&4&5"); @@ -70,7 +53,7 @@ public void testbuildUri() { } catch (URISyntaxException ex) { StringWriter errors = new StringWriter(); ex.printStackTrace(new PrintWriter(errors)); - Assert.assertTrue(errors.toString(), false); + Assert.fail(errors.toString()); } URL url = null; @@ -79,12 +62,12 @@ public void testbuildUri() { } catch (MalformedURLException ex) { StringWriter errors = new StringWriter(); ex.printStackTrace(new PrintWriter(errors)); - Assert.assertTrue(errors.toString(), false); + Assert.fail(errors.toString()); } - Assert.assertTrue(url.getProtocol().equals("https")); - Assert.assertTrue(url.getHost().equals("api.test.com")); - Assert.assertTrue(url.getPath().equals("/endpoint")); + Assert.assertEquals("https", url.getProtocol()); + Assert.assertEquals("api.test.com", url.getHost()); + Assert.assertEquals("/endpoint", url.getPath()); Assert.assertTrue(this.queryParamHasCorrectValue(url, "test1", "1")); Assert.assertTrue(this.queryParamHasCorrectValue(url, "test2", "2")); Assert.assertTrue(this.queryParamHasCorrectValue(url, "test3", "3")); @@ -96,31 +79,30 @@ public void testbuildUri() { public void testGetResponse() { Client client = new Client(); Response testResponse = new Response(); - Header[] mockedHeaders = null; + BasicHeader[] mockedHeaders = null; try { - when(statusline.getStatusCode()).thenReturn(200); - when(response.getStatusLine()).thenReturn(statusline); - when(response.getEntity()).thenReturn( - new InputStreamEntity( - new ByteArrayInputStream( - "{\"message\":\"success\"}".getBytes()))); - mockedHeaders = new Header[] { new BasicHeader("headerA", "valueA") }; - when(response.getAllHeaders()).thenReturn(mockedHeaders); - when(httpClient.execute(Matchers.any(HttpGet.class))).thenReturn(response); + when(mockResponse.getCode()).thenReturn(200); + when(mockResponse.getEntity()).thenReturn( + new InputStreamEntity( + new ByteArrayInputStream( + "{\"message\":\"success\"}".getBytes()), ContentType.APPLICATION_JSON)); + mockedHeaders = new BasicHeader[] { new BasicHeader("headerA", "valueA") }; + when(mockResponse.getHeaders()).thenReturn(mockedHeaders); + when(mockHttpClient.execute(any(ClassicHttpRequest.class), any(HttpClientResponseHandler.class))).thenReturn(mockResponse); HttpGet httpGet = new HttpGet("https://api.test.com"); - CloseableHttpResponse resp = httpClient.execute(httpGet); + ClassicHttpResponse resp = mockHttpClient.execute(httpGet, response -> response); testResponse = client.getResponse(resp); resp.close(); - } catch (IOException ex) { + } catch (IOException | HttpException ex) { StringWriter errors = new StringWriter(); ex.printStackTrace(new PrintWriter(errors)); - Assert.assertTrue(errors.toString(), false); + Assert.fail(errors.toString()); } - Assert.assertTrue(testResponse.getStatusCode() == 200); + Assert.assertEquals(200, testResponse.getStatusCode()); Assert.assertEquals(testResponse.getBody(), "{\"message\":\"success\"}"); - Map headers = new HashMap(); - for (Header h:mockedHeaders) { + Map headers = new HashMap<>(); + for (BasicHeader h:mockedHeaders) { headers.put(h.getName(), h.getValue()); } Assert.assertEquals(testResponse.getHeaders(), headers); @@ -129,38 +111,37 @@ public void testGetResponse() { public void testMethod(Method method, int statusCode) { Response testResponse = new Response(); Request request = new Request(); - Header[] mockedHeaders = null; + BasicHeader[] mockedHeaders = null; try { - when(statusline.getStatusCode()).thenReturn(statusCode); - when(response.getStatusLine()).thenReturn(statusline); - when(response.getEntity()).thenReturn( - new InputStreamEntity( - new ByteArrayInputStream( - "{\"message\":\"success\"}".getBytes()))); - mockedHeaders = new Header[] { new BasicHeader("headerA", "valueA") }; - when(response.getAllHeaders()).thenReturn(mockedHeaders); - when(httpClient.execute(Matchers.any(HttpGet.class))).thenReturn(response); + when(mockResponse.getCode()).thenReturn(statusCode); + when(mockResponse.getEntity()).thenReturn( + new InputStreamEntity( + new ByteArrayInputStream( + "{\"message\":\"success\"}".getBytes()), ContentType.APPLICATION_JSON)); + mockedHeaders = new BasicHeader[] { new BasicHeader("headerA", "valueA") }; + when(mockResponse.getHeaders()).thenReturn(mockedHeaders); + when(mockHttpClient.execute(any(ClassicHttpRequest.class), any(HttpClientResponseHandler.class))).thenReturn(mockResponse); request.setMethod(method); if ((method == Method.POST) || (method == Method.PATCH) || (method == Method.PUT)) { request.setBody("{\"test\":\"testResult\"}"); } request.setEndpoint("/test"); request.addHeader("Authorization", "Bearer XXXX"); - Client client = new Client(httpClient); + Client client = new Client(mockHttpClient); testResponse = client.get(request); } catch (URISyntaxException | IOException ex) { StringWriter errors = new StringWriter(); ex.printStackTrace(new PrintWriter(errors)); - Assert.assertTrue(errors.toString(), false); + Assert.fail(errors.toString()); } - Assert.assertTrue(testResponse.getStatusCode() == statusCode); + Assert.assertEquals(testResponse.getStatusCode(), statusCode); if (method != Method.DELETE) { Assert.assertEquals(testResponse.getBody(), "{\"message\":\"success\"}"); } Assert.assertEquals(testResponse.getBody(), "{\"message\":\"success\"}"); - Map headers = new HashMap(); - for (Header h:mockedHeaders) { + Map headers = new HashMap<>(); + for (BasicHeader h:mockedHeaders) { headers.put(h.getName(), h.getValue()); } Assert.assertEquals(testResponse.getHeaders(), headers); @@ -192,6 +173,6 @@ public void testDelete() { } private boolean queryParamHasCorrectValue(URL url, String key, String value) { - return url.getQuery().indexOf(key + "=" + value) != -1; + return url.getQuery().contains(key + "=" + value); } }