diff --git a/extras/retrofit2/src/main/java/org/asynchttpclient/extras/retrofit/AsyncHttpClientCall.java b/extras/retrofit2/src/main/java/org/asynchttpclient/extras/retrofit/AsyncHttpClientCall.java index e8980fddab..b9517f9fb1 100644 --- a/extras/retrofit2/src/main/java/org/asynchttpclient/extras/retrofit/AsyncHttpClientCall.java +++ b/extras/retrofit2/src/main/java/org/asynchttpclient/extras/retrofit/AsyncHttpClientCall.java @@ -45,6 +45,8 @@ class AsyncHttpClientCall implements Cloneable, okhttp3.Call { * @see #executeTimeoutMillis */ public static final long DEFAULT_EXECUTE_TIMEOUT_MILLIS = 30_000; + + private static final ResponseBody EMPTY_BODY = ResponseBody.create(null, ""); /** * Tells whether call has been executed. * @@ -254,6 +256,8 @@ private Response toOkhttpResponse(org.asynchttpclient.Response asyncHttpClientRe ? null : MediaType.parse(asyncHttpClientResponse.getContentType()); val okHttpBody = ResponseBody.create(contentType, asyncHttpClientResponse.getResponseBodyAsBytes()); rspBuilder.body(okHttpBody); + } else { + rspBuilder.body(EMPTY_BODY); } return rspBuilder.build(); diff --git a/extras/retrofit2/src/test/java/org/asynchttpclient/extras/retrofit/AsyncHttpClientCallTest.java b/extras/retrofit2/src/test/java/org/asynchttpclient/extras/retrofit/AsyncHttpClientCallTest.java index 90ab4b33e5..84ed4ddf47 100644 --- a/extras/retrofit2/src/test/java/org/asynchttpclient/extras/retrofit/AsyncHttpClientCallTest.java +++ b/extras/retrofit2/src/test/java/org/asynchttpclient/extras/retrofit/AsyncHttpClientCallTest.java @@ -41,6 +41,7 @@ import static org.mockito.Matchers.any; import static org.mockito.Mockito.*; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotEquals; import static org.testng.Assert.assertTrue; public class AsyncHttpClientCallTest { @@ -281,6 +282,19 @@ public void contentTypeIsProperlyParsedIfPresent() throws Exception { } + @Test + public void bodyIsNotNullInResponse() throws Exception { + AsyncHttpClient client = mock(AsyncHttpClient.class); + + givenResponseIsProduced(client, responseWithNoBody()); + + okhttp3.Response response = whenRequestIsMade(client, REQUEST); + + assertEquals(response.code(), 200); + assertEquals(response.header("Server"), "nginx"); + assertNotEquals(response.body(), null); + } + private void givenResponseIsProduced(AsyncHttpClient client, Response response) { when(client.executeRequest(any(org.asynchttpclient.Request.class), any())).thenAnswer(invocation -> { AsyncCompletionHandler handler = invocation.getArgument(1); @@ -323,6 +337,13 @@ private Response responseWithBody(String contentType, String content) { return response; } + private Response responseWithNoBody() { + Response response = aResponse(); + when(response.hasResponseBody()).thenReturn(false); + when(response.getContentType()).thenReturn(null); + return response; + } + private void doThrow(String message) { throw new RuntimeException(message); }