Skip to content

Commit 3596705

Browse files
committed
Remove test which are specific to apache http
1 parent 85efdaf commit 3596705

File tree

3 files changed

+88
-99
lines changed

3 files changed

+88
-99
lines changed

http-clients/apache-client/src/test/java/software/amazon/awssdk/http/apache/ApacheHttpClientWireMockTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.github.tomakehurst.wiremock.client.WireMock.any;
2020
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
2121
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
22+
import static org.assertj.core.api.Assertions.assertThat;
2223
import static org.mockito.Mockito.verify;
2324
import static org.mockito.Mockito.when;
2425
import static software.amazon.awssdk.http.SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES;
@@ -46,13 +47,15 @@
4647
import org.mockito.Mock;
4748
import org.mockito.junit.MockitoJUnitRunner;
4849
import software.amazon.awssdk.http.HttpExecuteRequest;
50+
import software.amazon.awssdk.http.HttpExecuteResponse;
4951
import software.amazon.awssdk.http.SdkHttpClient;
5052
import software.amazon.awssdk.http.SdkHttpClientTestSuite;
5153
import software.amazon.awssdk.http.SdkHttpFullRequest;
5254
import software.amazon.awssdk.http.SdkHttpMethod;
5355
import software.amazon.awssdk.http.apache.internal.ApacheHttpRequestConfig;
5456
import software.amazon.awssdk.http.apache.internal.impl.ConnectionManagerAwareHttpClient;
5557
import software.amazon.awssdk.utils.AttributeMap;
58+
import software.amazon.awssdk.utils.IoUtils;
5659

5760
@RunWith(MockitoJUnitRunner.class)
5861
public class ApacheHttpClientWireMockTest extends SdkHttpClientTestSuite {
@@ -179,6 +182,45 @@ public void explicitNullDnsResolver_WithLocalhost_successful() throws Exception
179182
overrideDnsResolver("localhost", true);
180183
}
181184

185+
@Test
186+
public void handlesVariousContentLengths() throws Exception {
187+
SdkHttpClient client = createSdkHttpClient();
188+
int[] contentLengths = {0, 1, 100, 1024, 65536};
189+
190+
for (int length : contentLengths) {
191+
String path = "/content-length-" + length;
192+
byte[] body = new byte[length];
193+
for (int i = 0; i < length; i++) {
194+
body[i] = (byte) ('A' + (i % 26));
195+
}
196+
197+
mockServer.stubFor(any(urlPathEqualTo(path))
198+
.willReturn(aResponse()
199+
.withStatus(200)
200+
.withHeader("Content-Length", String.valueOf(length))
201+
.withBody(body)));
202+
203+
SdkHttpFullRequest req = mockSdkRequest("http://localhost:" + mockServer.port() + path, SdkHttpMethod.GET);
204+
HttpExecuteResponse rsp = client.prepareRequest(HttpExecuteRequest.builder()
205+
.request(req)
206+
.build())
207+
.call();
208+
209+
assertThat(rsp.httpResponse().statusCode()).isEqualTo(200);
210+
211+
if (length == 0) {
212+
// Empty body should still have a response body present, but EOF immediately
213+
if (rsp.responseBody().isPresent()) {
214+
assertThat(rsp.responseBody().get().read()).isEqualTo(-1);
215+
}
216+
} else {
217+
assertThat(rsp.responseBody()).isPresent();
218+
byte[] readBody = IoUtils.toByteArray(rsp.responseBody().get());
219+
assertThat(readBody).isEqualTo(body);
220+
}
221+
}
222+
}
223+
182224
private void overrideDnsResolver(String hostName) throws IOException {
183225
overrideDnsResolver(hostName, false);
184226
}

http-clients/apache5-client/src/test/java/software/amazon/awssdk/http/apache5/Apache5HttpClientWireMockTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.github.tomakehurst.wiremock.client.WireMock.any;
2020
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
2121
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
22+
import static org.assertj.core.api.Assertions.assertThat;
2223
import static org.mockito.Mockito.verify;
2324
import static org.mockito.Mockito.when;
2425
import static software.amazon.awssdk.http.SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES;
@@ -47,13 +48,15 @@
4748
import org.mockito.Mock;
4849
import org.mockito.junit.MockitoJUnitRunner;
4950
import software.amazon.awssdk.http.HttpExecuteRequest;
51+
import software.amazon.awssdk.http.HttpExecuteResponse;
5052
import software.amazon.awssdk.http.SdkHttpClient;
5153
import software.amazon.awssdk.http.SdkHttpClientTestSuite;
5254
import software.amazon.awssdk.http.SdkHttpFullRequest;
5355
import software.amazon.awssdk.http.SdkHttpMethod;
5456
import software.amazon.awssdk.http.apache5.internal.Apache5HttpRequestConfig;
5557
import software.amazon.awssdk.http.apache5.internal.impl.ConnectionManagerAwareHttpClient;
5658
import software.amazon.awssdk.utils.AttributeMap;
59+
import software.amazon.awssdk.utils.IoUtils;
5760

5861
@RunWith(MockitoJUnitRunner.class)
5962
public class Apache5HttpClientWireMockTest extends SdkHttpClientTestSuite {
@@ -173,6 +176,47 @@ public void explicitNullDnsResolver_WithLocalhost_successful() throws Exception
173176
overrideDnsResolver("localhost", true);
174177
}
175178

179+
180+
181+
@Test
182+
public void handlesVariousContentLengths() throws Exception {
183+
SdkHttpClient client = createSdkHttpClient();
184+
int[] contentLengths = {0, 1, 100, 1024, 65536};
185+
186+
for (int length : contentLengths) {
187+
String path = "/content-length-" + length;
188+
byte[] body = new byte[length];
189+
for (int i = 0; i < length; i++) {
190+
body[i] = (byte) ('A' + (i % 26));
191+
}
192+
193+
mockServer.stubFor(any(urlPathEqualTo(path))
194+
.willReturn(aResponse()
195+
.withStatus(200)
196+
.withHeader("Content-Length", String.valueOf(length))
197+
.withBody(body)));
198+
199+
SdkHttpFullRequest req = mockSdkRequest("http://localhost:" + mockServer.port() + path, SdkHttpMethod.GET);
200+
HttpExecuteResponse rsp = client.prepareRequest(HttpExecuteRequest.builder()
201+
.request(req)
202+
.build())
203+
.call();
204+
205+
assertThat(rsp.httpResponse().statusCode()).isEqualTo(200);
206+
207+
if (length == 0) {
208+
// Empty body should still have a response body present, but EOF immediately
209+
if (rsp.responseBody().isPresent()) {
210+
assertThat(rsp.responseBody().get().read()).isEqualTo(-1);
211+
}
212+
} else {
213+
assertThat(rsp.responseBody()).isPresent();
214+
byte[] readBody = IoUtils.toByteArray(rsp.responseBody().get());
215+
assertThat(readBody).isEqualTo(body);
216+
}
217+
}
218+
}
219+
176220
private void overrideDnsResolver(String hostName) throws IOException {
177221
overrideDnsResolver(hostName, false);
178222
}

test/http-client-tests/src/main/java/software/amazon/awssdk/http/SdkHttpClientTestSuite.java

Lines changed: 2 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -238,52 +238,6 @@ public void doesNotRetryOn429StatusCode() throws Exception {
238238
}
239239
}
240240

241-
@Test
242-
public void handlesNoContentResponse() throws Exception {
243-
SdkHttpClient client = createSdkHttpClient();
244-
245-
mockServer.stubFor(any(urlPathEqualTo("/no-content"))
246-
.willReturn(aResponse()
247-
.withStatus(204)));
248-
249-
SdkHttpFullRequest req = mockSdkRequest("http://localhost:" + mockServer.port() + "/no-content",
250-
SdkHttpMethod.DELETE);
251-
HttpExecuteResponse rsp = client.prepareRequest(HttpExecuteRequest.builder()
252-
.request(req)
253-
.build())
254-
.call();
255-
256-
assertThat(rsp.httpResponse().statusCode()).isEqualTo(204);
257-
assertThat(rsp.responseBody()).isEmpty();
258-
}
259-
260-
@Test
261-
public void handlesLargeResponseBody() throws Exception {
262-
SdkHttpClient client = createSdkHttpClient();
263-
// Create a large response body (1MB)
264-
byte[] largeBody = new byte[1024 * 1024];
265-
for (int i = 0; i < largeBody.length; i++) {
266-
largeBody[i] = (byte) (i % 256);
267-
}
268-
mockServer.stubFor(any(urlPathEqualTo("/large"))
269-
.willReturn(aResponse()
270-
.withStatus(200)
271-
.withBody(largeBody)));
272-
273-
SdkHttpFullRequest req = mockSdkRequest("http://localhost:" + mockServer.port() + "/large", SdkHttpMethod.GET);
274-
HttpExecuteResponse rsp = client.prepareRequest(HttpExecuteRequest.builder()
275-
.request(req)
276-
.build())
277-
.call();
278-
279-
assertThat(rsp.httpResponse().statusCode()).isEqualTo(200);
280-
assertThat(rsp.responseBody()).isPresent();
281-
282-
// Read the entire response and verify
283-
byte[] readBuffer = IoUtils.toByteArray(rsp.responseBody().get());
284-
assertThat(readBuffer).isEqualTo(largeBody);
285-
}
286-
287241
@Test
288242
public void testAbortResponseStream() throws Exception {
289243
SdkHttpClient client = createSdkHttpClient();
@@ -340,46 +294,6 @@ public void handlesMultipleSequentialRequests() throws Exception {
340294
mockServer.verify(5, getRequestedFor(urlEqualTo("/sequential")));
341295
}
342296

343-
@Test
344-
public void handlesVariousContentLengths() throws Exception {
345-
SdkHttpClient client = createSdkHttpClient();
346-
int[] contentLengths = {0, 1, 100, 1024, 65536};
347-
348-
for (int length : contentLengths) {
349-
String path = "/content-length-" + length;
350-
byte[] body = new byte[length];
351-
for (int i = 0; i < length; i++) {
352-
body[i] = (byte) ('A' + (i % 26));
353-
}
354-
355-
mockServer.stubFor(any(urlPathEqualTo(path))
356-
.willReturn(aResponse()
357-
.withStatus(200)
358-
.withHeader("Content-Length", String.valueOf(length))
359-
.withBody(body)));
360-
361-
SdkHttpFullRequest req = mockSdkRequest("http://localhost:" + mockServer.port() + path, SdkHttpMethod.GET);
362-
HttpExecuteResponse rsp = client.prepareRequest(HttpExecuteRequest.builder()
363-
.request(req)
364-
.build())
365-
.call();
366-
367-
assertThat(rsp.httpResponse().statusCode()).isEqualTo(200);
368-
369-
if (length == 0) {
370-
// Empty body should still have a response body present, but EOF immediately
371-
if (rsp.responseBody().isPresent()) {
372-
assertThat(rsp.responseBody().get().read()).isEqualTo(-1);
373-
}
374-
} else {
375-
assertThat(rsp.responseBody()).isPresent();
376-
byte[] readBody = IoUtils.toByteArray(rsp.responseBody().get());
377-
assertThat(readBody).isEqualTo(body);
378-
}
379-
}
380-
}
381-
382-
383297
private void validateStatusCodeWithRetryCheck(SdkHttpClient client,
384298
int expectedStatusCode,
385299
int expectedRequestCount) throws IOException {
@@ -504,22 +418,11 @@ private static SdkHttpFullRequest.Builder mockSdkRequestBuilder(String uriString
504418
}
505419

506420
protected SdkHttpFullRequest mockSdkRequest(String uriString, SdkHttpMethod method) {
507-
URI uri = URI.create(uriString);
508-
SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder()
509-
.uri(uri)
510-
.method(method)
511-
.putHeader("Host", uri.getHost())
512-
.putHeader("User-Agent", "hello-world!");
513-
514-
// Only add body for methods that typically have a body
515-
if (method != SdkHttpMethod.HEAD && method != SdkHttpMethod.GET && method != SdkHttpMethod.DELETE) {
421+
SdkHttpFullRequest.Builder requestBuilder = mockSdkRequestBuilder(uriString, method);
422+
if (method != SdkHttpMethod.HEAD) {
516423
byte[] content = "Body".getBytes(StandardCharsets.UTF_8);
517424
requestBuilder.putHeader("Content-Length", Integer.toString(content.length));
518425
requestBuilder.contentStreamProvider(() -> new ByteArrayInputStream(content));
519-
} else if (method == SdkHttpMethod.GET || method == SdkHttpMethod.DELETE) {
520-
// For GET and DELETE, explicitly set Content-Length to 0 or don't set it at all
521-
// Some clients like AWS CRT are strict about this
522-
requestBuilder.contentStreamProvider(() -> new ByteArrayInputStream(new byte[0]));
523426
}
524427

525428
return requestBuilder.build();

0 commit comments

Comments
 (0)