Browse Source

Adds deflate encoding support

pull/231/head
Santhosh Kumar Tekuri 10 years ago
parent
commit
2d06330b49
  1. 9
      core/src/main/java/feign/Client.java
  2. 4
      core/src/main/java/feign/Util.java
  3. 17
      core/src/test/java/feign/FeignTest.java
  4. 15
      core/src/test/java/feign/assertj/RecordedRequestAssert.java

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

@ -24,6 +24,7 @@ import java.util.Collection; @@ -24,6 +24,7 @@ import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.GZIPOutputStream;
import javax.net.ssl.HostnameVerifier;
@ -34,6 +35,7 @@ import feign.Request.Options; @@ -34,6 +35,7 @@ import feign.Request.Options;
import static feign.Util.CONTENT_ENCODING;
import static feign.Util.CONTENT_LENGTH;
import static feign.Util.ENCODING_DEFLATE;
import static feign.Util.ENCODING_GZIP;
/**
@ -93,6 +95,9 @@ public interface Client { @@ -93,6 +95,9 @@ public interface Client {
boolean
gzipEncodedRequest =
contentEncodingValues != null && contentEncodingValues.contains(ENCODING_GZIP);
boolean
deflateEncodedRequest =
contentEncodingValues != null && contentEncodingValues.contains(ENCODING_DEFLATE);
boolean hasAcceptHeader = false;
Integer contentLength = null;
@ -102,7 +107,7 @@ public interface Client { @@ -102,7 +107,7 @@ public interface Client {
}
for (String value : request.headers().get(field)) {
if (field.equals(CONTENT_LENGTH)) {
if (!gzipEncodedRequest) {
if (!gzipEncodedRequest && !deflateEncodedRequest) {
contentLength = Integer.valueOf(value);
connection.addRequestProperty(field, value);
}
@ -126,6 +131,8 @@ public interface Client { @@ -126,6 +131,8 @@ public interface Client {
OutputStream out = connection.getOutputStream();
if (gzipEncodedRequest) {
out = new GZIPOutputStream(out);
} else if (deflateEncodedRequest) {
out = new DeflaterOutputStream(out);
}
try {
out.write(request.body());

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

@ -57,6 +57,10 @@ public class Util { @@ -57,6 +57,10 @@ public class Util {
* Value for the Content-Encoding header that indicates that GZIP encoding is in use.
*/
public static final String ENCODING_GZIP = "gzip";
/**
* Value for the Content-Encoding header that indicates that DEFLATE encoding is in use.
*/
public static final String ENCODING_DEFLATE = "deflate";
/**
* UTF-8: eight-bit UCS Transformation Format.
*/

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

@ -156,6 +156,19 @@ public class FeignTest { @@ -156,6 +156,19 @@ public class FeignTest {
.hasGzippedBody("[netflix, denominator, password]".getBytes(UTF_8));
}
@Test
public void postDeflateEncodedBodyParam() throws Exception {
server.enqueue(new MockResponse().setBody("foo"));
TestInterface api = new TestInterfaceBuilder().target("http://localhost:" + server.getPort());
api.deflateBody(Arrays.asList("netflix", "denominator", "password"));
assertThat(server.takeRequest())
.hasNoHeaderNamed("Content-Length")
.hasDeflatedBody("[netflix, denominator, password]".getBytes(UTF_8));
}
@Test
public void singleInterceptor() throws Exception {
server.enqueue(new MockResponse().setBody("foo"));
@ -409,6 +422,10 @@ public class FeignTest { @@ -409,6 +422,10 @@ public class FeignTest {
@Headers("Content-Encoding: gzip")
void gzipBody(List<String> contents);
@RequestLine("POST /")
@Headers("Content-Encoding: deflate")
void deflateBody(List<String> contents);
@RequestLine("POST /")
void form(
@Param("customer_name") String customer, @Param("user_name") String user,

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

@ -28,6 +28,7 @@ import java.io.IOException; @@ -28,6 +28,7 @@ import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;
import feign.Util;
@ -77,6 +78,20 @@ public final class RecordedRequestAssert @@ -77,6 +78,20 @@ public final class RecordedRequestAssert
return this;
}
public RecordedRequestAssert hasDeflatedBody(byte[] expectedUncompressed) {
isNotNull();
byte[] compressedBody = actual.getBody();
byte[] uncompressedBody;
try {
uncompressedBody =
Util.toByteArray(new InflaterInputStream(new ByteArrayInputStream(compressedBody)));
} catch (IOException e) {
throw new RuntimeException(e);
}
arrays.assertContains(info, uncompressedBody, expectedUncompressed);
return this;
}
public RecordedRequestAssert hasBody(byte[] expected) {
isNotNull();
arrays.assertContains(info, actual.getBody(), expected);

Loading…
Cancel
Save