Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unzip/Deflate content on error status for Default Client #2184

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions core/src/main/java/feign/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,12 @@ Response convertResponse(HttpURLConnection connection, Request request) throws I
if (status >= 400) {
stream = connection.getErrorStream();
} else {
if (this.isGzip(headers.get(CONTENT_ENCODING))) {
stream = new GZIPInputStream(connection.getInputStream());
} else if (this.isDeflate(headers.get(CONTENT_ENCODING))) {
stream = new InflaterInputStream(connection.getInputStream());
} else {
stream = connection.getInputStream();
}
stream = connection.getInputStream();
}
if (this.isGzip(headers.get(CONTENT_ENCODING))) {
stream = new GZIPInputStream(stream);
} else if (this.isDeflate(headers.get(CONTENT_ENCODING))) {
stream = new InflaterInputStream(stream);
}
gdufrene marked this conversation as resolved.
Show resolved Hide resolved
return Response.builder()
.status(status)
Expand Down
53 changes: 53 additions & 0 deletions core/src/test/java/feign/client/AbstractClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import feign.Client;
import feign.CollectionFormat;
import feign.Feign.Builder;
Expand All @@ -31,6 +32,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -410,6 +412,32 @@ public void canSupportGzip() throws Exception {
.isEqualToIgnoringCase(responseData);
}

@Test
public void canSupportGzipOnError() throws Exception {
/* enqueue a zipped response */
final String responseData = "Compressed Data";
server.enqueue(new MockResponse()
.setResponseCode(400)
.addHeader("Content-Encoding", "gzip")
.setBody(new Buffer().write(compress(responseData))));

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(responseData);
}

}

@Test
public void canSupportDeflate() throws Exception {
/* enqueue a zipped response */
Expand All @@ -428,6 +456,31 @@ public void canSupportDeflate() throws Exception {
.isEqualToIgnoringCase(responseData);
}

@Test
public void canSupportDeflateOnError() throws Exception {
/* enqueue a zipped response */
final String responseData = "Compressed Data";
server.enqueue(new MockResponse()
.setResponseCode(400)
.addHeader("Content-Encoding", "deflate")
.setBody(new Buffer().write(deflate(responseData))));

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(responseData);
}
}

@Test
public void canExceptCaseInsensitiveHeader() throws Exception {
/* enqueue a zipped response */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,21 @@ public void canSupportGzip() throws Exception {
assumeFalse("Google HTTP client client do not support gzip compression", false);
}

@Override
public void canSupportGzipOnError() throws Exception {
assumeFalse("Google HTTP client client do not support gzip compression", false);
}

@Override
public void canSupportDeflate() throws Exception {
assumeFalse("Google HTTP client client do not support deflate compression", false);
}

@Override
public void canSupportDeflateOnError() throws Exception {
assumeFalse("Google HTTP client client do not support deflate compression", false);
}

@Override
public void canExceptCaseInsensitiveHeader() throws Exception {
assumeFalse("Google HTTP client client do not support gzip compression", false);
Expand Down
10 changes: 10 additions & 0 deletions jaxrs2/src/test/java/feign/jaxrs2/JAXRSClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,21 @@ public void canSupportGzip() throws Exception {
assumeFalse("JaxRS client do not support gzip compression", false);
}

@Override
public void canSupportGzipOnError() throws Exception {
assumeFalse("JaxRS client do not support gzip compression", false);
}

@Override
public void canSupportDeflate() throws Exception {
assumeFalse("JaxRS client do not support deflate compression", false);
}

@Override
public void canSupportDeflateOnError() throws Exception {
assumeFalse("JaxRS client do not support deflate compression", false);
}

@Override
public void canExceptCaseInsensitiveHeader() throws Exception {
assumeFalse("JaxRS client do not support gzip compression", false);
gdufrene marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
10 changes: 10 additions & 0 deletions okhttp/src/test/java/feign/okhttp/OkHttpClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,21 @@ public void canSupportGzip() throws Exception {
assumeFalse("OkHTTP client do not support gzip compression", false);
}

@Override
public void canSupportGzipOnError() throws Exception {
assumeFalse("OkHTTP client do not support gzip compression", false);
}
gdufrene marked this conversation as resolved.
Show resolved Hide resolved

@Override
public void canSupportDeflate() throws Exception {
assumeFalse("OkHTTP client do not support deflate compression", false);
}

@Override
public void canSupportDeflateOnError() throws Exception {
assumeFalse("OkHTTP client do not support deflate compression", false);
}
gdufrene marked this conversation as resolved.
Show resolved Hide resolved

@Override
public void canExceptCaseInsensitiveHeader() throws Exception {
gdufrene marked this conversation as resolved.
Show resolved Hide resolved
assumeFalse("OkHTTP client do not support gzip compression", false);
gdufrene marked this conversation as resolved.
Show resolved Hide resolved
gdufrene marked this conversation as resolved.
Show resolved Hide resolved
Expand Down