|
| 1 | +/** |
| 2 | + * Copyright 2012-2018 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.jaxrs2; |
| 15 | + |
| 16 | +import feign.Feign.Builder; |
| 17 | +import feign.Headers; |
| 18 | +import feign.RequestLine; |
| 19 | +import feign.Response; |
| 20 | +import feign.Util; |
| 21 | +import feign.assertj.MockWebServerAssertions; |
| 22 | +import feign.client.AbstractClientTest; |
| 23 | +import feign.jaxrs2.JAXRSClient; |
| 24 | +import feign.Feign; |
| 25 | +import okhttp3.mockwebserver.MockResponse; |
| 26 | +import org.junit.Test; |
| 27 | +import java.io.ByteArrayInputStream; |
| 28 | +import java.io.IOException; |
| 29 | +import javax.ws.rs.ProcessingException; |
| 30 | +import static feign.Util.UTF_8; |
| 31 | +import static java.util.Arrays.asList; |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | +import static org.junit.Assert.assertEquals; |
| 34 | +import org.junit.Assume; |
| 35 | + |
| 36 | +/** Tests client-specific behavior, such as ensuring Content-Length is sent when specified. */ |
| 37 | +public class JAXRSClientTest extends AbstractClientTest { |
| 38 | + |
| 39 | + @Override |
| 40 | + public Builder newBuilder() { |
| 41 | + return Feign.builder().client(new JAXRSClient()); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void testPatch() throws Exception { |
| 46 | + try { |
| 47 | + super.testPatch(); |
| 48 | + } catch (final ProcessingException e) { |
| 49 | + Assume.assumeNoException("JaxRS client do not support PATCH requests", e); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void noResponseBodyForPut() { |
| 55 | + try { |
| 56 | + super.noResponseBodyForPut(); |
| 57 | + } catch (final IllegalStateException e) { |
| 58 | + Assume.assumeNoException("JaxRS client do not support empty bodies on PUT", e); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void reasonPhraseIsOptional() throws IOException, InterruptedException { |
| 64 | + server.enqueue(new MockResponse().setStatus("HTTP/1.1 " + 200)); |
| 65 | + |
| 66 | + final TestInterface api = newBuilder() |
| 67 | + .target(TestInterface.class, "http://localhost:" + server.getPort()); |
| 68 | + |
| 69 | + final Response response = api.post("foo"); |
| 70 | + |
| 71 | + assertThat(response.status()).isEqualTo(200); |
| 72 | + // jaxrsclient is creating a reason when none is present |
| 73 | + // assertThat(response.reason()).isNullOrEmpty(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void parsesRequestAndResponse() throws IOException, InterruptedException { |
| 78 | + server.enqueue(new MockResponse().setBody("foo").addHeader("Foo: Bar")); |
| 79 | + |
| 80 | + final TestInterface api = newBuilder() |
| 81 | + .target(TestInterface.class, "http://localhost:" + server.getPort()); |
| 82 | + |
| 83 | + final Response response = api.post("foo"); |
| 84 | + |
| 85 | + assertThat(response.status()).isEqualTo(200); |
| 86 | + assertThat(response.reason()).isEqualTo("OK"); |
| 87 | + assertThat(response.headers()) |
| 88 | + .containsEntry("Content-Length", asList("3")) |
| 89 | + .containsEntry("Foo", asList("Bar")); |
| 90 | + assertThat(response.body().asInputStream()) |
| 91 | + .hasContentEqualTo(new ByteArrayInputStream("foo".getBytes(UTF_8))); |
| 92 | + |
| 93 | + MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("POST") |
| 94 | + .hasPath("/?foo=bar&foo=baz&qux=") |
| 95 | + .hasBody("foo"); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testContentTypeWithoutCharset2() throws Exception { |
| 100 | + server.enqueue(new MockResponse() |
| 101 | + .setBody("AAAAAAAA")); |
| 102 | + final JaxRSClientTestInterface api = newBuilder() |
| 103 | + .target(JaxRSClientTestInterface.class, "http://localhost:" + server.getPort()); |
| 104 | + |
| 105 | + final Response response = api.getWithContentType(); |
| 106 | + // Response length should not be null |
| 107 | + assertEquals("AAAAAAAA", Util.toString(response.body().asReader())); |
| 108 | + |
| 109 | + MockWebServerAssertions.assertThat(server.takeRequest()) |
| 110 | + .hasHeaders("Accept: text/plain", "Content-Type: text/plain") // Note: OkHttp adds content |
| 111 | + // length. |
| 112 | + .hasMethod("GET"); |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + public interface JaxRSClientTestInterface { |
| 117 | + |
| 118 | + @RequestLine("GET /") |
| 119 | + @Headers({"Accept: text/plain", "Content-Type: text/plain"}) |
| 120 | + Response getWithContentType(); |
| 121 | + } |
| 122 | +} |
0 commit comments