|
| 1 | +/* |
| 2 | + * Copyright 2015 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package feign.client; |
| 17 | + |
| 18 | +import com.squareup.okhttp.mockwebserver.MockResponse; |
| 19 | +import com.squareup.okhttp.mockwebserver.SocketPolicy; |
| 20 | +import com.squareup.okhttp.mockwebserver.rule.MockWebServerRule; |
| 21 | +import dagger.Lazy; |
| 22 | +import feign.Client; |
| 23 | +import feign.Feign; |
| 24 | +import feign.FeignException; |
| 25 | +import feign.Headers; |
| 26 | +import feign.RequestLine; |
| 27 | +import feign.Response; |
| 28 | +import java.io.ByteArrayInputStream; |
| 29 | +import java.io.IOException; |
| 30 | +import java.net.ProtocolException; |
| 31 | +import javax.net.ssl.HostnameVerifier; |
| 32 | +import javax.net.ssl.HttpsURLConnection; |
| 33 | +import javax.net.ssl.SSLSession; |
| 34 | +import javax.net.ssl.SSLSocketFactory; |
| 35 | +import org.junit.Rule; |
| 36 | +import org.junit.Test; |
| 37 | +import org.junit.rules.ExpectedException; |
| 38 | + |
| 39 | +import static feign.Util.UTF_8; |
| 40 | +import static feign.assertj.MockWebServerAssertions.assertThat; |
| 41 | +import static java.util.Arrays.asList; |
| 42 | +import static org.hamcrest.core.Is.isA; |
| 43 | +import static org.junit.Assert.assertEquals; |
| 44 | + |
| 45 | +public class DefaultClientTest { |
| 46 | + @Rule public final ExpectedException thrown = ExpectedException.none(); |
| 47 | + @Rule public final MockWebServerRule server = new MockWebServerRule(); |
| 48 | + |
| 49 | + interface TestInterface { |
| 50 | + @RequestLine("POST /?foo=bar&foo=baz&qux=") |
| 51 | + @Headers({"Foo: Bar", "Foo: Baz", "Qux: ", "Content-Type: text/plain"}) Response post(String body); |
| 52 | + |
| 53 | + @RequestLine("PATCH /") String patch(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test public void parsesRequestAndResponse() throws IOException, InterruptedException { |
| 57 | + server.enqueue(new MockResponse().setBody("foo").addHeader("Foo: Bar")); |
| 58 | + |
| 59 | + TestInterface api = Feign.builder().target(TestInterface.class, "http://localhost:" + server.getPort()); |
| 60 | + |
| 61 | + Response response = api.post("foo"); |
| 62 | + |
| 63 | + assertThat(response.status()).isEqualTo(200); |
| 64 | + assertThat(response.reason()).isEqualTo("OK"); |
| 65 | + assertThat(response.headers()) |
| 66 | + .containsEntry("Content-Length", asList("3")) |
| 67 | + .containsEntry("Foo", asList("Bar")); |
| 68 | + assertThat(response.body().asInputStream()).hasContentEqualTo(new ByteArrayInputStream("foo".getBytes(UTF_8))); |
| 69 | + |
| 70 | + assertThat(server.takeRequest()).hasMethod("POST") |
| 71 | + .hasPath("/?foo=bar&foo=baz&qux=") |
| 72 | + .hasHeaders("Foo: Bar", "Foo: Baz", "Qux: ", "Content-Length: 3") |
| 73 | + .hasBody("foo"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test public void parsesErrorResponse() throws IOException, InterruptedException { |
| 77 | + thrown.expect(FeignException.class); |
| 78 | + thrown.expectMessage("status 500 reading TestInterface#post(String); content:\n" + "ARGHH"); |
| 79 | + |
| 80 | + server.enqueue(new MockResponse().setResponseCode(500).setBody("ARGHH")); |
| 81 | + |
| 82 | + TestInterface api = Feign.builder().target(TestInterface.class, "http://localhost:" + server.getPort()); |
| 83 | + |
| 84 | + api.post("foo"); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * We currently don't include the <a href="http://java.net/jira/browse/JERSEY-639">60-line workaround</a> |
| 89 | + * jersey uses to overcome the lack of support for PATCH. For now, prefer okhttp. |
| 90 | + * |
| 91 | + * @see java.net.HttpURLConnection#setRequestMethod |
| 92 | + */ |
| 93 | + @Test public void patchUnsupported() throws IOException, InterruptedException { |
| 94 | + thrown.expectCause(isA(ProtocolException.class)); |
| 95 | + |
| 96 | + TestInterface api = Feign.builder().target(TestInterface.class, "http://localhost:" + server.getPort()); |
| 97 | + |
| 98 | + api.patch(); |
| 99 | + } |
| 100 | + |
| 101 | + Client trustSSLSockets = new Client.Default(new Lazy<SSLSocketFactory>() { |
| 102 | + @Override public SSLSocketFactory get() { |
| 103 | + return TrustingSSLSocketFactory.get(); |
| 104 | + } |
| 105 | + }, new Lazy<HostnameVerifier>() { |
| 106 | + @Override public HostnameVerifier get() { |
| 107 | + return HttpsURLConnection.getDefaultHostnameVerifier(); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + @Test public void canOverrideSSLSocketFactory() throws IOException, InterruptedException { |
| 112 | + server.get().useHttps(TrustingSSLSocketFactory.get("localhost"), false); |
| 113 | + server.enqueue(new MockResponse()); |
| 114 | + |
| 115 | + TestInterface api = Feign.builder() |
| 116 | + .client(trustSSLSockets) |
| 117 | + .target(TestInterface.class, "https://localhost:" + server.getPort()); |
| 118 | + |
| 119 | + api.post("foo"); |
| 120 | + } |
| 121 | + |
| 122 | + Client disableHostnameVerification = new Client.Default(new Lazy<SSLSocketFactory>() { |
| 123 | + @Override public SSLSocketFactory get() { |
| 124 | + return TrustingSSLSocketFactory.get(); |
| 125 | + } |
| 126 | + }, new Lazy<HostnameVerifier>() { |
| 127 | + @Override public HostnameVerifier get() { |
| 128 | + return new HostnameVerifier() { |
| 129 | + @Override |
| 130 | + public boolean verify(String s, SSLSession sslSession) { |
| 131 | + return true; |
| 132 | + } |
| 133 | + }; |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + @Test public void canOverrideHostnameVerifier() throws IOException, InterruptedException { |
| 138 | + server.get().useHttps(TrustingSSLSocketFactory.get("bad.example.com"), false); |
| 139 | + server.enqueue(new MockResponse()); |
| 140 | + |
| 141 | + TestInterface api = Feign.builder() |
| 142 | + .client(disableHostnameVerification) |
| 143 | + .target(TestInterface.class, "https://localhost:" + server.getPort()); |
| 144 | + |
| 145 | + api.post("foo"); |
| 146 | + } |
| 147 | + |
| 148 | + @Test public void retriesFailedHandshake() throws IOException, InterruptedException { |
| 149 | + server.get().useHttps(TrustingSSLSocketFactory.get("localhost"), false); |
| 150 | + server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE)); |
| 151 | + server.enqueue(new MockResponse()); |
| 152 | + |
| 153 | + TestInterface api = Feign.builder() |
| 154 | + .client(trustSSLSockets) |
| 155 | + .target(TestInterface.class, "https://localhost:" + server.getPort()); |
| 156 | + |
| 157 | + api.post("foo"); |
| 158 | + assertEquals(2, server.getRequestCount()); |
| 159 | + } |
| 160 | +} |
0 commit comments