From f07c484b7ae225f8c9a919223649d3d0490b82be Mon Sep 17 00:00:00 2001 From: Witalij Berdinskich Date: Mon, 8 Aug 2022 00:57:10 +0300 Subject: [PATCH] Proof that clients support gzip and deflate compression (#1713) * Proof that clients support gzip and deflate compression * Remove unused private methods --- .../java/feign/client/AbstractClientTest.java | 80 ++++++++++++++++++- .../java/feign/client/DefaultClientTest.java | 75 ----------------- googlehttpclient/README.md | 2 +- .../GoogleHttpClientTest.java | 21 +++++ .../java/feign/jaxrs2/JAXRSClientTest.java | 19 +++++ .../java/feign/okhttp/OkHttpClientTest.java | 21 +++++ 6 files changed, 141 insertions(+), 77 deletions(-) diff --git a/core/src/test/java/feign/client/AbstractClientTest.java b/core/src/test/java/feign/client/AbstractClientTest.java index 45c8ce52..e9790eed 100644 --- a/core/src/test/java/feign/client/AbstractClientTest.java +++ b/core/src/test/java/feign/client/AbstractClientTest.java @@ -14,7 +14,8 @@ package feign.client; import static feign.Util.UTF_8; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; import static org.junit.Assert.assertEquals; import feign.Client; import feign.CollectionFormat; @@ -28,13 +29,18 @@ import feign.Response; import feign.Util; import feign.assertj.MockWebServerAssertions; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.zip.DeflaterOutputStream; +import java.util.zip.GZIPOutputStream; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.RecordedRequest; +import okio.Buffer; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -386,6 +392,60 @@ public abstract class AbstractClientTest { .hasOneOfPath("/?foo=bar,baz", "/?foo=bar%2Cbaz"); } + @Test + public void canSupportGzip() throws Exception { + /* enqueue a zipped response */ + final String responseData = "Compressed Data"; + server.enqueue(new MockResponse() + .addHeader("Content-Encoding", "gzip") + .setBody(new Buffer().write(compress(responseData)))); + + TestInterface api = newBuilder() + .target(TestInterface.class, "http://localhost:" + server.getPort()); + + String result = api.get(); + + /* verify that the response is unzipped */ + assertThat(result).isNotNull() + .isEqualToIgnoringCase(responseData); + } + + @Test + public void canSupportDeflate() throws Exception { + /* enqueue a zipped response */ + final String responseData = "Compressed Data"; + server.enqueue(new MockResponse() + .addHeader("Content-Encoding", "deflate") + .setBody(new Buffer().write(deflate(responseData)))); + + TestInterface api = newBuilder() + .target(TestInterface.class, "http://localhost:" + server.getPort()); + + String result = api.get(); + + /* verify that the response is unzipped */ + assertThat(result).isNotNull() + .isEqualToIgnoringCase(responseData); + } + + @Test + public void canExceptCaseInsensitiveHeader() throws Exception { + /* enqueue a zipped response */ + final String responseData = "Compressed Data"; + server.enqueue(new MockResponse() + .addHeader("content-encoding", "gzip") + .setBody(new Buffer().write(compress(responseData)))); + + TestInterface api = newBuilder() + .target(TestInterface.class, "http://localhost:" + server.getPort()); + + String result = api.get(); + + /* verify that the response is unzipped */ + assertThat(result).isNotNull() + .isEqualToIgnoringCase(responseData); + } + @SuppressWarnings("UnusedReturnValue") public interface TestInterface { @@ -435,4 +495,22 @@ public abstract class AbstractClientTest { Response postWithContentType(String body, @Param("contentType") String contentType); } + private byte[] compress(String data) throws Exception { + try (ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length())) { + GZIPOutputStream gzipOutputStream = new GZIPOutputStream(bos); + gzipOutputStream.write(data.getBytes(StandardCharsets.UTF_8), 0, data.length()); + gzipOutputStream.close(); + return bos.toByteArray(); + } + } + + private byte[] deflate(String data) throws Exception { + try (ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length())) { + DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(bos); + deflaterOutputStream.write(data.getBytes(StandardCharsets.UTF_8), 0, data.length()); + deflaterOutputStream.close(); + return bos.toByteArray(); + } + } + } diff --git a/core/src/test/java/feign/client/DefaultClientTest.java b/core/src/test/java/feign/client/DefaultClientTest.java index 7ba7bedb..d41ea696 100644 --- a/core/src/test/java/feign/client/DefaultClientTest.java +++ b/core/src/test/java/feign/client/DefaultClientTest.java @@ -150,79 +150,4 @@ public class DefaultClientTest extends AbstractClientTest { assertThat(connection).isNotNull().isInstanceOf(HttpURLConnection.class); } - - @Test - public void canSupportGzip() throws Exception { - /* enqueue a zipped response */ - final String responseData = "Compressed Data"; - server.enqueue(new MockResponse() - .addHeader("Content-Encoding", "gzip") - .setBody(new Buffer().write(compress(responseData)))); - - TestInterface api = newBuilder() - .target(TestInterface.class, "http://localhost:" + server.getPort()); - - String result = api.get(); - - /* verify that the response is unzipped */ - assertThat(result).isNotNull() - .isEqualToIgnoringCase(responseData); - - } - - @Test - public void canExeptCaseInsensitiveHeader() throws Exception { - /* enqueue a zipped response */ - final String responseData = "Compressed Data"; - server.enqueue(new MockResponse() - .addHeader("content-encoding", "gzip") - .setBody(new Buffer().write(compress(responseData)))); - - TestInterface api = newBuilder() - .target(TestInterface.class, "http://localhost:" + server.getPort()); - - String result = api.get(); - - /* verify that the response is unzipped */ - assertThat(result).isNotNull() - .isEqualToIgnoringCase(responseData); - - } - - @Test - public void canSupportDeflate() throws Exception { - /* enqueue a zipped response */ - final String responseData = "Compressed Data"; - server.enqueue(new MockResponse() - .addHeader("Content-Encoding", "deflate") - .setBody(new Buffer().write(deflate(responseData)))); - - TestInterface api = newBuilder() - .target(TestInterface.class, "http://localhost:" + server.getPort()); - - String result = api.get(); - - /* verify that the response is unzipped */ - assertThat(result).isNotNull() - .isEqualToIgnoringCase(responseData); - - } - - private byte[] compress(String data) throws Exception { - try (ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length())) { - GZIPOutputStream gzipOutputStream = new GZIPOutputStream(bos); - gzipOutputStream.write(data.getBytes(StandardCharsets.UTF_8), 0, data.length()); - gzipOutputStream.close(); - return bos.toByteArray(); - } - } - - private byte[] deflate(String data) throws Exception { - try (ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length())) { - DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(bos); - deflaterOutputStream.write(data.getBytes(StandardCharsets.UTF_8), 0, data.length()); - deflaterOutputStream.close(); - return bos.toByteArray(); - } - } } diff --git a/googlehttpclient/README.md b/googlehttpclient/README.md index ccc3903e..5ca59242 100644 --- a/googlehttpclient/README.md +++ b/googlehttpclient/README.md @@ -2,7 +2,7 @@ This module is a feign [Client](https://github.com/OpenFeign/feign/blob/master/core/src/main/java/feign/Client.java) to use the java [Google Http Client](https://github.com/googleapis/google-http-java-client). -To use this, add to your classpath (via maven, or otherwise). Then cofigure Feign to use the GoogleHttpClient: +To use this, add to your classpath (via maven, or otherwise). Then configure Feign to use the GoogleHttpClient: ```java GitHub github = Feign.builder() diff --git a/googlehttpclient/src/test/java/feign/googlehttpclient/GoogleHttpClientTest.java b/googlehttpclient/src/test/java/feign/googlehttpclient/GoogleHttpClientTest.java index 74664e36..478f49fd 100644 --- a/googlehttpclient/src/test/java/feign/googlehttpclient/GoogleHttpClientTest.java +++ b/googlehttpclient/src/test/java/feign/googlehttpclient/GoogleHttpClientTest.java @@ -16,6 +16,7 @@ package feign.googlehttpclient; import feign.Feign; import feign.Feign.Builder; import feign.client.AbstractClientTest; +import static org.junit.Assume.assumeFalse; public class GoogleHttpClientTest extends AbstractClientTest { @Override @@ -34,4 +35,24 @@ public class GoogleHttpClientTest extends AbstractClientTest { @Override public void parsesUnauthorizedResponseBody() {} + + /* + * Google HTTP client with NetHttpTransport does not support gzip and deflate compression + * out-of-the-box. You can replace the transport with Apache HTTP Client. + */ + @Override + public void canSupportGzip() 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 canExceptCaseInsensitiveHeader() throws Exception { + assumeFalse("Google HTTP client client do not support gzip compression", false); + } + } diff --git a/jaxrs2/src/test/java/feign/jaxrs2/JAXRSClientTest.java b/jaxrs2/src/test/java/feign/jaxrs2/JAXRSClientTest.java index 7b28b668..1016391b 100644 --- a/jaxrs2/src/test/java/feign/jaxrs2/JAXRSClientTest.java +++ b/jaxrs2/src/test/java/feign/jaxrs2/JAXRSClientTest.java @@ -29,6 +29,7 @@ import java.util.Collections; import static feign.Util.UTF_8; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; +import static org.junit.Assume.assumeFalse; /** * Tests client-specific behavior, such as ensuring Content-Length is sent when specified. @@ -143,6 +144,24 @@ public class JAXRSClientTest extends AbstractClientTest { .hasMethod("POST"); } + /* + * JaxRS does not support gzip and deflate compression out-of-the-box. + */ + @Override + public void canSupportGzip() 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 canExceptCaseInsensitiveHeader() throws Exception { + assumeFalse("JaxRS client do not support gzip compression", false); + } + public interface JaxRSClientTestInterface { @RequestLine("GET /") diff --git a/okhttp/src/test/java/feign/okhttp/OkHttpClientTest.java b/okhttp/src/test/java/feign/okhttp/OkHttpClientTest.java index d494b5ab..982181e9 100644 --- a/okhttp/src/test/java/feign/okhttp/OkHttpClientTest.java +++ b/okhttp/src/test/java/feign/okhttp/OkHttpClientTest.java @@ -29,6 +29,7 @@ import okhttp3.mockwebserver.MockResponse; import org.assertj.core.data.MapEntry; import org.junit.Test; import static org.junit.Assert.assertEquals; +import static org.junit.Assume.assumeFalse; /** Tests client-specific behavior, such as ensuring Content-Length is sent when specified. */ public class OkHttpClientTest extends AbstractClientTest { @@ -103,6 +104,26 @@ public class OkHttpClientTest extends AbstractClientTest { } + /* + * OkHTTP does not support gzip and deflate compression out-of-the-box. But you can add an + * interceptor that implies it, see + * https://stackoverflow.com/questions/51901333/okhttp-3-how-to-decompress-gzip-deflate-response- + * manually-using-java-android + */ + @Override + public void canSupportGzip() throws Exception { + assumeFalse("OkHTTP client do not support gzip compression", false); + } + + @Override + public void canSupportDeflate() throws Exception { + assumeFalse("OkHTTP client do not support deflate compression", false); + } + + @Override + public void canExceptCaseInsensitiveHeader() throws Exception { + assumeFalse("OkHTTP client do not support gzip compression", false); + } public interface OkHttpClientTestInterface {