|
14 | 14 | package feign.client; |
15 | 15 |
|
16 | 16 | 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; |
18 | 19 | import static org.junit.Assert.assertEquals; |
19 | 20 |
|
20 | 21 | import feign.Client; |
|
29 | 30 | import feign.Util; |
30 | 31 | import feign.assertj.MockWebServerAssertions; |
31 | 32 | import java.io.ByteArrayInputStream; |
| 33 | +import java.io.ByteArrayOutputStream; |
32 | 34 | import java.io.IOException; |
| 35 | +import java.nio.charset.StandardCharsets; |
33 | 36 | import java.util.Arrays; |
34 | 37 | import java.util.Collections; |
35 | 38 | import java.util.List; |
| 39 | +import java.util.zip.DeflaterOutputStream; |
| 40 | +import java.util.zip.GZIPOutputStream; |
36 | 41 | import okhttp3.mockwebserver.MockResponse; |
37 | 42 | import okhttp3.mockwebserver.MockWebServer; |
38 | 43 | import okhttp3.mockwebserver.RecordedRequest; |
| 44 | +import okio.Buffer; |
39 | 45 | import org.junit.Rule; |
40 | 46 | import org.junit.Test; |
41 | 47 | import org.junit.rules.ExpectedException; |
@@ -396,6 +402,60 @@ public void testAlternativeCollectionFormat() throws Exception { |
396 | 402 | .hasOneOfPath("/?foo=bar,baz", "/?foo=bar%2Cbaz"); |
397 | 403 | } |
398 | 404 |
|
| 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 | + |
399 | 459 | @SuppressWarnings("UnusedReturnValue") |
400 | 460 | public interface TestInterface { |
401 | 461 |
|
@@ -442,4 +502,22 @@ public interface TestInterface { |
442 | 502 | @Headers({"Foo: Bar", "Foo: Baz", "Qux: ", "Content-Type: {contentType}"}) |
443 | 503 | Response postWithContentType(String body, @Param("contentType") String contentType); |
444 | 504 | } |
| 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 | + } |
445 | 523 | } |
0 commit comments