|
20 | 20 | import static com.github.tomakehurst.wiremock.client.WireMock.any;
|
21 | 21 | import static com.github.tomakehurst.wiremock.client.WireMock.containing;
|
22 | 22 | import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
|
| 23 | +import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; |
| 24 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
23 | 25 | import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
|
24 | 26 | import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
|
25 | 27 | import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
|
@@ -59,6 +61,7 @@ public abstract class SdkHttpClientTestSuite {
|
59 | 61 | private static final Logger LOG = Logger.loggerFor(SdkHttpClientTestSuite.class);
|
60 | 62 |
|
61 | 63 | private static final ConnectionCountingTrafficListener CONNECTION_COUNTER = new ConnectionCountingTrafficListener();
|
| 64 | + private static final int HTTP_TOO_MANY_REQUESTS = 429; |
62 | 65 |
|
63 | 66 | @Rule
|
64 | 67 | public WireMockRule mockServer = createWireMockRule();
|
@@ -218,6 +221,47 @@ public void testCustomTlsTrustManagerAndTrustAllFails() throws Exception {
|
218 | 221 | assertThatThrownBy(() -> createSdkHttpClient(httpClientOptions)).isInstanceOf(IllegalArgumentException.class);
|
219 | 222 | }
|
220 | 223 |
|
| 224 | + @Test |
| 225 | + public void doesNotRetryOn429StatusCode() throws Exception { |
| 226 | + SdkHttpClientOptions httpClientOptions = new SdkHttpClientOptions(); |
| 227 | + httpClientOptions.trustAll(true); |
| 228 | + try (SdkHttpClient client = createSdkHttpClient(httpClientOptions)) { |
| 229 | + stubForMockRequest(HTTP_TOO_MANY_REQUESTS); |
| 230 | + SdkHttpFullRequest req = mockSdkRequest("http://localhost:" + mockServer.port(), SdkHttpMethod.POST); |
| 231 | + HttpExecuteResponse response = client.prepareRequest(HttpExecuteRequest.builder() |
| 232 | + .request(req) |
| 233 | + .contentStreamProvider(req.contentStreamProvider().orElse(null)) |
| 234 | + .build()) |
| 235 | + .call(); |
| 236 | + // Drain the response body if present |
| 237 | + response.responseBody().ifPresent(IoUtils::drainInputStream); |
| 238 | + |
| 239 | + // Verify the response has 429 status code |
| 240 | + assertThat(response.httpResponse().statusCode()).isEqualTo(429); |
| 241 | + |
| 242 | + // Verify that the request was made exactly once (no retries) |
| 243 | + mockServer.verify(1, postRequestedFor(urlEqualTo("/")) |
| 244 | + .withHeader("Host", containing("localhost"))); |
| 245 | + |
| 246 | + // Reset and make another request to ensure it's not a connection issue |
| 247 | + mockServer.resetAll(); |
| 248 | + stubForMockRequest(200); |
| 249 | + HttpExecuteResponse successResponse = client.prepareRequest(HttpExecuteRequest.builder() |
| 250 | + .request(req) |
| 251 | + .contentStreamProvider(req.contentStreamProvider().orElse(null)) |
| 252 | + .build()) |
| 253 | + .call(); |
| 254 | + successResponse.responseBody().ifPresent(IoUtils::drainInputStream); |
| 255 | + |
| 256 | + // Verify the second request succeeded |
| 257 | + assertThat(successResponse.httpResponse().statusCode()).isEqualTo(200); |
| 258 | + |
| 259 | + // Verify only one request was made for each call (no retries) |
| 260 | + mockServer.verify(1, postRequestedFor(urlEqualTo("/"))); |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + |
221 | 265 | protected void testForResponseCode(int returnCode) throws Exception {
|
222 | 266 | testForResponseCode(returnCode, SdkHttpMethod.POST);
|
223 | 267 | }
|
|
0 commit comments