Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non-null ResponseBody for responses with empty body #1585

Merged
merged 2 commits into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Response> handler = invocation.getArgument(1);
Expand Down Expand Up @@ -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);
}
Expand Down