diff --git a/core/src/main/java/com/redhat/lightblue/client/request/CRUDRequest.java b/core/src/main/java/com/redhat/lightblue/client/request/CRUDRequest.java index d117363..bfe6c61 100644 --- a/core/src/main/java/com/redhat/lightblue/client/request/CRUDRequest.java +++ b/core/src/main/java/com/redhat/lightblue/client/request/CRUDRequest.java @@ -1,9 +1,5 @@ package com.redhat.lightblue.client.request; -import org.apache.commons.lang.StringUtils; - -import com.fasterxml.jackson.databind.JsonNode; - import com.redhat.lightblue.client.http.HttpMethod; import com.redhat.lightblue.client.Operation; diff --git a/core/src/main/java/com/redhat/lightblue/client/request/LightblueRequest.java b/core/src/main/java/com/redhat/lightblue/client/request/LightblueRequest.java index 0bbc320..f4e02c1 100644 --- a/core/src/main/java/com/redhat/lightblue/client/request/LightblueRequest.java +++ b/core/src/main/java/com/redhat/lightblue/client/request/LightblueRequest.java @@ -39,6 +39,7 @@ public HttpMethod getHttpMethod() { /** * Requst body as string, defaults to getBodyJson().toString() */ + @Deprecated public String getBody() { JsonNode body=getBodyJson(); if(body!=null) { diff --git a/http/src/main/java/com/redhat/lightblue/client/http/LightblueHttpClient.java b/http/src/main/java/com/redhat/lightblue/client/http/LightblueHttpClient.java index 65e6275..b334cd9 100644 --- a/http/src/main/java/com/redhat/lightblue/client/http/LightblueHttpClient.java +++ b/http/src/main/java/com/redhat/lightblue/client/http/LightblueHttpClient.java @@ -275,7 +275,7 @@ protected HttpResponse callService(LightblueRequest request, String baseUri) thr long t1 = System.currentTimeMillis(); - HttpResponse response = httpTransport.executeRequest(request, baseUri); + HttpResponse response = httpTransport.executeRequest(request, baseUri, mapper); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Response received from service: {}", response.getBody()); diff --git a/http/src/main/java/com/redhat/lightblue/client/http/transport/HttpTransport.java b/http/src/main/java/com/redhat/lightblue/client/http/transport/HttpTransport.java index 71c948e..507b260 100644 --- a/http/src/main/java/com/redhat/lightblue/client/http/transport/HttpTransport.java +++ b/http/src/main/java/com/redhat/lightblue/client/http/transport/HttpTransport.java @@ -5,8 +5,9 @@ import com.redhat.lightblue.client.http.LightblueHttpClientException; import com.redhat.lightblue.client.request.LightblueRequest; -public interface HttpTransport extends Closeable { - - HttpResponse executeRequest(LightblueRequest request, String baseUri) throws LightblueHttpClientException; +import com.fasterxml.jackson.databind.ObjectMapper; +public interface HttpTransport extends Closeable { + HttpResponse executeRequest(LightblueRequest request, String baseUri, ObjectMapper mapper) + throws LightblueHttpClientException; } diff --git a/http/src/main/java/com/redhat/lightblue/client/http/transport/JavaNetHttpTransport.java b/http/src/main/java/com/redhat/lightblue/client/http/transport/JavaNetHttpTransport.java index f3815b7..23ac4f9 100644 --- a/http/src/main/java/com/redhat/lightblue/client/http/transport/JavaNetHttpTransport.java +++ b/http/src/main/java/com/redhat/lightblue/client/http/transport/JavaNetHttpTransport.java @@ -13,9 +13,10 @@ import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSocketFactory; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -114,7 +115,8 @@ public static JavaNetHttpTransport fromLightblueClientConfiguration(LightblueCli } @Override - public HttpResponse executeRequest(LightblueRequest request, String baseUri) throws LightblueHttpClientException { + public HttpResponse executeRequest(LightblueRequest request, String baseUri, ObjectMapper mapper) + throws LightblueHttpClientException { try { String url = request.getRestURI(baseUri); LOGGER.debug("Executing request, url={}", url); @@ -128,7 +130,9 @@ public HttpResponse executeRequest(LightblueRequest request, String baseUri) thr } } - connection.setRequestMethod((request.getHttpMethod() != null ? request.getHttpMethod().toString() : HttpMethod.GET.name())); + connection.setRequestMethod((request.getHttpMethod() != null + ? request.getHttpMethod().toString() + : HttpMethod.GET.name())); connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("Accept-Charset", "utf-8"); @@ -141,13 +145,13 @@ public HttpResponse executeRequest(LightblueRequest request, String baseUri) thr } if (this.compression == Compression.LZF) { - LOGGER.debug("Advertising lzf decoding capabilities to lightblue"); + LOGGER.trace("Advertising lzf decoding capabilities to lightblue"); connection.setRequestProperty("Accept-Encoding", "lzf"); } - String body = request.getBody(); - if (StringUtils.isNotBlank(body)) { - sendRequestBody(body, connection); + JsonNode body = request.getBodyJson(); + if (body != null && !body.isNull()) { + sendRequestBody(body, connection, mapper); } return new HttpResponse(response(connection), connection.getHeaderFields()); @@ -163,19 +167,13 @@ public void close() throws IOException { // Nothing to do } - private void sendRequestBody(String body, HttpURLConnection connection) + private void sendRequestBody(Object body, HttpURLConnection connection, ObjectMapper mapper) throws IOException { - byte[] bodyUtf8Bytes = body.getBytes(UTF_8); - int length = bodyUtf8Bytes.length; - connection.setDoOutput(true); - connection.setFixedLengthStreamingMode(length); - - connection.setRequestProperty("Content-Length", Integer.toString(length)); connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); try (OutputStream requestStream = connection.getOutputStream()) { - requestStream.write(bodyUtf8Bytes); + mapper.writeValue(requestStream, body); } } @@ -214,10 +212,10 @@ private String response(HttpURLConnection connection) throws LightblueHttpClient */ private String readResponseStream(InputStream responseStream, HttpURLConnection connection) throws IOException { - LOGGER.debug("Reading response stream"); + LOGGER.trace("Reading response stream"); InputStream decodedStream = responseStream; if (compression == Compression.LZF && "lzf".equals(connection.getHeaderField("Content-Encoding"))) { - LOGGER.debug("Decoding lzf"); + LOGGER.trace("Decoding lzf"); decodedStream = new LZFInputStream(responseStream); } diff --git a/http/src/test/java/com/redhat/lightblue/client/http/LightblueHttpClientTest.java b/http/src/test/java/com/redhat/lightblue/client/http/LightblueHttpClientTest.java index e35a585..961b0ca 100644 --- a/http/src/test/java/com/redhat/lightblue/client/http/LightblueHttpClientTest.java +++ b/http/src/test/java/com/redhat/lightblue/client/http/LightblueHttpClientTest.java @@ -8,6 +8,7 @@ import java.util.List; import java.util.Map; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Assert; import org.junit.Test; @@ -42,7 +43,8 @@ public void testPojoMapping() throws Exception { String response = "{\"matchCount\": 1, \"modifiedCount\": 0, \"processed\": [{\"_id\": \"idhash\", \"field\":\"value\"}], \"status\": \"COMPLETE\"}"; - when(httpTransport.executeRequest(any(LightblueRequest.class), anyString())).thenReturn(new FakeResponse(response, null)); + when(httpTransport.executeRequest(any(LightblueRequest.class), anyString(), any(ObjectMapper.class))) + .thenReturn(new FakeResponse(response, null)); SimpleModelObject[] results = client.data(findRequest, SimpleModelObject[].class); @@ -56,7 +58,8 @@ public void testPrimitiveMapping() throws Exception { GenerateRequest request = new GenerateRequest("foo", "bar"); request.path("x").nValues(3); String response = "{ \"processed\": [\"1\",\"2\",\"3\"], \"status\": \"COMPLETE\"}"; - when(httpTransport.executeRequest(any(LightblueRequest.class), anyString())).thenReturn(new FakeResponse(response, null)); + when(httpTransport.executeRequest(any(LightblueRequest.class), anyString(), any(ObjectMapper.class))) + .thenReturn(new FakeResponse(response, null)); String[] results = client.data(request, String[].class); @@ -75,7 +78,8 @@ public void testPojoMappingWithParsingError() throws Exception { String response = "{\"processed\":\"

This is not json

\"}"; - when(httpTransport.executeRequest(any(LightblueRequest.class), anyString())).thenReturn(new FakeResponse(response, null)); + when(httpTransport.executeRequest(any(LightblueRequest.class), anyString(), any(ObjectMapper.class))) + .thenReturn(new FakeResponse(response, null)); client.data(findRequest, SimpleModelObject[].class); } @@ -105,7 +109,8 @@ public void testParseInvalidJson() throws Exception { @Test public void testUsingDefaultExecution_ReadPreference() throws Exception { - when(httpTransport.executeRequest(any(LightblueRequest.class), anyString())).thenReturn(new FakeResponse("{}", null)); + when(httpTransport.executeRequest(any(LightblueRequest.class), anyString(), any(ObjectMapper.class))) + .thenReturn(new FakeResponse("{}", null)); LightblueClientConfiguration c = new LightblueClientConfiguration(); c.setReadPreference(ReadPreference.primary); @@ -125,7 +130,8 @@ public void testUsingDefaultExecution_ReadPreference() throws Exception { @Test public void testUsingDefaultExecution_WriteConcern() throws Exception { - when(httpTransport.executeRequest(any(LightblueRequest.class), anyString())).thenReturn(new FakeResponse("{}", null)); + when(httpTransport.executeRequest(any(LightblueRequest.class), anyString(), any(ObjectMapper.class))) + .thenReturn(new FakeResponse("{}", null)); LightblueClientConfiguration c = new LightblueClientConfiguration(); c.setWriteConcern("majority"); @@ -145,7 +151,8 @@ public void testUsingDefaultExecution_WriteConcern() throws Exception { @Test public void testUsingDefaultExecution_MaxQueryTimeMS() throws Exception { - when(httpTransport.executeRequest(any(LightblueRequest.class), anyString())).thenReturn(new FakeResponse("{}", null)); + when(httpTransport.executeRequest(any(LightblueRequest.class), anyString(), any(ObjectMapper.class))) + .thenReturn(new FakeResponse("{}", null)); LightblueClientConfiguration c = new LightblueClientConfiguration(); c.setMaxQueryTimeMS(1000); @@ -168,7 +175,8 @@ public void testUsingDefaultExecution_MaxQueryTimeMS() throws Exception { */ @Test public void testUsingDefaultExecution_OverrideExecution() throws Exception { - when(httpTransport.executeRequest(any(LightblueRequest.class), anyString())).thenReturn(new FakeResponse("{}", null)); + when(httpTransport.executeRequest(any(LightblueRequest.class), anyString(), any(ObjectMapper.class))) + .thenReturn(new FakeResponse("{}", null)); LightblueClientConfiguration c = new LightblueClientConfiguration(); c.setReadPreference(ReadPreference.primary); diff --git a/http/src/test/java/com/redhat/lightblue/client/http/testing/doubles/FakeLightblueRequest.java b/http/src/test/java/com/redhat/lightblue/client/http/testing/doubles/FakeLightblueRequest.java index 7b92ecc..7ff54c2 100644 --- a/http/src/test/java/com/redhat/lightblue/client/http/testing/doubles/FakeLightblueRequest.java +++ b/http/src/test/java/com/redhat/lightblue/client/http/testing/doubles/FakeLightblueRequest.java @@ -23,7 +23,7 @@ public String getBody() { @Override public JsonNode getBodyJson() { - return JSON.toJsonNode(body); + return body == null ? null : JSON.toJsonNode(body); } @Override diff --git a/http/src/test/java/com/redhat/lightblue/client/http/transport/JavaNetHttpTransportCompressionTest.java b/http/src/test/java/com/redhat/lightblue/client/http/transport/JavaNetHttpTransportCompressionTest.java index 7f6ac9d..fd4f890 100644 --- a/http/src/test/java/com/redhat/lightblue/client/http/transport/JavaNetHttpTransportCompressionTest.java +++ b/http/src/test/java/com/redhat/lightblue/client/http/transport/JavaNetHttpTransportCompressionTest.java @@ -12,6 +12,7 @@ import java.io.PrintWriter; import java.net.HttpURLConnection; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -26,6 +27,7 @@ import com.redhat.lightblue.client.http.testing.doubles.FakeLightblueRequest; import com.redhat.lightblue.client.http.transport.JavaNetHttpTransportTest.CallConnectAndReturn; import com.redhat.lightblue.client.request.LightblueRequest; +import com.redhat.lightblue.client.util.JSON; /** * Testing {@link JavaNetHttpTransport}'s lzf compression support. @@ -36,6 +38,8 @@ @RunWith(JUnit4.class) public class JavaNetHttpTransportCompressionTest { + private static final ObjectMapper mapper = JSON.getDefaultObjectMapper(); + private HttpURLConnection mockConnection = mock(HttpURLConnection.class); private JavaNetHttpTransport.ConnectionFactory mockConnectionFactory = mock(JavaNetHttpTransport.ConnectionFactory.class); @@ -51,7 +55,7 @@ public void before() throws IOException { public void shouldSetAcceptEncodingHeaderToLzfByDefault() throws LightblueHttpClientException { LightblueRequest request = new FakeLightblueRequest("", HttpMethod.GET, "/foo/bar"); - client.executeRequest(request, ""); + client.executeRequest(request, "", mapper); Mockito.verify(mockConnection).setRequestProperty("Accept-Encoding", "lzf"); } @@ -61,7 +65,7 @@ public void shouldNotSetAcceptEncodingHeaderWhenCompressionIsDisabled() throws L LightblueRequest request = new FakeLightblueRequest("", HttpMethod.GET, "/foo/bar"); client.setCompression(Compression.NONE); - client.executeRequest(request, ""); + client.executeRequest(request, "", mapper); Mockito.verify(mockConnection, never()).setRequestProperty(Mockito.eq("Accept-Encoding"), Mockito.anyString()); } @@ -84,7 +88,7 @@ public void shouldDecodeResponseWhenCompressionEnabledAndContentEncodingIsDefine when(mockConnection.getInputStream()).thenReturn(inResponseBody); - String response = client.executeRequest(request, "").getBody(); + String response = client.executeRequest(request, "", mapper).getBody(); Assert.assertEquals(TEST_RESPONSE, response); } diff --git a/http/src/test/java/com/redhat/lightblue/client/http/transport/JavaNetHttpTransportIntegrationTest.java b/http/src/test/java/com/redhat/lightblue/client/http/transport/JavaNetHttpTransportIntegrationTest.java index 757db6f..fe470a7 100644 --- a/http/src/test/java/com/redhat/lightblue/client/http/transport/JavaNetHttpTransportIntegrationTest.java +++ b/http/src/test/java/com/redhat/lightblue/client/http/transport/JavaNetHttpTransportIntegrationTest.java @@ -7,6 +7,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; @@ -18,6 +19,7 @@ import com.redhat.lightblue.client.http.LightblueHttpClientException; import com.redhat.lightblue.client.http.testing.doubles.FakeLightblueRequest; import com.redhat.lightblue.client.request.LightblueRequest; +import com.redhat.lightblue.client.util.JSON; /** * Tests the somewhat complicated and vaguely documented semantics of JDK's @@ -28,6 +30,8 @@ */ @RunWith(JUnit4.class) public class JavaNetHttpTransportIntegrationTest { + private static final ObjectMapper mapper = JSON.getDefaultObjectMapper(); + @Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort()); @@ -38,9 +42,9 @@ public void shouldReturnResponseBodyOfSuccessfulRequest() throws LightblueHttpCl wireMockRule.stubFor(any(urlMatching(".*")) .willReturn(aResponse().withBody("The body").withStatus(200))); - LightblueRequest request = new FakeLightblueRequest("", HttpMethod.GET, "/"); + LightblueRequest request = new FakeLightblueRequest(null, HttpMethod.GET, "/"); - String response = client.executeRequest(request, wireMockUrl()).getBody(); + String response = client.executeRequest(request, wireMockUrl(), mapper).getBody(); assertThat(response, is("The body")); } @@ -50,9 +54,9 @@ public void shouldReturnResponseBodyOfUnsuccessfulRequest() throws LightblueHttp wireMockRule.stubFor(any(urlMatching(".*")) .willReturn(aResponse().withBody("The body").withStatus(500))); - LightblueRequest request = new FakeLightblueRequest("", HttpMethod.GET, "/"); + LightblueRequest request = new FakeLightblueRequest(null, HttpMethod.GET, "/"); - Assert.assertEquals("The body", client.executeRequest(request, wireMockUrl()).getBody()); + Assert.assertEquals("The body", client.executeRequest(request, wireMockUrl(), mapper).getBody()); } @Test @@ -60,10 +64,10 @@ public void shouldThrowExceptionOnUnsuccessfulRequestWithNoResponseBody() { wireMockRule.stubFor(any(urlMatching(".*")) .willReturn(aResponse().withStatus(500))); - LightblueRequest request = new FakeLightblueRequest("", HttpMethod.GET, "/"); + LightblueRequest request = new FakeLightblueRequest(null, HttpMethod.GET, "/"); try { - client.executeRequest(request, wireMockUrl()); + client.executeRequest(request, wireMockUrl(), mapper); Assert.fail(); } catch (LightblueHttpClientException e) { Assert.assertEquals(500, e.getHttpStatus()); diff --git a/http/src/test/java/com/redhat/lightblue/client/http/transport/JavaNetHttpTransportTest.java b/http/src/test/java/com/redhat/lightblue/client/http/transport/JavaNetHttpTransportTest.java index d89b6ad..f3def72 100644 --- a/http/src/test/java/com/redhat/lightblue/client/http/transport/JavaNetHttpTransportTest.java +++ b/http/src/test/java/com/redhat/lightblue/client/http/transport/JavaNetHttpTransportTest.java @@ -22,6 +22,7 @@ import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSocketFactory; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -35,9 +36,12 @@ import com.redhat.lightblue.client.http.LightblueHttpClientException; import com.redhat.lightblue.client.http.testing.doubles.FakeLightblueRequest; import com.redhat.lightblue.client.request.LightblueRequest; +import com.redhat.lightblue.client.util.JSON; @RunWith(JUnit4.class) public class JavaNetHttpTransportTest { + private static final ObjectMapper mapper = JSON.getDefaultObjectMapper(); + private HttpURLConnection mockConnection = mock(HttpURLConnection.class); private HttpsURLConnection mockSslConnection = mock(HttpsURLConnection.class); private JavaNetHttpTransport.ConnectionFactory mockConnectionFactory @@ -74,9 +78,9 @@ public void stubMockConnectionFactory() throws Exception { @Test(timeout = 500) public void shouldConnectToUrlOfRequestUsingBaseUri() throws Exception { - LightblueRequest getFooBar = new FakeLightblueRequest("", HttpMethod.GET, "/foo/bar"); + LightblueRequest getFooBar = new FakeLightblueRequest(null, HttpMethod.GET, "/foo/bar"); - client.executeRequest(getFooBar, "http://theblueislight.com"); + client.executeRequest(getFooBar, "http://theblueislight.com", mapper); verify(mockConnectionFactory).openConnection("http://theblueislight.com/foo/bar"); verify(mockConnection).connect(); @@ -84,11 +88,11 @@ public void shouldConnectToUrlOfRequestUsingBaseUri() throws Exception { @Test(timeout = 500) public void shouldSetHttpMethod() throws Exception { - LightblueRequest deleteRequest = new FakeLightblueRequest("", HttpMethod.DELETE, ""); - LightblueRequest getRequest = new FakeLightblueRequest("", HttpMethod.GET, ""); + LightblueRequest deleteRequest = new FakeLightblueRequest(null, HttpMethod.DELETE, ""); + LightblueRequest getRequest = new FakeLightblueRequest(null, HttpMethod.GET, ""); - client.executeRequest(deleteRequest, ""); - client.executeRequest(getRequest, ""); + client.executeRequest(deleteRequest, "", mapper); + client.executeRequest(getRequest, "", mapper); verify(mockConnection).setRequestMethod("DELETE"); verify(mockConnection).setRequestMethod("GET"); @@ -96,9 +100,9 @@ public void shouldSetHttpMethod() throws Exception { @Test(timeout = 500) public void shouldSetHttpMethodBeforeSendingRequestWithBody() throws Exception { - LightblueRequest requestWithBody = new FakeLightblueRequest("Yup", HttpMethod.POST, ""); + LightblueRequest requestWithBody = new FakeLightblueRequest("{\"test\": 123}", HttpMethod.POST, ""); - client.executeRequest(requestWithBody, ""); + client.executeRequest(requestWithBody, "", mapper); InOrder inOrder = inOrder(mockConnection); inOrder.verify(mockConnection).setRequestMethod(anyString()); @@ -107,22 +111,22 @@ public void shouldSetHttpMethodBeforeSendingRequestWithBody() throws Exception { @Test(timeout = 500) public void shouldSendBodyUsingUtf8IfNotEmpty() throws Exception { - LightblueRequest konnichiWa = new FakeLightblueRequest("こんにちは", HttpMethod.POST, ""); + LightblueRequest konnichiWa = new FakeLightblueRequest("{\"test\":\"こんにちは\"}", HttpMethod.POST, ""); - client.executeRequest(konnichiWa, ""); + client.executeRequest(konnichiWa, "", mapper); InOrder inOrder = inOrder(mockConnection); inOrder.verify(mockConnection).setDoOutput(true); inOrder.verify(mockConnection).connect(); - assertThat(requestStream.toString("UTF-8"), is("こんにちは")); + assertThat(requestStream.toString("UTF-8"), is("{\"test\":\"こんにちは\"}")); } @Test(timeout = 500) public void shouldNotSendEmptyBody() throws Exception { - LightblueRequest noBody = new FakeLightblueRequest("", HttpMethod.GET, ""); + LightblueRequest noBody = new FakeLightblueRequest(null, HttpMethod.GET, ""); - client.executeRequest(noBody, ""); + client.executeRequest(noBody, "", mapper); verify(mockConnection, never()).setDoOutput(true); verify(mockConnection, never()).getOutputStream(); @@ -133,9 +137,9 @@ public void shouldUseSslSocketFactoryProvidedIfHttpsConnection() throws Exceptio SSLSocketFactory mockSslSocketFactory = mock(SSLSocketFactory.class); JavaNetHttpTransport client = new JavaNetHttpTransport(mockConnectionFactory, mockSslSocketFactory); - LightblueRequest getFooBar = new FakeLightblueRequest("", HttpMethod.GET, "/foo/bar"); + LightblueRequest getFooBar = new FakeLightblueRequest(null, HttpMethod.GET, "/foo/bar"); - client.executeRequest(getFooBar, "https://much_secure.so_ssl"); + client.executeRequest(getFooBar, "https://much_secure.so_ssl", mapper); InOrder inOrder = inOrder(mockSslConnection); inOrder.verify(mockSslConnection).setSSLSocketFactory(mockSslSocketFactory); @@ -146,28 +150,28 @@ public void shouldUseSslSocketFactoryProvidedIfHttpsConnection() throws Exceptio public void shouldNotUseNullSslSocketFactory_shouldFallBackToDefault() throws Exception { JavaNetHttpTransport client = new JavaNetHttpTransport(mockConnectionFactory, null); - LightblueRequest getFooBar = new FakeLightblueRequest("", HttpMethod.GET, "/foo/bar"); + LightblueRequest getFooBar = new FakeLightblueRequest(null, HttpMethod.GET, "/foo/bar"); - client.executeRequest(getFooBar, "https://much_secure.so_ssl"); + client.executeRequest(getFooBar, "https://much_secure.so_ssl", mapper); verify(mockSslConnection, never()).setSSLSocketFactory(isNull(SSLSocketFactory.class)); } @Test(timeout = 500) public void shouldReturnSuccessResponseDecodedWithUtf8WhenContentLengthIsKnown() throws Exception { - LightblueRequest helloInJapanese = new FakeLightblueRequest("", HttpMethod.GET, "/hello/japanese"); + LightblueRequest helloInJapanese = new FakeLightblueRequest(null, HttpMethod.GET, "/hello/japanese"); String konnichiWa = "こんにちは"; responseStream.print(konnichiWa); when(mockConnection.getContentLength()).thenReturn(konnichiWa.getBytes("UTF-8").length); - assertThat(client.executeRequest(helloInJapanese, "").getBody(), is(konnichiWa)); + assertThat(client.executeRequest(helloInJapanese, "", mapper).getBody(), is(konnichiWa)); } @Test(timeout = 500) public void shouldReturnSuccessResponseDecodedWithUtf8WhenContentLengthIsNotKnown() throws Exception { - LightblueRequest helloInJapanese = new FakeLightblueRequest("", HttpMethod.GET, "/hello/japanese"); + LightblueRequest helloInJapanese = new FakeLightblueRequest(null, HttpMethod.GET, "/hello/japanese"); String konnichiWa = "こんにちは"; @@ -175,21 +179,21 @@ public void shouldReturnSuccessResponseDecodedWithUtf8WhenContentLengthIsNotKnow responseStream.close(); when(mockConnection.getContentLength()).thenReturn(-1); - assertThat(client.executeRequest(helloInJapanese, "").getBody(), is(konnichiWa)); + assertThat(client.executeRequest(helloInJapanese, "", mapper).getBody(), is(konnichiWa)); } @Test(timeout = 500) public void shouldReturnEmptySuccessResponse() throws Exception { - LightblueRequest getFooBar = new FakeLightblueRequest("", HttpMethod.GET, "/foo/bar"); + LightblueRequest getFooBar = new FakeLightblueRequest(null, HttpMethod.GET, "/foo/bar"); when(mockConnection.getContentLength()).thenReturn(0); - assertThat(client.executeRequest(getFooBar, "").getBody(), is("")); + assertThat(client.executeRequest(getFooBar, "", mapper).getBody(), is("")); } @Test(timeout = 500) public void shouldReturnErrorResponseUsingUtf8WhenContentLengthIsKnown() throws Exception { - LightblueRequest badHelloRequest = new FakeLightblueRequest("", HttpMethod.GET, "/hello/%E0%B2%A0_%E0%B2%A0"); + LightblueRequest badHelloRequest = new FakeLightblueRequest(null, HttpMethod.GET, "/hello/%E0%B2%A0_%E0%B2%A0"); String error = "ಠ_ಠ is not a language"; @@ -197,12 +201,12 @@ public void shouldReturnErrorResponseUsingUtf8WhenContentLengthIsKnown() throws errorResponseStream.print(error); when(mockConnection.getContentLength()).thenReturn(error.getBytes("UTF-8").length); - Assert.assertEquals(error, client.executeRequest(badHelloRequest, "").getBody()); + Assert.assertEquals(error, client.executeRequest(badHelloRequest, "", mapper).getBody()); } @Test(timeout = 500) public void shouldReturnErrorResponseUsingUtf8WhenContentLengthIsNotKnown() throws Exception { - LightblueRequest badHelloRequest = new FakeLightblueRequest("", HttpMethod.GET, "/hello/%E0%B2%A0_%E0%B2%A0"); + LightblueRequest badHelloRequest = new FakeLightblueRequest(null, HttpMethod.GET, "/hello/%E0%B2%A0_%E0%B2%A0"); String error = "ಠ_ಠ is not a language"; @@ -211,18 +215,18 @@ public void shouldReturnErrorResponseUsingUtf8WhenContentLengthIsNotKnown() thro errorResponseStream.close(); when(mockConnection.getContentLength()).thenReturn(-1); - Assert.assertEquals(error, client.executeRequest(badHelloRequest, "").getBody()); + Assert.assertEquals(error, client.executeRequest(badHelloRequest, "", mapper).getBody()); } @Test(timeout = 500) public void shouldThrowExceptionOnEmptyErrorResponse() throws Exception { - LightblueRequest badHelloRequest = new FakeLightblueRequest("", HttpMethod.GET, "/hello/%E0%B2%A0_%E0%B2%A0"); + LightblueRequest badHelloRequest = new FakeLightblueRequest(null, HttpMethod.GET, "/hello/%E0%B2%A0_%E0%B2%A0"); doThrow(new IOException()).when(mockConnection).getInputStream(); when(mockConnection.getContentLength()).thenReturn(0); try { - client.executeRequest(badHelloRequest, ""); + client.executeRequest(badHelloRequest, "", mapper); Assert.fail(); } catch (LightblueHttpClientException e) { Assert.assertEquals("", e.getHttpResponseBody()); @@ -231,20 +235,21 @@ public void shouldThrowExceptionOnEmptyErrorResponse() throws Exception { @Test(timeout = 500) public void shouldSetContentLengthHeaderBasedOnUtf8BytesInRequest() throws Exception { - LightblueRequest newHello = new FakeLightblueRequest("ಠ_ಠ", HttpMethod.POST, "/hello/facelang"); + LightblueRequest newHello = new FakeLightblueRequest("{\"face\": \"ಠ_ಠ\"}", HttpMethod.POST, "/hello/facelang"); - client.executeRequest(newHello, ""); + client.executeRequest(newHello, "", mapper); InOrder inOrder = inOrder(mockConnection); - inOrder.verify(mockConnection).setRequestProperty("Content-Length", Integer.toString("ಠ_ಠ".getBytes("UTF-8").length)); + inOrder.verify(mockConnection).setRequestProperty( + "Content-Length", Integer.toString("{\"face\": \"ಠ_ಠ\"}".getBytes("UTF-8").length)); inOrder.verify(mockConnection).connect(); } @Test(timeout = 500) public void shouldSetContentTypeToApplicationJsonWithUtf8CharsetWhenMakingRequestWithBody() throws Exception { - LightblueRequest newHello = new FakeLightblueRequest("ಠ_ಠ", HttpMethod.POST, "/hello/facelang"); + LightblueRequest newHello = new FakeLightblueRequest("{\"face\": \"ಠ_ಠ\"}", HttpMethod.POST, "/hello/facelang"); - client.executeRequest(newHello, ""); + client.executeRequest(newHello, "", mapper); InOrder inOrder = inOrder(mockConnection); inOrder.verify(mockConnection).setRequestProperty("Content-Type", "application/json; charset=utf-8"); @@ -253,9 +258,9 @@ public void shouldSetContentTypeToApplicationJsonWithUtf8CharsetWhenMakingReques @Test(timeout = 500) public void shouldSetAcceptHeaderToJsonBeforeMakingRequest() throws Exception { - LightblueRequest request = new FakeLightblueRequest("", HttpMethod.GET, "/foo/bar"); + LightblueRequest request = new FakeLightblueRequest(null, HttpMethod.GET, "/foo/bar"); - client.executeRequest(request, ""); + client.executeRequest(request, "", mapper); InOrder inOrder = inOrder(mockConnection); inOrder.verify(mockConnection).setRequestProperty("Accept", "application/json"); @@ -264,9 +269,9 @@ public void shouldSetAcceptHeaderToJsonBeforeMakingRequest() throws Exception { @Test(timeout = 500) public void shouldSetAcceptCharsetHeaderToUtf8BeforeMakingRequest() throws Exception { - LightblueRequest request = new FakeLightblueRequest("", HttpMethod.GET, "/foo/bar"); + LightblueRequest request = new FakeLightblueRequest(null, HttpMethod.GET, "/foo/bar"); - client.executeRequest(request, ""); + client.executeRequest(request, "", mapper); InOrder inOrder = inOrder(mockConnection); inOrder.verify(mockConnection).setRequestProperty("Accept-Charset", "utf-8"); @@ -278,9 +283,9 @@ public void shouldUseBasicAuthIfCredentialsAreProvided() throws Exception { JavaNetHttpTransport client = new JavaNetHttpTransport(mockConnectionFactory, null, null, "Aladdin", "OpenSesame"); - LightblueRequest request = new FakeLightblueRequest("", HttpMethod.GET, "/foo/bar"); + LightblueRequest request = new FakeLightblueRequest(null, HttpMethod.GET, "/foo/bar"); - client.executeRequest(request, ""); + client.executeRequest(request, "", mapper); InOrder inOrder = inOrder(mockConnection); inOrder.verify(mockConnection).setRequestProperty("Authorization", "Basic QWxhZGRpbjpPcGVuU2VzYW1l");