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

Release/2.8.0 #580

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
ensure async response is closed
jimmyjames committed Nov 3, 2023
commit 6d01c8875a3b471a1fbfdc242b0bba36f6e997cd
2 changes: 2 additions & 0 deletions src/main/java/com/auth0/net/client/DefaultHttpClient.java
Original file line number Diff line number Diff line change
@@ -100,6 +100,8 @@ public void onResponse(@NotNull Call call, @NotNull Response response) {
future.complete(buildResponse(response));
} catch (IOException e) {
future.completeExceptionally(e);
} finally {
response.close();
}
}
});
55 changes: 55 additions & 0 deletions src/test/java/com/auth0/net/client/DefaultHttpClientTest.java
Original file line number Diff line number Diff line change
@@ -462,6 +462,31 @@ public void alwaysCloseResponseOnSuccessfulRequest() throws IOException {
verify(response, times(1)).close();
}

@Test
public void alwaysCloseResponseOnSuccessfulRequestAsync() throws Exception {
OkHttpClient okClient = Mockito.mock(OkHttpClient.class);
okhttp3.Response response = Mockito.mock(okhttp3.Response.class);
Call call = Mockito.mock(Call.class);

doReturn(call).when(okClient).newCall(any());

doAnswer(invocation -> {
((Callback) invocation.getArgument(0)).onResponse(call, response);
return null;
}).when(call).enqueue(any(Callback.class));

Headers headers = Mockito.mock(Headers.class);
when(response.headers()).thenReturn(headers);

DefaultHttpClient client = new DefaultHttpClient(okClient);
Auth0HttpRequest request = Auth0HttpRequest.newBuilder(server.url("/users/").toString(), HttpMethod.POST)
.withBody(HttpRequestBody.create("application/json", "{}".getBytes()))
.build();

client.sendRequestAsync(request).get();
verify(response, times(1)).close();
}

@Test
public void closesResponseOnAPIError() throws Exception {
okhttp3.Response response = Mockito.mock(okhttp3.Response.class);
@@ -490,6 +515,36 @@ public void closesResponseOnAPIError() throws Exception {
verify(response, times(1)).close();
}

@Test
public void closesResponseOnAPIErrorAsync() throws Exception {
OkHttpClient okClient = Mockito.mock(OkHttpClient.class);
okhttp3.Response response = Mockito.mock(okhttp3.Response.class);
Call call = Mockito.mock(Call.class);

doReturn(call).when(okClient).newCall(any());

doAnswer(invocation -> {
((Callback) invocation.getArgument(0)).onResponse(call, response);
return null;
}).when(call).enqueue(any(Callback.class));

Headers headers = Mockito.mock(Headers.class);
when(response.headers()).thenReturn(headers);

DefaultHttpClient client = new DefaultHttpClient(okClient);
Auth0HttpRequest request = Auth0HttpRequest.newBuilder(server.url("/users/").toString(), HttpMethod.POST)
.withBody(HttpRequestBody.create("application/json", "{}".getBytes()))
.build();

MockResponse mockResponse = new MockResponse()
.setResponseCode(500)
.setBody("server error");

server.enqueue(mockResponse);
client.sendRequestAsync(request).get();
verify(response, times(1)).close();
}

@Test
public void makesFormBodyPostRequest() throws Exception {
Map<String, Object> params = new HashMap<>();