Skip to content

Commit 95e413e

Browse files
authored
Proof that clients support gzip and deflate compression (#1713)
* Proof that clients support gzip and deflate compression * Remove unused private methods
1 parent de32880 commit 95e413e

File tree

6 files changed

+142
-79
lines changed

6 files changed

+142
-79
lines changed

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

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
package feign.client;
1515

1616
import static feign.Util.UTF_8;
17-
import static org.assertj.core.api.Assertions.*;
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.assertj.core.api.Assertions.entry;
1819
import static org.junit.Assert.assertEquals;
1920

2021
import feign.Client;
@@ -29,13 +30,18 @@
2930
import feign.Util;
3031
import feign.assertj.MockWebServerAssertions;
3132
import java.io.ByteArrayInputStream;
33+
import java.io.ByteArrayOutputStream;
3234
import java.io.IOException;
35+
import java.nio.charset.StandardCharsets;
3336
import java.util.Arrays;
3437
import java.util.Collections;
3538
import java.util.List;
39+
import java.util.zip.DeflaterOutputStream;
40+
import java.util.zip.GZIPOutputStream;
3641
import okhttp3.mockwebserver.MockResponse;
3742
import okhttp3.mockwebserver.MockWebServer;
3843
import okhttp3.mockwebserver.RecordedRequest;
44+
import okio.Buffer;
3945
import org.junit.Rule;
4046
import org.junit.Test;
4147
import org.junit.rules.ExpectedException;
@@ -396,6 +402,60 @@ public void testAlternativeCollectionFormat() throws Exception {
396402
.hasOneOfPath("/?foo=bar,baz", "/?foo=bar%2Cbaz");
397403
}
398404

405+
@Test
406+
public void canSupportGzip() throws Exception {
407+
/* enqueue a zipped response */
408+
final String responseData = "Compressed Data";
409+
server.enqueue(
410+
new MockResponse()
411+
.addHeader("Content-Encoding", "gzip")
412+
.setBody(new Buffer().write(compress(responseData))));
413+
414+
TestInterface api =
415+
newBuilder().target(TestInterface.class, "http://localhost:" + server.getPort());
416+
417+
String result = api.get();
418+
419+
/* verify that the response is unzipped */
420+
assertThat(result).isNotNull().isEqualToIgnoringCase(responseData);
421+
}
422+
423+
@Test
424+
public void canSupportDeflate() throws Exception {
425+
/* enqueue a zipped response */
426+
final String responseData = "Compressed Data";
427+
server.enqueue(
428+
new MockResponse()
429+
.addHeader("Content-Encoding", "deflate")
430+
.setBody(new Buffer().write(deflate(responseData))));
431+
432+
TestInterface api =
433+
newBuilder().target(TestInterface.class, "http://localhost:" + server.getPort());
434+
435+
String result = api.get();
436+
437+
/* verify that the response is unzipped */
438+
assertThat(result).isNotNull().isEqualToIgnoringCase(responseData);
439+
}
440+
441+
@Test
442+
public void canExceptCaseInsensitiveHeader() throws Exception {
443+
/* enqueue a zipped response */
444+
final String responseData = "Compressed Data";
445+
server.enqueue(
446+
new MockResponse()
447+
.addHeader("content-encoding", "gzip")
448+
.setBody(new Buffer().write(compress(responseData))));
449+
450+
TestInterface api =
451+
newBuilder().target(TestInterface.class, "http://localhost:" + server.getPort());
452+
453+
String result = api.get();
454+
455+
/* verify that the response is unzipped */
456+
assertThat(result).isNotNull().isEqualToIgnoringCase(responseData);
457+
}
458+
399459
@SuppressWarnings("UnusedReturnValue")
400460
public interface TestInterface {
401461

@@ -442,4 +502,22 @@ public interface TestInterface {
442502
@Headers({"Foo: Bar", "Foo: Baz", "Qux: ", "Content-Type: {contentType}"})
443503
Response postWithContentType(String body, @Param("contentType") String contentType);
444504
}
505+
506+
private byte[] compress(String data) throws Exception {
507+
try (ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length())) {
508+
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(bos);
509+
gzipOutputStream.write(data.getBytes(StandardCharsets.UTF_8), 0, data.length());
510+
gzipOutputStream.close();
511+
return bos.toByteArray();
512+
}
513+
}
514+
515+
private byte[] deflate(String data) throws Exception {
516+
try (ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length())) {
517+
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(bos);
518+
deflaterOutputStream.write(data.getBytes(StandardCharsets.UTF_8), 0, data.length());
519+
deflaterOutputStream.close();
520+
return bos.toByteArray();
521+
}
522+
}
445523
}

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

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import feign.Feign;
2323
import feign.Feign.Builder;
2424
import feign.RetryableException;
25-
import java.io.ByteArrayOutputStream;
2625
import java.io.IOException;
2726
import java.net.HttpURLConnection;
2827
import java.net.InetSocketAddress;
@@ -31,14 +30,10 @@
3130
import java.net.Proxy.Type;
3231
import java.net.SocketAddress;
3332
import java.net.URL;
34-
import java.nio.charset.StandardCharsets;
35-
import java.util.zip.DeflaterOutputStream;
36-
import java.util.zip.GZIPOutputStream;
3733
import javax.net.ssl.HostnameVerifier;
3834
import javax.net.ssl.SSLSession;
3935
import okhttp3.mockwebserver.MockResponse;
4036
import okhttp3.mockwebserver.SocketPolicy;
41-
import okio.Buffer;
4237
import org.junit.Test;
4338

4439
/** Tests client-specific behavior, such as ensuring Content-Length is sent when specified. */
@@ -152,76 +147,4 @@ public void canCreateWithExplicitCredentials() throws Exception {
152147
HttpURLConnection connection = proxied.getConnection(new URL("http://www.example.com"));
153148
assertThat(connection).isNotNull().isInstanceOf(HttpURLConnection.class);
154149
}
155-
156-
@Test
157-
public void canSupportGzip() throws Exception {
158-
/* enqueue a zipped response */
159-
final String responseData = "Compressed Data";
160-
server.enqueue(
161-
new MockResponse()
162-
.addHeader("Content-Encoding", "gzip")
163-
.setBody(new Buffer().write(compress(responseData))));
164-
165-
TestInterface api =
166-
newBuilder().target(TestInterface.class, "http://localhost:" + server.getPort());
167-
168-
String result = api.get();
169-
170-
/* verify that the response is unzipped */
171-
assertThat(result).isNotNull().isEqualToIgnoringCase(responseData);
172-
}
173-
174-
@Test
175-
public void canExeptCaseInsensitiveHeader() throws Exception {
176-
/* enqueue a zipped response */
177-
final String responseData = "Compressed Data";
178-
server.enqueue(
179-
new MockResponse()
180-
.addHeader("content-encoding", "gzip")
181-
.setBody(new Buffer().write(compress(responseData))));
182-
183-
TestInterface api =
184-
newBuilder().target(TestInterface.class, "http://localhost:" + server.getPort());
185-
186-
String result = api.get();
187-
188-
/* verify that the response is unzipped */
189-
assertThat(result).isNotNull().isEqualToIgnoringCase(responseData);
190-
}
191-
192-
@Test
193-
public void canSupportDeflate() throws Exception {
194-
/* enqueue a zipped response */
195-
final String responseData = "Compressed Data";
196-
server.enqueue(
197-
new MockResponse()
198-
.addHeader("Content-Encoding", "deflate")
199-
.setBody(new Buffer().write(deflate(responseData))));
200-
201-
TestInterface api =
202-
newBuilder().target(TestInterface.class, "http://localhost:" + server.getPort());
203-
204-
String result = api.get();
205-
206-
/* verify that the response is unzipped */
207-
assertThat(result).isNotNull().isEqualToIgnoringCase(responseData);
208-
}
209-
210-
private byte[] compress(String data) throws Exception {
211-
try (ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length())) {
212-
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(bos);
213-
gzipOutputStream.write(data.getBytes(StandardCharsets.UTF_8), 0, data.length());
214-
gzipOutputStream.close();
215-
return bos.toByteArray();
216-
}
217-
}
218-
219-
private byte[] deflate(String data) throws Exception {
220-
try (ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length())) {
221-
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(bos);
222-
deflaterOutputStream.write(data.getBytes(StandardCharsets.UTF_8), 0, data.length());
223-
deflaterOutputStream.close();
224-
return bos.toByteArray();
225-
}
226-
}
227150
}

googlehttpclient/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
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).
44

5-
To use this, add to your classpath (via maven, or otherwise). Then cofigure Feign to use the GoogleHttpClient:
5+
To use this, add to your classpath (via maven, or otherwise). Then configure Feign to use the GoogleHttpClient:
66

77
```java
88
GitHub github = Feign.builder()

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
package feign.googlehttpclient;
1515

16+
import static org.junit.Assume.assumeFalse;
17+
1618
import feign.Feign;
1719
import feign.Feign.Builder;
1820
import feign.client.AbstractClientTest;
@@ -33,4 +35,23 @@ public void testPatch() {}
3335

3436
@Override
3537
public void parsesUnauthorizedResponseBody() {}
38+
39+
/*
40+
* Google HTTP client with NetHttpTransport does not support gzip and deflate compression
41+
* out-of-the-box. You can replace the transport with Apache HTTP Client.
42+
*/
43+
@Override
44+
public void canSupportGzip() throws Exception {
45+
assumeFalse("Google HTTP client client do not support gzip compression", false);
46+
}
47+
48+
@Override
49+
public void canSupportDeflate() throws Exception {
50+
assumeFalse("Google HTTP client client do not support deflate compression", false);
51+
}
52+
53+
@Override
54+
public void canExceptCaseInsensitiveHeader() throws Exception {
55+
assumeFalse("Google HTTP client client do not support gzip compression", false);
56+
}
3657
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import static feign.Util.UTF_8;
1717
import static org.assertj.core.api.Assertions.assertThat;
1818
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assume.assumeFalse;
1920

2021
import feign.*;
2122
import feign.Feign.Builder;
@@ -150,6 +151,24 @@ public void testConsumesMultipleWithContentTypeHeaderAndBody() throws Exception
150151
.hasMethod("POST");
151152
}
152153

154+
/*
155+
* JaxRS does not support gzip and deflate compression out-of-the-box.
156+
*/
157+
@Override
158+
public void canSupportGzip() throws Exception {
159+
assumeFalse("JaxRS client do not support gzip compression", false);
160+
}
161+
162+
@Override
163+
public void canSupportDeflate() throws Exception {
164+
assumeFalse("JaxRS client do not support deflate compression", false);
165+
}
166+
167+
@Override
168+
public void canExceptCaseInsensitiveHeader() throws Exception {
169+
assumeFalse("JaxRS client do not support gzip compression", false);
170+
}
171+
153172
public interface JaxRSClientTestInterface {
154173

155174
@RequestLine("GET /")

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package feign.okhttp;
1515

1616
import static org.junit.Assert.assertEquals;
17+
import static org.junit.Assume.assumeFalse;
1718

1819
import feign.Feign;
1920
import feign.Feign.Builder;
@@ -103,6 +104,27 @@ public void testFollowRedirect() throws Exception {
103104
assertEquals(expectedBody, payload);
104105
}
105106

107+
/*
108+
* OkHTTP does not support gzip and deflate compression out-of-the-box. But you can add an
109+
* interceptor that implies it, see
110+
* https://stackoverflow.com/questions/51901333/okhttp-3-how-to-decompress-gzip-deflate-response-
111+
* manually-using-java-android
112+
*/
113+
@Override
114+
public void canSupportGzip() throws Exception {
115+
assumeFalse("OkHTTP client do not support gzip compression", false);
116+
}
117+
118+
@Override
119+
public void canSupportDeflate() throws Exception {
120+
assumeFalse("OkHTTP client do not support deflate compression", false);
121+
}
122+
123+
@Override
124+
public void canExceptCaseInsensitiveHeader() throws Exception {
125+
assumeFalse("OkHTTP client do not support gzip compression", false);
126+
}
127+
106128
public interface OkHttpClientTestInterface {
107129

108130
@RequestLine("GET /")

0 commit comments

Comments
 (0)