|
16 | 16 | package software.amazon.awssdk.http.apache;
|
17 | 17 |
|
18 | 18 | import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.any; |
19 | 20 | import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
|
20 | 21 | import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
|
21 | 22 | import static org.mockito.Mockito.verify;
|
|
25 | 26 | import com.github.tomakehurst.wiremock.client.WireMock;
|
26 | 27 | import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
27 | 28 | import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
|
| 29 | +import java.io.IOException; |
28 | 30 | import java.net.HttpURLConnection;
|
| 31 | +import java.net.InetAddress; |
| 32 | +import java.net.URI; |
| 33 | +import java.net.UnknownHostException; |
29 | 34 | import org.apache.http.HttpHost;
|
30 | 35 | import org.apache.http.auth.AuthScope;
|
31 | 36 | import org.apache.http.auth.Credentials;
|
32 | 37 | import org.apache.http.auth.UsernamePasswordCredentials;
|
33 | 38 | import org.apache.http.client.CredentialsProvider;
|
| 39 | +import org.apache.http.conn.DnsResolver; |
34 | 40 | import org.apache.http.conn.HttpClientConnectionManager;
|
35 | 41 | import org.apache.http.conn.routing.HttpRoute;
|
| 42 | +import org.apache.http.impl.conn.SystemDefaultDnsResolver; |
36 | 43 | import org.junit.Rule;
|
37 | 44 | import org.junit.Test;
|
38 | 45 | import org.junit.runner.RunWith;
|
39 | 46 | import org.mockito.Mock;
|
40 | 47 | import org.mockito.runners.MockitoJUnitRunner;
|
| 48 | +import software.amazon.awssdk.http.HttpExecuteRequest; |
41 | 49 | import software.amazon.awssdk.http.SdkHttpClient;
|
42 | 50 | import software.amazon.awssdk.http.SdkHttpClientTestSuite;
|
| 51 | +import software.amazon.awssdk.http.SdkHttpFullRequest; |
| 52 | +import software.amazon.awssdk.http.SdkHttpMethod; |
43 | 53 | import software.amazon.awssdk.http.apache.internal.ApacheHttpRequestConfig;
|
44 | 54 | import software.amazon.awssdk.http.apache.internal.impl.ConnectionManagerAwareHttpClient;
|
45 | 55 | import software.amazon.awssdk.utils.AttributeMap;
|
@@ -148,4 +158,69 @@ public void clear() {
|
148 | 158 |
|
149 | 159 | mockProxyServer.verify(2, RequestPatternBuilder.allRequests());
|
150 | 160 | }
|
| 161 | + |
| 162 | + @Test |
| 163 | + public void overrideDnsResolver_WithDnsMatchingResolver_successful() throws Exception { |
| 164 | + overrideDnsResolver("magic.local.host"); |
| 165 | + } |
| 166 | + |
| 167 | + @Test(expected = UnknownHostException.class) |
| 168 | + public void overrideDnsResolver_WithUnknownHost_throwsException() throws Exception { |
| 169 | + overrideDnsResolver("sad.local.host"); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void overrideDnsResolver_WithLocalhost_successful() throws Exception { |
| 174 | + overrideDnsResolver("localhost"); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void explicitNullDnsResolver_WithLocalhost_successful() throws Exception { |
| 179 | + overrideDnsResolver("localhost", true); |
| 180 | + } |
| 181 | + |
| 182 | + private void overrideDnsResolver(String hostName) throws IOException { |
| 183 | + overrideDnsResolver(hostName, false); |
| 184 | + } |
| 185 | + |
| 186 | + private void overrideDnsResolver(String hostName, boolean nullifyResolver) throws IOException { |
| 187 | + |
| 188 | + DnsResolver dnsResolver = new SystemDefaultDnsResolver() { |
| 189 | + @Override |
| 190 | + public InetAddress[] resolve(final String host) throws UnknownHostException { |
| 191 | + if (host.equalsIgnoreCase("magic.local.host")) { |
| 192 | + return new InetAddress[] { InetAddress.getByName("127.0.0.1") }; |
| 193 | + } else { |
| 194 | + return super.resolve(host); |
| 195 | + } |
| 196 | + } |
| 197 | + }; |
| 198 | + if (nullifyResolver) { |
| 199 | + dnsResolver = null; |
| 200 | + } |
| 201 | + |
| 202 | + SdkHttpClient client = ApacheHttpClient.builder() |
| 203 | + .dnsResolver(dnsResolver) |
| 204 | + .buildWithDefaults(AttributeMap.builder() |
| 205 | + .put(TRUST_ALL_CERTIFICATES, Boolean.TRUE) |
| 206 | + .build()); |
| 207 | + |
| 208 | + mockProxyServer.resetToDefaultMappings(); |
| 209 | + mockProxyServer.stubFor(any(urlPathEqualTo("/")).willReturn(aResponse().withStatus(HttpURLConnection.HTTP_OK))); |
| 210 | + |
| 211 | + URI uri = URI.create("https://" + hostName + ":" + mockProxyServer.httpsPort()); |
| 212 | + SdkHttpFullRequest req = SdkHttpFullRequest.builder() |
| 213 | + .uri(uri) |
| 214 | + .method(SdkHttpMethod.POST) |
| 215 | + .putHeader("Host", uri.getHost()) |
| 216 | + .build(); |
| 217 | + |
| 218 | + client.prepareRequest(HttpExecuteRequest.builder() |
| 219 | + .request(req) |
| 220 | + .contentStreamProvider(req.contentStreamProvider().orElse(null)) |
| 221 | + .build()) |
| 222 | + .call(); |
| 223 | + |
| 224 | + mockProxyServer.verify(1, RequestPatternBuilder.allRequests()); |
| 225 | + } |
151 | 226 | }
|
0 commit comments