Skip to content

Commit 67b7684

Browse files
authored
Use mock HTTP client instead of WireMock (#3893)
1 parent c305d38 commit 67b7684

File tree

2 files changed

+116
-7
lines changed

2 files changed

+116
-7
lines changed

test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/timeout/async/AsyncApiCallTimeoutTest.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515

1616
package software.amazon.awssdk.protocol.tests.timeout.async;
1717

18-
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
19-
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
20-
import static com.github.tomakehurst.wiremock.client.WireMock.post;
21-
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
2218
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
2319
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
2420

@@ -36,7 +32,10 @@
3632
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
3733
import software.amazon.awssdk.core.exception.ApiCallTimeoutException;
3834
import software.amazon.awssdk.core.retry.RetryPolicy;
35+
import software.amazon.awssdk.http.HttpExecuteResponse;
36+
import software.amazon.awssdk.http.SdkHttpResponse;
3937
import software.amazon.awssdk.protocol.tests.timeout.BaseApiCallTimeoutTest;
38+
import software.amazon.awssdk.protocol.tests.util.MockAsyncHttpClient;
4039
import software.amazon.awssdk.regions.Region;
4140
import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient;
4241
import software.amazon.awssdk.services.protocolrestjson.model.AllTypesResponse;
@@ -104,13 +103,34 @@ protected WireMockRule wireMock() {
104103

105104
@Test
106105
public void increaseTimeoutInRequestOverrideConfig_shouldTakePrecedence() {
107-
stubFor(post(anyUrl())
108-
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_AFTER_TIMEOUT)));
106+
MockAsyncHttpClient mockClient = new MockAsyncHttpClient();
107+
ProtocolRestJsonAsyncClient asyncClient = createClientWithMockClient(mockClient);
108+
mockClient.stubNextResponse(mockResponse(200));
109+
mockClient.withFixedDelay(DELAY_AFTER_TIMEOUT);
110+
109111
CompletableFuture<AllTypesResponse> allTypesResponseCompletableFuture =
110-
client.allTypes(b -> b.overrideConfiguration(c -> c.apiCallTimeout(Duration.ofMillis(DELAY_AFTER_TIMEOUT + 1000))));
112+
asyncClient.allTypes(b -> b.overrideConfiguration(c -> c.apiCallTimeout(Duration.ofMillis(DELAY_AFTER_TIMEOUT + 1000))));
111113

112114
AllTypesResponse response = allTypesResponseCompletableFuture.join();
113115
assertThat(response).isNotNull();
114116
}
115117

118+
public ProtocolRestJsonAsyncClient createClientWithMockClient(MockAsyncHttpClient mockClient) {
119+
return ProtocolRestJsonAsyncClient.builder()
120+
.region(Region.US_WEST_1)
121+
.httpClient(mockClient)
122+
.credentialsProvider(() -> AwsBasicCredentials.create("akid", "skid"))
123+
.overrideConfiguration(b -> b.apiCallTimeout(Duration.ofMillis(TIMEOUT))
124+
.retryPolicy(RetryPolicy.none()))
125+
.build();
126+
}
127+
128+
private HttpExecuteResponse mockResponse(int statusCode) {
129+
return HttpExecuteResponse.builder()
130+
.response(SdkHttpResponse.builder()
131+
.statusCode(statusCode)
132+
.build())
133+
.build();
134+
}
135+
116136
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package software.amazon.awssdk.protocol.tests.util;
2+
3+
import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely;
4+
5+
import java.nio.ByteBuffer;
6+
import java.util.Optional;
7+
import java.util.concurrent.CompletableFuture;
8+
import org.reactivestreams.Subscriber;
9+
import org.reactivestreams.Subscription;
10+
import software.amazon.awssdk.http.HttpExecuteResponse;
11+
import software.amazon.awssdk.http.async.AsyncExecuteRequest;
12+
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
13+
import software.amazon.awssdk.http.async.SdkHttpContentPublisher;
14+
import software.amazon.awssdk.utils.IoUtils;
15+
16+
/**
17+
* Mock implementation of {@link SdkAsyncHttpClient}.
18+
*/
19+
public class MockAsyncHttpClient implements SdkAsyncHttpClient {
20+
private HttpExecuteResponse nextResponse;
21+
private long fixedDelay;
22+
23+
@Override
24+
public CompletableFuture<Void> execute(AsyncExecuteRequest request) {
25+
26+
byte[] content = nextResponse.responseBody().map(p -> invokeSafely(() -> IoUtils.toByteArray(p)))
27+
.orElseGet(() -> new byte[0]);
28+
29+
request.responseHandler().onHeaders(nextResponse.httpResponse());
30+
request.responseHandler().onStream(new ResponsePublisher(content));
31+
32+
try {
33+
Thread.sleep(fixedDelay);
34+
} catch (InterruptedException e) {
35+
}
36+
37+
return CompletableFuture.completedFuture(null);
38+
}
39+
40+
@Override
41+
public void close() {
42+
}
43+
44+
public void stubNextResponse(HttpExecuteResponse nextResponse) {
45+
this.nextResponse = nextResponse;
46+
}
47+
48+
public void withFixedDelay(long fixedDelay) {
49+
this.fixedDelay = fixedDelay;
50+
}
51+
52+
private static class ResponsePublisher implements SdkHttpContentPublisher {
53+
private final byte[] content;
54+
55+
private ResponsePublisher(byte[] content) {
56+
this.content = content;
57+
}
58+
59+
@Override
60+
public Optional<Long> contentLength() {
61+
return Optional.of((long) content.length);
62+
}
63+
64+
@Override
65+
public void subscribe(Subscriber<? super ByteBuffer> s) {
66+
s.onSubscribe(new Subscription() {
67+
private boolean running = true;
68+
69+
@Override
70+
public void request(long n) {
71+
if (n <= 0) {
72+
running = false;
73+
s.onError(new IllegalArgumentException("Demand must be positive"));
74+
} else if (running) {
75+
running = false;
76+
s.onNext(ByteBuffer.wrap(content));
77+
s.onComplete();
78+
}
79+
}
80+
81+
@Override
82+
public void cancel() {
83+
running = false;
84+
}
85+
});
86+
}
87+
}
88+
89+
}

0 commit comments

Comments
 (0)