Skip to content

Commit e3bbad7

Browse files
OKonevaslandelle
authored andcommitted
Add non-null ResponseBody for responses with empty body (AsyncHttpClient#1585), close AsyncHttpClient#1568
* Add non-null ResponseBody for responses with empty body * Introduced empty ResponseBody as a constant
1 parent cabac4a commit e3bbad7

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

extras/retrofit2/src/main/java/org/asynchttpclient/extras/retrofit/AsyncHttpClientCall.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class AsyncHttpClientCall implements Cloneable, okhttp3.Call {
4545
* @see #executeTimeoutMillis
4646
*/
4747
public static final long DEFAULT_EXECUTE_TIMEOUT_MILLIS = 30_000;
48+
49+
private static final ResponseBody EMPTY_BODY = ResponseBody.create(null, "");
4850
/**
4951
* Tells whether call has been executed.
5052
*
@@ -254,6 +256,8 @@ private Response toOkhttpResponse(org.asynchttpclient.Response asyncHttpClientRe
254256
? null : MediaType.parse(asyncHttpClientResponse.getContentType());
255257
val okHttpBody = ResponseBody.create(contentType, asyncHttpClientResponse.getResponseBodyAsBytes());
256258
rspBuilder.body(okHttpBody);
259+
} else {
260+
rspBuilder.body(EMPTY_BODY);
257261
}
258262

259263
return rspBuilder.build();

extras/retrofit2/src/test/java/org/asynchttpclient/extras/retrofit/AsyncHttpClientCallTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import static org.mockito.Matchers.any;
4242
import static org.mockito.Mockito.*;
4343
import static org.testng.Assert.assertEquals;
44+
import static org.testng.Assert.assertNotEquals;
4445
import static org.testng.Assert.assertTrue;
4546

4647
public class AsyncHttpClientCallTest {
@@ -281,6 +282,19 @@ public void contentTypeIsProperlyParsedIfPresent() throws Exception {
281282

282283
}
283284

285+
@Test
286+
public void bodyIsNotNullInResponse() throws Exception {
287+
AsyncHttpClient client = mock(AsyncHttpClient.class);
288+
289+
givenResponseIsProduced(client, responseWithNoBody());
290+
291+
okhttp3.Response response = whenRequestIsMade(client, REQUEST);
292+
293+
assertEquals(response.code(), 200);
294+
assertEquals(response.header("Server"), "nginx");
295+
assertNotEquals(response.body(), null);
296+
}
297+
284298
private void givenResponseIsProduced(AsyncHttpClient client, Response response) {
285299
when(client.executeRequest(any(org.asynchttpclient.Request.class), any())).thenAnswer(invocation -> {
286300
AsyncCompletionHandler<Response> handler = invocation.getArgument(1);
@@ -323,6 +337,13 @@ private Response responseWithBody(String contentType, String content) {
323337
return response;
324338
}
325339

340+
private Response responseWithNoBody() {
341+
Response response = aResponse();
342+
when(response.hasResponseBody()).thenReturn(false);
343+
when(response.getContentType()).thenReturn(null);
344+
return response;
345+
}
346+
326347
private void doThrow(String message) {
327348
throw new RuntimeException(message);
328349
}

0 commit comments

Comments
 (0)