Skip to content

Commit

Permalink
Client.Default - Null pointer exception when receiving an 'empty' res…
Browse files Browse the repository at this point in the history
…ponse with compression (#2510)

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

* Apply code style
  • Loading branch information
gromspys authored Aug 21, 2024
1 parent b86d297 commit 9347d2b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
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 @@ -137,9 +137,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
41 changes: 41 additions & 0 deletions core/src/test/java/feign/client/AbstractClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,26 @@ 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 @@ -489,6 +509,27 @@ 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

0 comments on commit 9347d2b

Please sign in to comment.