Skip to content

Commit 2d06330

Browse files
Adds deflate encoding support
1 parent cbf15f0 commit 2d06330

4 files changed

Lines changed: 44 additions & 1 deletion

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.LinkedHashMap;
2525
import java.util.List;
2626
import java.util.Map;
27+
import java.util.zip.DeflaterOutputStream;
2728
import java.util.zip.GZIPOutputStream;
2829

2930
import javax.net.ssl.HostnameVerifier;
@@ -34,6 +35,7 @@
3435

3536
import static feign.Util.CONTENT_ENCODING;
3637
import static feign.Util.CONTENT_LENGTH;
38+
import static feign.Util.ENCODING_DEFLATE;
3739
import static feign.Util.ENCODING_GZIP;
3840

3941
/**
@@ -93,6 +95,9 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
9395
boolean
9496
gzipEncodedRequest =
9597
contentEncodingValues != null && contentEncodingValues.contains(ENCODING_GZIP);
98+
boolean
99+
deflateEncodedRequest =
100+
contentEncodingValues != null && contentEncodingValues.contains(ENCODING_DEFLATE);
96101

97102
boolean hasAcceptHeader = false;
98103
Integer contentLength = null;
@@ -102,7 +107,7 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
102107
}
103108
for (String value : request.headers().get(field)) {
104109
if (field.equals(CONTENT_LENGTH)) {
105-
if (!gzipEncodedRequest) {
110+
if (!gzipEncodedRequest && !deflateEncodedRequest) {
106111
contentLength = Integer.valueOf(value);
107112
connection.addRequestProperty(field, value);
108113
}
@@ -126,6 +131,8 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
126131
OutputStream out = connection.getOutputStream();
127132
if (gzipEncodedRequest) {
128133
out = new GZIPOutputStream(out);
134+
} else if (deflateEncodedRequest) {
135+
out = new DeflaterOutputStream(out);
129136
}
130137
try {
131138
out.write(request.body());

core/src/main/java/feign/Util.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public class Util {
5757
* Value for the Content-Encoding header that indicates that GZIP encoding is in use.
5858
*/
5959
public static final String ENCODING_GZIP = "gzip";
60+
/**
61+
* Value for the Content-Encoding header that indicates that DEFLATE encoding is in use.
62+
*/
63+
public static final String ENCODING_DEFLATE = "deflate";
6064
/**
6165
* UTF-8: eight-bit UCS Transformation Format.
6266
*/

core/src/test/java/feign/FeignTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,19 @@ public void postGZIPEncodedBodyParam() throws Exception {
156156
.hasGzippedBody("[netflix, denominator, password]".getBytes(UTF_8));
157157
}
158158

159+
@Test
160+
public void postDeflateEncodedBodyParam() throws Exception {
161+
server.enqueue(new MockResponse().setBody("foo"));
162+
163+
TestInterface api = new TestInterfaceBuilder().target("http://localhost:" + server.getPort());
164+
165+
api.deflateBody(Arrays.asList("netflix", "denominator", "password"));
166+
167+
assertThat(server.takeRequest())
168+
.hasNoHeaderNamed("Content-Length")
169+
.hasDeflatedBody("[netflix, denominator, password]".getBytes(UTF_8));
170+
}
171+
159172
@Test
160173
public void singleInterceptor() throws Exception {
161174
server.enqueue(new MockResponse().setBody("foo"));
@@ -409,6 +422,10 @@ void login(
409422
@Headers("Content-Encoding: gzip")
410423
void gzipBody(List<String> contents);
411424

425+
@RequestLine("POST /")
426+
@Headers("Content-Encoding: deflate")
427+
void deflateBody(List<String> contents);
428+
412429
@RequestLine("POST /")
413430
void form(
414431
@Param("customer_name") String customer, @Param("user_name") String user,

core/src/test/java/feign/assertj/RecordedRequestAssert.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.LinkedHashSet;
2929
import java.util.Set;
3030
import java.util.zip.GZIPInputStream;
31+
import java.util.zip.InflaterInputStream;
3132

3233
import feign.Util;
3334

@@ -77,6 +78,20 @@ public RecordedRequestAssert hasGzippedBody(byte[] expectedUncompressed) {
7778
return this;
7879
}
7980

81+
public RecordedRequestAssert hasDeflatedBody(byte[] expectedUncompressed) {
82+
isNotNull();
83+
byte[] compressedBody = actual.getBody();
84+
byte[] uncompressedBody;
85+
try {
86+
uncompressedBody =
87+
Util.toByteArray(new InflaterInputStream(new ByteArrayInputStream(compressedBody)));
88+
} catch (IOException e) {
89+
throw new RuntimeException(e);
90+
}
91+
arrays.assertContains(info, uncompressedBody, expectedUncompressed);
92+
return this;
93+
}
94+
8095
public RecordedRequestAssert hasBody(byte[] expected) {
8196
isNotNull();
8297
arrays.assertContains(info, actual.getBody(), expected);

0 commit comments

Comments
 (0)