Skip to content

Commit d7ae6eb

Browse files
authored
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
1 parent 6942cd4 commit d7ae6eb

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

core/src/main/java/feign/Client.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ Response convertResponse(HttpURLConnection connection, Request request) throws I
134134
} else {
135135
stream = connection.getInputStream();
136136
}
137-
if (this.isGzip(headers.get(CONTENT_ENCODING))) {
137+
if (stream != null && this.isGzip(headers.get(CONTENT_ENCODING))) {
138138
stream = new GZIPInputStream(stream);
139-
} else if (this.isDeflate(headers.get(CONTENT_ENCODING))) {
139+
} else if (stream != null && this.isDeflate(headers.get(CONTENT_ENCODING))) {
140140
stream = new InflaterInputStream(stream);
141141
}
142142
return Response.builder()

core/src/test/java/feign/client/AbstractClientTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,25 @@ public void canSupportGzipOnError() throws Exception {
419419

420420
}
421421

422+
@Test
423+
public void canSupportGzipOnErrorWithoutBody() throws Exception {
424+
server.enqueue(new MockResponse().setResponseCode(400)
425+
.addHeader("Content-Encoding", "gzip"));
426+
427+
TestInterface api =
428+
newBuilder().target(TestInterface.class, "http://localhost:" + server.getPort());
429+
430+
try {
431+
api.get();
432+
fail("Expect FeignException");
433+
} catch (FeignException e) {
434+
/* verify that the response is unzipped */
435+
assertThat(e.responseBody()).isNotEmpty()
436+
.map(body -> new String(body.array(), StandardCharsets.UTF_8))
437+
.get().isEqualTo("");
438+
}
439+
}
440+
422441
@Test
423442
public void canSupportDeflate() throws Exception {
424443
/* enqueue a zipped response */
@@ -456,6 +475,25 @@ public void canSupportDeflateOnError() throws Exception {
456475
}
457476
}
458477

478+
@Test
479+
public void canSupportDeflateOnErrorWithoutBody() throws Exception {
480+
server.enqueue(new MockResponse().setResponseCode(400)
481+
.addHeader("Content-Encoding", "deflate"));
482+
483+
TestInterface api =
484+
newBuilder().target(TestInterface.class, "http://localhost:" + server.getPort());
485+
486+
try {
487+
api.get();
488+
fail("Expect FeignException");
489+
} catch (FeignException e) {
490+
/* verify that the response is unzipped */
491+
assertThat(e.responseBody()).isNotEmpty()
492+
.map(body -> new String(body.array(), StandardCharsets.UTF_8))
493+
.get().isEqualTo("");
494+
}
495+
}
496+
459497
@Test
460498
public void canExceptCaseInsensitiveHeader() throws Exception {
461499
/* enqueue a zipped response */

0 commit comments

Comments
 (0)