|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.http.nio.netty; |
| 17 | + |
| 18 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.any; |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; |
| 22 | +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
| 23 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
| 24 | +import static com.github.tomakehurst.wiremock.client.WireMock.verify; |
| 25 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; |
| 26 | +import static java.util.Collections.singletonMap; |
| 27 | +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; |
| 28 | +import static org.apache.commons.lang3.StringUtils.reverse; |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | +import static software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClientTestUtils.assertCanReceiveBasicRequest; |
| 31 | +import static software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClientTestUtils.createProvider; |
| 32 | +import static software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClientTestUtils.createRequest; |
| 33 | +import static software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClientTestUtils.makeSimpleRequest; |
| 34 | + |
| 35 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 36 | +import java.io.IOException; |
| 37 | +import java.net.URI; |
| 38 | +import java.util.concurrent.ExecutionException; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | +import java.util.concurrent.TimeoutException; |
| 41 | +import org.assertj.core.api.Condition; |
| 42 | +import org.junit.AfterClass; |
| 43 | +import org.junit.Before; |
| 44 | +import org.junit.Rule; |
| 45 | +import org.junit.Test; |
| 46 | +import org.junit.runner.RunWith; |
| 47 | +import org.mockito.junit.MockitoJUnitRunner; |
| 48 | +import software.amazon.awssdk.http.SdkHttpConfigurationOption; |
| 49 | +import software.amazon.awssdk.http.SdkHttpFullRequest; |
| 50 | +import software.amazon.awssdk.http.SdkHttpMethod; |
| 51 | +import software.amazon.awssdk.http.SdkHttpRequest; |
| 52 | +import software.amazon.awssdk.http.async.AsyncExecuteRequest; |
| 53 | +import software.amazon.awssdk.http.async.SdkAsyncHttpClient; |
| 54 | +import software.amazon.awssdk.utils.AttributeMap; |
| 55 | + |
| 56 | +@RunWith(MockitoJUnitRunner.class) |
| 57 | +public class NettyNioAsyncHttpClientNonBlockingDnsTest { |
| 58 | + |
| 59 | + private final RecordingNetworkTrafficListener wiremockTrafficListener = new RecordingNetworkTrafficListener(); |
| 60 | + |
| 61 | + private static final SdkAsyncHttpClient client = NettyNioAsyncHttpClient.builder() |
| 62 | + .buildWithDefaults( |
| 63 | + AttributeMap.builder() |
| 64 | + .put(SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, true) |
| 65 | + .put(SdkHttpConfigurationOption.USE_NONBLOCKING_DNS_RESOLVER, true) |
| 66 | + .build()); |
| 67 | + |
| 68 | + @Rule |
| 69 | + public WireMockRule mockServer = new WireMockRule(wireMockConfig() |
| 70 | + .dynamicPort() |
| 71 | + .dynamicHttpsPort() |
| 72 | + .networkTrafficListener(wiremockTrafficListener)); |
| 73 | + |
| 74 | + @Before |
| 75 | + public void methodSetup() { |
| 76 | + wiremockTrafficListener.reset(); |
| 77 | + } |
| 78 | + |
| 79 | + @AfterClass |
| 80 | + public static void tearDown() throws Exception { |
| 81 | + client.close(); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void useNonBlockingDnsResolver_shouldHonor() { |
| 86 | + try (NettyNioAsyncHttpClient client = (NettyNioAsyncHttpClient) NettyNioAsyncHttpClient.builder() |
| 87 | + .build()) { |
| 88 | + assertThat(client.configuration().isNonBlockingResolver()).isEqualTo(false); |
| 89 | + } |
| 90 | + |
| 91 | + try (NettyNioAsyncHttpClient client = (NettyNioAsyncHttpClient) NettyNioAsyncHttpClient.builder() |
| 92 | + .useNonBlockingDnsResolver(false) |
| 93 | + .build()) { |
| 94 | + assertThat(client.configuration().isNonBlockingResolver()).isEqualTo(false); |
| 95 | + } |
| 96 | + |
| 97 | + try (NettyNioAsyncHttpClient client = (NettyNioAsyncHttpClient) NettyNioAsyncHttpClient.builder() |
| 98 | + .useNonBlockingDnsResolver(true) |
| 99 | + .build()) { |
| 100 | + assertThat(client.configuration().isNonBlockingResolver()).isEqualTo(true); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void canSendContentAndGetThatContentBackNonBlockingDns() throws Exception { |
| 106 | + String body = randomAlphabetic(50); |
| 107 | + stubFor(any(urlEqualTo("/echo?reversed=true")) |
| 108 | + .withRequestBody(equalTo(body)) |
| 109 | + .willReturn(aResponse().withBody(reverse(body)))); |
| 110 | + URI uri = URI.create("http://localhost:" + mockServer.port()); |
| 111 | + |
| 112 | + SdkHttpRequest request = createRequest(uri, "/echo", body, SdkHttpMethod.POST, singletonMap("reversed", "true")); |
| 113 | + |
| 114 | + RecordingResponseHandler recorder = new RecordingResponseHandler(); |
| 115 | + |
| 116 | + client.execute(AsyncExecuteRequest.builder().request(request).requestContentPublisher(createProvider(body)).responseHandler(recorder).build()); |
| 117 | + |
| 118 | + recorder.completeFuture.get(5, TimeUnit.SECONDS); |
| 119 | + |
| 120 | + verify(1, postRequestedFor(urlEqualTo("/echo?reversed=true"))); |
| 121 | + |
| 122 | + assertThat(recorder.fullResponseAsString()).isEqualTo(reverse(body)); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void defaultThreadFactoryUsesHelpfulName() throws Exception { |
| 127 | + // Make a request to ensure a thread is primed |
| 128 | + makeSimpleRequest(client, mockServer); |
| 129 | + |
| 130 | + String expectedPattern = "aws-java-sdk-NettyEventLoop-\\d+-\\d+"; |
| 131 | + assertThat(Thread.getAllStackTraces().keySet()) |
| 132 | + .areAtLeast(1, new Condition<>(t -> t.getName().matches(expectedPattern), |
| 133 | + "Matches default thread pattern: `%s`", expectedPattern)); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void canMakeBasicRequestOverHttp() throws Exception { |
| 138 | + String smallBody = randomAlphabetic(10); |
| 139 | + URI uri = URI.create("http://localhost:" + mockServer.port()); |
| 140 | + |
| 141 | + assertCanReceiveBasicRequest(client, uri, smallBody); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void canMakeBasicRequestOverHttps() throws Exception { |
| 146 | + String smallBody = randomAlphabetic(10); |
| 147 | + URI uri = URI.create("https://localhost:" + mockServer.httpsPort()); |
| 148 | + |
| 149 | + assertCanReceiveBasicRequest(client, uri, smallBody); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void canHandleLargerPayloadsOverHttp() throws Exception { |
| 154 | + String largishBody = randomAlphabetic(25000); |
| 155 | + |
| 156 | + URI uri = URI.create("http://localhost:" + mockServer.port()); |
| 157 | + |
| 158 | + assertCanReceiveBasicRequest(client, uri, largishBody); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void canHandleLargerPayloadsOverHttps() throws Exception { |
| 163 | + String largishBody = randomAlphabetic(25000); |
| 164 | + |
| 165 | + URI uri = URI.create("https://localhost:" + mockServer.httpsPort()); |
| 166 | + |
| 167 | + assertCanReceiveBasicRequest(client, uri, largishBody); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void requestContentOnlyEqualToContentLengthHeaderFromProvider() throws InterruptedException, ExecutionException, TimeoutException, IOException { |
| 172 | + final String content = randomAlphabetic(32); |
| 173 | + final String streamContent = content + reverse(content); |
| 174 | + stubFor(any(urlEqualTo("/echo?reversed=true")) |
| 175 | + .withRequestBody(equalTo(content)) |
| 176 | + .willReturn(aResponse().withBody(reverse(content)))); |
| 177 | + URI uri = URI.create("http://localhost:" + mockServer.port()); |
| 178 | + |
| 179 | + SdkHttpFullRequest request = createRequest(uri, "/echo", streamContent, SdkHttpMethod.POST, singletonMap("reversed", "true")); |
| 180 | + request = request.toBuilder().putHeader("Content-Length", Integer.toString(content.length())).build(); |
| 181 | + RecordingResponseHandler recorder = new RecordingResponseHandler(); |
| 182 | + |
| 183 | + client.execute(AsyncExecuteRequest.builder().request(request).requestContentPublisher(createProvider(streamContent)).responseHandler(recorder).build()); |
| 184 | + |
| 185 | + recorder.completeFuture.get(5, TimeUnit.SECONDS); |
| 186 | + |
| 187 | + // HTTP servers will stop processing the request as soon as it reads |
| 188 | + // bytes equal to 'Content-Length' so we need to inspect the raw |
| 189 | + // traffic to ensure that there wasn't anything after that. |
| 190 | + assertThat(wiremockTrafficListener.requests().toString()).endsWith(content); |
| 191 | + } |
| 192 | +} |
0 commit comments