Browse Source

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 <velo@users.noreply.github.com>
master
Guillaume Dufrêne 1 year ago committed by GitHub
parent
commit
b500a0585f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      core/src/main/java/feign/Client.java
  2. 53
      core/src/test/java/feign/client/AbstractClientTest.java
  3. 10
      googlehttpclient/src/test/java/feign/googlehttpclient/GoogleHttpClientTest.java
  4. 10
      jaxrs2/src/test/java/feign/jaxrs2/JAXRSClientTest.java
  5. 10
      okhttp/src/test/java/feign/okhttp/OkHttpClientTest.java

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

@ -132,13 +132,12 @@ public interface Client {
if (status >= 400) { if (status >= 400) {
stream = connection.getErrorStream(); stream = connection.getErrorStream();
} else { } else {
if (this.isGzip(headers.get(CONTENT_ENCODING))) { stream = connection.getInputStream();
stream = new GZIPInputStream(connection.getInputStream()); }
} else if (this.isDeflate(headers.get(CONTENT_ENCODING))) { if (this.isGzip(headers.get(CONTENT_ENCODING))) {
stream = new InflaterInputStream(connection.getInputStream()); stream = new GZIPInputStream(stream);
} else { } else if (this.isDeflate(headers.get(CONTENT_ENCODING))) {
stream = connection.getInputStream(); stream = new InflaterInputStream(stream);
}
} }
return Response.builder() return Response.builder()
.status(status) .status(status)

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

@ -17,6 +17,7 @@ import static feign.Util.UTF_8;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry; import static org.assertj.core.api.Assertions.entry;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import feign.Client; import feign.Client;
import feign.CollectionFormat; import feign.CollectionFormat;
import feign.Feign.Builder; import feign.Feign.Builder;
@ -31,6 +32,7 @@ import feign.assertj.MockWebServerAssertions;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -410,6 +412,32 @@ public abstract class AbstractClientTest {
.isEqualToIgnoringCase(responseData); .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 @Test
public void canSupportDeflate() throws Exception { public void canSupportDeflate() throws Exception {
/* enqueue a zipped response */ /* enqueue a zipped response */
@ -428,6 +456,31 @@ public abstract class AbstractClientTest {
.isEqualToIgnoringCase(responseData); .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 @Test
public void canExceptCaseInsensitiveHeader() throws Exception { public void canExceptCaseInsensitiveHeader() throws Exception {
/* enqueue a zipped response */ /* enqueue a zipped response */

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

@ -54,11 +54,21 @@ public class GoogleHttpClientTest extends AbstractClientTest {
assumeFalse("Google HTTP client client do not support gzip compression", false); 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 @Override
public void canSupportDeflate() throws Exception { public void canSupportDeflate() throws Exception {
assumeFalse("Google HTTP client client do not support deflate compression", false); 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 @Override
public void canExceptCaseInsensitiveHeader() throws Exception { public void canExceptCaseInsensitiveHeader() throws Exception {
assumeFalse("Google HTTP client client do not support gzip compression", false); assumeFalse("Google HTTP client client do not support gzip compression", false);

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

@ -152,11 +152,21 @@ public class JAXRSClientTest extends AbstractClientTest {
assumeFalse("JaxRS client do not support gzip compression", false); 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 @Override
public void canSupportDeflate() throws Exception { public void canSupportDeflate() throws Exception {
assumeFalse("JaxRS client do not support deflate compression", false); 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 @Override
public void canExceptCaseInsensitiveHeader() throws Exception { public void canExceptCaseInsensitiveHeader() throws Exception {
assumeFalse("JaxRS client do not support gzip compression", false); assumeFalse("JaxRS client do not support gzip compression", false);

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

@ -115,11 +115,21 @@ public class OkHttpClientTest extends AbstractClientTest {
assumeFalse("OkHTTP client do not support gzip compression", false); assumeFalse("OkHTTP client do not support gzip compression", false);
} }
@Override
public void canSupportGzipOnError() throws Exception {
assumeFalse("OkHTTP client do not support gzip compression", false);
}
@Override @Override
public void canSupportDeflate() throws Exception { public void canSupportDeflate() throws Exception {
assumeFalse("OkHTTP client do not support deflate compression", false); assumeFalse("OkHTTP client do not support deflate compression", false);
} }
@Override
public void canSupportDeflateOnError() throws Exception {
assumeFalse("OkHTTP client do not support deflate compression", false);
}
@Override @Override
public void canExceptCaseInsensitiveHeader() throws Exception { public void canExceptCaseInsensitiveHeader() throws Exception {
assumeFalse("OkHTTP client do not support gzip compression", false); assumeFalse("OkHTTP client do not support gzip compression", false);

Loading…
Cancel
Save