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
  • Loading branch information
gromspys authored and velo committed Aug 19, 2024
1 parent 376b0a2 commit 74f542e
Show file tree
Hide file tree
Showing 2 changed files with 40 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 @@ -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

0 comments on commit 74f542e

Please sign in to comment.