Skip to content

Commit b500a05

Browse files
gdufrenevelo
andauthored
Unzip/Deflate content on error status for Default Client (#2184)
* Unzip/Deflate content on error status for Default Client * Fix assert message --------- Co-authored-by: Marvin Froeder <[email protected]>
1 parent dacb086 commit b500a05

File tree

5 files changed

+89
-7
lines changed

5 files changed

+89
-7
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,12 @@ Response convertResponse(HttpURLConnection connection, Request request) throws I
132132
if (status >= 400) {
133133
stream = connection.getErrorStream();
134134
} else {
135-
if (this.isGzip(headers.get(CONTENT_ENCODING))) {
136-
stream = new GZIPInputStream(connection.getInputStream());
137-
} else if (this.isDeflate(headers.get(CONTENT_ENCODING))) {
138-
stream = new InflaterInputStream(connection.getInputStream());
139-
} else {
140-
stream = connection.getInputStream();
141-
}
135+
stream = connection.getInputStream();
136+
}
137+
if (this.isGzip(headers.get(CONTENT_ENCODING))) {
138+
stream = new GZIPInputStream(stream);
139+
} else if (this.isDeflate(headers.get(CONTENT_ENCODING))) {
140+
stream = new InflaterInputStream(stream);
142141
}
143142
return Response.builder()
144143
.status(status)

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.assertj.core.api.Assertions.assertThat;
1818
import static org.assertj.core.api.Assertions.entry;
1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.fail;
2021
import feign.Client;
2122
import feign.CollectionFormat;
2223
import feign.Feign.Builder;
@@ -31,6 +32,7 @@
3132
import java.io.ByteArrayInputStream;
3233
import java.io.ByteArrayOutputStream;
3334
import java.io.IOException;
35+
import java.nio.ByteBuffer;
3436
import java.nio.charset.StandardCharsets;
3537
import java.util.Arrays;
3638
import java.util.Collections;
@@ -410,6 +412,32 @@ public void canSupportGzip() throws Exception {
410412
.isEqualToIgnoringCase(responseData);
411413
}
412414

415+
@Test
416+
public void canSupportGzipOnError() throws Exception {
417+
/* enqueue a zipped response */
418+
final String responseData = "Compressed Data";
419+
server.enqueue(new MockResponse()
420+
.setResponseCode(400)
421+
.addHeader("Content-Encoding", "gzip")
422+
.setBody(new Buffer().write(compress(responseData))));
423+
424+
TestInterface api = newBuilder()
425+
.target(TestInterface.class, "http://localhost:" + server.getPort());
426+
427+
try {
428+
api.get();
429+
fail("Expect FeignException");
430+
} catch (FeignException e) {
431+
/* verify that the response is unzipped */
432+
assertThat(e.responseBody())
433+
.isNotEmpty()
434+
.map(body -> new String(body.array(), StandardCharsets.UTF_8))
435+
.get()
436+
.isEqualTo(responseData);
437+
}
438+
439+
}
440+
413441
@Test
414442
public void canSupportDeflate() throws Exception {
415443
/* enqueue a zipped response */
@@ -428,6 +456,31 @@ public void canSupportDeflate() throws Exception {
428456
.isEqualToIgnoringCase(responseData);
429457
}
430458

459+
@Test
460+
public void canSupportDeflateOnError() throws Exception {
461+
/* enqueue a zipped response */
462+
final String responseData = "Compressed Data";
463+
server.enqueue(new MockResponse()
464+
.setResponseCode(400)
465+
.addHeader("Content-Encoding", "deflate")
466+
.setBody(new Buffer().write(deflate(responseData))));
467+
468+
TestInterface api = newBuilder()
469+
.target(TestInterface.class, "http://localhost:" + server.getPort());
470+
471+
try {
472+
api.get();
473+
fail("Expect FeignException");
474+
} catch (FeignException e) {
475+
/* verify that the response is unzipped */
476+
assertThat(e.responseBody())
477+
.isNotEmpty()
478+
.map(body -> new String(body.array(), StandardCharsets.UTF_8))
479+
.get()
480+
.isEqualTo(responseData);
481+
}
482+
}
483+
431484
@Test
432485
public void canExceptCaseInsensitiveHeader() throws Exception {
433486
/* enqueue a zipped response */

googlehttpclient/src/test/java/feign/googlehttpclient/GoogleHttpClientTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,21 @@ public void canSupportGzip() throws Exception {
5454
assumeFalse("Google HTTP client client do not support gzip compression", false);
5555
}
5656

57+
@Override
58+
public void canSupportGzipOnError() throws Exception {
59+
assumeFalse("Google HTTP client client do not support gzip compression", false);
60+
}
61+
5762
@Override
5863
public void canSupportDeflate() throws Exception {
5964
assumeFalse("Google HTTP client client do not support deflate compression", false);
6065
}
6166

67+
@Override
68+
public void canSupportDeflateOnError() throws Exception {
69+
assumeFalse("Google HTTP client client do not support deflate compression", false);
70+
}
71+
6272
@Override
6373
public void canExceptCaseInsensitiveHeader() throws Exception {
6474
assumeFalse("Google HTTP client client do not support gzip compression", false);

jaxrs2/src/test/java/feign/jaxrs2/JAXRSClientTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,21 @@ public void canSupportGzip() throws Exception {
152152
assumeFalse("JaxRS client do not support gzip compression", false);
153153
}
154154

155+
@Override
156+
public void canSupportGzipOnError() throws Exception {
157+
assumeFalse("JaxRS client do not support gzip compression", false);
158+
}
159+
155160
@Override
156161
public void canSupportDeflate() throws Exception {
157162
assumeFalse("JaxRS client do not support deflate compression", false);
158163
}
159164

165+
@Override
166+
public void canSupportDeflateOnError() throws Exception {
167+
assumeFalse("JaxRS client do not support deflate compression", false);
168+
}
169+
160170
@Override
161171
public void canExceptCaseInsensitiveHeader() throws Exception {
162172
assumeFalse("JaxRS client do not support gzip compression", false);

okhttp/src/test/java/feign/okhttp/OkHttpClientTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,21 @@ public void canSupportGzip() throws Exception {
115115
assumeFalse("OkHTTP client do not support gzip compression", false);
116116
}
117117

118+
@Override
119+
public void canSupportGzipOnError() throws Exception {
120+
assumeFalse("OkHTTP client do not support gzip compression", false);
121+
}
122+
118123
@Override
119124
public void canSupportDeflate() throws Exception {
120125
assumeFalse("OkHTTP client do not support deflate compression", false);
121126
}
122127

128+
@Override
129+
public void canSupportDeflateOnError() throws Exception {
130+
assumeFalse("OkHTTP client do not support deflate compression", false);
131+
}
132+
123133
@Override
124134
public void canExceptCaseInsensitiveHeader() throws Exception {
125135
assumeFalse("OkHTTP client do not support gzip compression", false);

0 commit comments

Comments
 (0)