|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 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 static feign.Util.UTF_8; |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | +import static org.junit.jupiter.api.Assumptions.assumeFalse; |
| 19 | +import java.io.ByteArrayInputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Collections; |
| 22 | +import org.assertj.core.data.MapEntry; |
| 23 | +import org.junit.jupiter.api.Assumptions; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import feign.Headers; |
| 26 | +import feign.RequestLine; |
| 27 | +import feign.Response; |
| 28 | +import feign.Util; |
| 29 | +import feign.assertj.MockWebServerAssertions; |
| 30 | +import feign.client.AbstractClientTest; |
| 31 | +import okhttp3.mockwebserver.MockResponse; |
| 32 | + |
| 33 | +public abstract class AbstractJAXRSClientTest extends AbstractClientTest { |
| 34 | + |
| 35 | + @Override |
| 36 | + public void patch() throws Exception { |
| 37 | + try { |
| 38 | + super.patch(); |
| 39 | + } catch (final RuntimeException e) { |
| 40 | + Assumptions.assumeFalse(false, "JaxRS client do not support PATCH requests"); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void noResponseBodyForPut() throws Exception { |
| 46 | + try { |
| 47 | + super.noResponseBodyForPut(); |
| 48 | + } catch (final IllegalStateException e) { |
| 49 | + Assumptions.assumeFalse(false, "JaxRS client do not support empty bodies on PUT"); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void noResponseBodyForPatch() { |
| 55 | + try { |
| 56 | + super.noResponseBodyForPatch(); |
| 57 | + } catch (final IllegalStateException e) { |
| 58 | + Assumptions.assumeFalse(false, "JaxRS client do not support PATCH requests"); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + @Test |
| 64 | + public void reasonPhraseIsOptional() throws IOException, InterruptedException { |
| 65 | + server.enqueue(new MockResponse().setStatus("HTTP/1.1 " + 200)); |
| 66 | + |
| 67 | + final TestInterface api = newBuilder() |
| 68 | + .target(TestInterface.class, "http://localhost:" + server.getPort()); |
| 69 | + |
| 70 | + final Response response = api.post("foo"); |
| 71 | + |
| 72 | + assertThat(response.status()).isEqualTo(200); |
| 73 | + // jaxrsclient is creating a reason when none is present |
| 74 | + // assertThat(response.reason()).isNullOrEmpty(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + @Test |
| 79 | + public void parsesRequestAndResponse() throws IOException, InterruptedException { |
| 80 | + server.enqueue(new MockResponse().setBody("foo").addHeader("Foo: Bar")); |
| 81 | + |
| 82 | + final TestInterface api = newBuilder() |
| 83 | + .target(TestInterface.class, "http://localhost:" + server.getPort()); |
| 84 | + |
| 85 | + final Response response = api.post("foo"); |
| 86 | + |
| 87 | + assertThat(response.status()).isEqualTo(200); |
| 88 | + assertThat(response.reason()).isEqualTo("OK"); |
| 89 | + assertThat(response.headers()) |
| 90 | + .hasEntrySatisfying("Content-Length", value -> { |
| 91 | + assertThat(value).contains("3"); |
| 92 | + }).hasEntrySatisfying("Foo", value -> { |
| 93 | + assertThat(value).contains("Bar"); |
| 94 | + }); |
| 95 | + assertThat(response.body().asInputStream()) |
| 96 | + .hasSameContentAs(new ByteArrayInputStream("foo".getBytes(UTF_8))); |
| 97 | + |
| 98 | + /* queries with no values are omitted from the uri. See RFC 6750 */ |
| 99 | + MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("POST") |
| 100 | + .hasPath("/?foo=bar&foo=baz&qux") |
| 101 | + .hasBody("foo"); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void contentTypeWithoutCharset2() throws Exception { |
| 106 | + server.enqueue(new MockResponse() |
| 107 | + .setBody("AAAAAAAA")); |
| 108 | + final JaxRSClientTestInterface api = newBuilder() |
| 109 | + .target(JaxRSClientTestInterface.class, "http://localhost:" + server.getPort()); |
| 110 | + |
| 111 | + final Response response = api.getWithContentType(); |
| 112 | + // Response length should not be null |
| 113 | + assertThat(Util.toString(response.body().asReader(UTF_8))).isEqualTo("AAAAAAAA"); |
| 114 | + |
| 115 | + MockWebServerAssertions.assertThat(server.takeRequest()) |
| 116 | + .hasHeaders( |
| 117 | + MapEntry.entry("Accept", Collections.singletonList("text/plain")), |
| 118 | + MapEntry.entry("Content-Type", Collections.singletonList("text/plain"))) |
| 119 | + .hasMethod("GET"); |
| 120 | + } |
| 121 | + |
| 122 | + /* |
| 123 | + * JaxRS does not support gzip and deflate compression out-of-the-box. |
| 124 | + */ |
| 125 | + @Override |
| 126 | + public void canSupportGzip() throws Exception { |
| 127 | + assumeFalse(false, "JaxRS client do not support gzip compression"); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void canSupportGzipOnError() throws Exception { |
| 132 | + assumeFalse(false, "JaxRS client do not support gzip compression"); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void canSupportDeflate() throws Exception { |
| 137 | + assumeFalse(false, "JaxRS client do not support deflate compression"); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void canSupportDeflateOnError() throws Exception { |
| 142 | + assumeFalse(false, "JaxRS client do not support deflate compression"); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void canExceptCaseInsensitiveHeader() throws Exception { |
| 147 | + assumeFalse(false, "JaxRS client do not support gzip compression"); |
| 148 | + } |
| 149 | + |
| 150 | + public interface JaxRSClientTestInterface { |
| 151 | + |
| 152 | + @RequestLine("GET /") |
| 153 | + @Headers({"Accept: text/plain", "Content-Type: text/plain"}) |
| 154 | + Response getWithContentType(); |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | + @Override |
| 159 | + public void veryLongResponseNullLength() { |
| 160 | + assumeFalse(false, "JaxRS client hang if the response doesn't have a payload"); |
| 161 | + } |
| 162 | +} |
0 commit comments