Skip to content
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 @@ -382,8 +382,10 @@ private HttpResponse toHttpResponse(java.net.http.HttpResponse<InputStream> resp
if (contentEncoding != null
&& !"gzip".equalsIgnoreCase(contentEncoding)
&& !"identity".equalsIgnoreCase(contentEncoding)) {
throw new UnsupportedContentEncodingException(
"Unsupported Content-Encoding: " + contentEncoding);
try (InputStream ignored = response.body()) {
throw new UnsupportedContentEncodingException(
"Unsupported Content-Encoding: " + contentEncoding);
}
}
boolean decompress = "gzip".equalsIgnoreCase(contentEncoding);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,51 @@ void send_successfulResponse_callsOnResponse() throws Exception {
}
}

@SuppressWarnings("unchecked")
@Test
void sendInternal_unsupportedContentEncoding_closesBody() throws Exception {
AtomicBoolean closed = new AtomicBoolean();
java.net.http.HttpResponse<InputStream> mockJdkResponse =
mock(java.net.http.HttpResponse.class);
when(mockJdkResponse.statusCode()).thenReturn(200);
when(mockJdkResponse.body())
.thenReturn(
new ByteArrayInputStream(new byte[0]) {
@Override
public void close() throws IOException {
closed.set(true);
super.close();
}
});
when(mockJdkResponse.headers())
.thenReturn(
HttpHeaders.of(
Collections.singletonMap("Content-Encoding", Collections.singletonList("br")),
(a, b) -> true));
doReturn(mockJdkResponse).when(mockHttpClient).send(any(), any());

JdkHttpSender testSender =
new JdkHttpSender(
mockHttpClient,
URI.create("http://localhost"),
"text/plain",
null,
Duration.ofSeconds(10),
Collections::emptyMap,
null,
null,
Long.MAX_VALUE);

try {
assertThatThrownBy(() -> testSender.sendInternal(new NoOpRequestBodyWriter()))
.isInstanceOf(IOException.class)
.hasMessage("Unsupported Content-Encoding: br");
assertThat(closed.get()).isTrue();
} finally {
testSender.shutdown();
}
}

@Test
void send_ioException_callsOnError() throws Exception {
doThrow(new IOException("send failed")).when(mockHttpClient).send(any(), any());
Expand Down
Loading