diff --git a/core/src/main/java/feign/Client.java b/core/src/main/java/feign/Client.java index 0227fc32b..20aeeae0d 100644 --- a/core/src/main/java/feign/Client.java +++ b/core/src/main/java/feign/Client.java @@ -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() diff --git a/core/src/test/java/feign/client/AbstractClientTest.java b/core/src/test/java/feign/client/AbstractClientTest.java index 578bfbd21..3fb210379 100644 --- a/core/src/test/java/feign/client/AbstractClientTest.java +++ b/core/src/test/java/feign/client/AbstractClientTest.java @@ -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 */ @@ -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 */