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

Client.Default - Null pointer exception when receiving an 'empty' response with compression #2510

Merged
merged 2 commits into from
Aug 21, 2024
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
4 changes: 2 additions & 2 deletions core/src/main/java/feign/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ Response convertResponse(HttpURLConnection connection, Request request) throws I
} else {
stream = connection.getInputStream();
}
if (this.isGzip(headers.get(CONTENT_ENCODING))) {
if (stream != null && this.isGzip(headers.get(CONTENT_ENCODING))) {
stream = new GZIPInputStream(stream);
} else if (this.isDeflate(headers.get(CONTENT_ENCODING))) {
} else if (stream != null && this.isDeflate(headers.get(CONTENT_ENCODING))) {
stream = new InflaterInputStream(stream);
}
return Response.builder()
Expand Down
38 changes: 38 additions & 0 deletions core/src/test/java/feign/client/AbstractClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,25 @@ public void canSupportGzipOnError() throws Exception {

}

@Test
public void canSupportGzipOnErrorWithoutBody() throws Exception {
server.enqueue(new MockResponse().setResponseCode(400)
.addHeader("Content-Encoding", "gzip"));

TestInterface api =
newBuilder().target(TestInterface.class, "http://localhost:" + server.getPort());

try {
api.get();
fail("Expect FeignException");
} catch (FeignException e) {
/* verify that the response is unzipped */
assertThat(e.responseBody()).isNotEmpty()
.map(body -> new String(body.array(), StandardCharsets.UTF_8))
.get().isEqualTo("");
}
}

@Test
public void canSupportDeflate() throws Exception {
/* enqueue a zipped response */
Expand Down Expand Up @@ -456,6 +475,25 @@ public void canSupportDeflateOnError() throws Exception {
}
}

@Test
public void canSupportDeflateOnErrorWithoutBody() throws Exception {
server.enqueue(new MockResponse().setResponseCode(400)
.addHeader("Content-Encoding", "deflate"));

TestInterface api =
newBuilder().target(TestInterface.class, "http://localhost:" + server.getPort());

try {
api.get();
fail("Expect FeignException");
} catch (FeignException e) {
/* verify that the response is unzipped */
assertThat(e.responseBody()).isNotEmpty()
.map(body -> new String(body.array(), StandardCharsets.UTF_8))
.get().isEqualTo("");
}
}

@Test
public void canExceptCaseInsensitiveHeader() throws Exception {
/* enqueue a zipped response */
Expand Down
Loading