From 5d92174d686d3f1a608bb1d42de25d3f615413dd Mon Sep 17 00:00:00 2001 From: Sergei Korneev <23792@mail.ru> Date: Wed, 21 Aug 2024 12:05:16 +0200 Subject: [PATCH] Client.Default - Null pointer exception when receiving an 'empty' response with compression (#2510) * Client.Default - Null pointer exception when receiving an 'empty' response with compression * Apply code style --- core/src/main/java/feign/Client.java | 4 +- .../java/feign/client/AbstractClientTest.java | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/feign/Client.java b/core/src/main/java/feign/Client.java index 0172b3d6e..93a17dd90 100644 --- a/core/src/main/java/feign/Client.java +++ b/core/src/main/java/feign/Client.java @@ -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() diff --git a/core/src/test/java/feign/client/AbstractClientTest.java b/core/src/test/java/feign/client/AbstractClientTest.java index 90ad61e0c..2b6256234 100644 --- a/core/src/test/java/feign/client/AbstractClientTest.java +++ b/core/src/test/java/feign/client/AbstractClientTest.java @@ -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 */ @@ -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 */