|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 The Feign Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | +package feign.hc5; |
| 15 | + |
| 16 | +import static org.junit.Assume.assumeTrue; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 18 | + |
| 19 | +import java.io.ByteArrayInputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.util.zip.GZIPInputStream; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import feign.Feign; |
| 27 | +import feign.Feign.Builder; |
| 28 | +import feign.RequestLine; |
| 29 | +import feign.client.AbstractClientTest; |
| 30 | +import okhttp3.mockwebserver.MockResponse; |
| 31 | +import okhttp3.mockwebserver.RecordedRequest; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests that 'Content-Encoding: gzip' is handled correctly |
| 35 | + */ |
| 36 | +public class GzipHttp5ClientTest extends AbstractClientTest { |
| 37 | + |
| 38 | + @Override |
| 39 | + public Builder newBuilder() { |
| 40 | + return Feign.builder().client(new ApacheHttp5Client()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testWithCompressedBody() throws InterruptedException, IOException { |
| 45 | + final TestInterface testInterface = buildTestInterface(true); |
| 46 | + |
| 47 | + server.enqueue(new MockResponse().setBody("foo")); |
| 48 | + |
| 49 | + assertEquals("foo", testInterface.withBody("bar")); |
| 50 | + final RecordedRequest request1 = server.takeRequest(); |
| 51 | + assertEquals("/test", request1.getPath()); |
| 52 | + |
| 53 | + ByteArrayInputStream bodyContentIs = new ByteArrayInputStream(request1.getBody().readByteArray()); |
| 54 | + byte[] uncompressed = new GZIPInputStream(bodyContentIs).readAllBytes(); |
| 55 | + |
| 56 | + assertEquals("bar", new String(uncompressed, StandardCharsets.UTF_8)); |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testWithUncompressedBody() throws InterruptedException, IOException { |
| 62 | + final TestInterface testInterface = buildTestInterface(false); |
| 63 | + |
| 64 | + server.enqueue(new MockResponse().setBody("foo")); |
| 65 | + |
| 66 | + assertEquals("foo", testInterface.withBody("bar")); |
| 67 | + final RecordedRequest request1 = server.takeRequest(); |
| 68 | + assertEquals("/test", request1.getPath()); |
| 69 | + |
| 70 | + assertEquals("bar", request1.getBody().readString(StandardCharsets.UTF_8)); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + private TestInterface buildTestInterface(boolean compress) { |
| 75 | + return newBuilder() |
| 76 | + .requestInterceptor(req -> req.header("Content-Encoding", compress ? "gzip" : "")) |
| 77 | + .target(TestInterface.class, "http://localhost:" + server.getPort()); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + @Override |
| 82 | + public void testVeryLongResponseNullLength() { |
| 83 | + assumeTrue("HC5 client seems to hang with response size equalto Long.MAX", false); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void testContentTypeDefaultsToRequestCharset() throws Exception { |
| 88 | + assumeTrue("this test is flaky on windows, but works fine.", false); |
| 89 | + } |
| 90 | + |
| 91 | + public interface TestInterface { |
| 92 | + |
| 93 | + @RequestLine("POST /test") |
| 94 | + String withBody(String body); |
| 95 | + |
| 96 | + } |
| 97 | +} |
0 commit comments