|
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,48 @@ 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 | + |
| 229 | + try (SdkHttpClient client = createSdkHttpClient(httpClientOptions)) { |
| 230 | + // Test 429 with no retry |
| 231 | + validateStatusCodeWithRetryCheck(client, HTTP_TOO_MANY_REQUESTS, 1); |
| 232 | + |
| 233 | + // Reset and test normal request works |
| 234 | + mockServer.resetAll(); |
| 235 | + validateStatusCodeWithRetryCheck(client, HttpURLConnection.HTTP_OK, 1); |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + private void validateStatusCodeWithRetryCheck(SdkHttpClient client, |
| 240 | + int expectedStatusCode, |
| 241 | + int expectedRequestCount) throws IOException { |
| 242 | + stubForMockRequest(expectedStatusCode); |
| 243 | + SdkHttpFullRequest request = mockSdkRequest("http://localhost:" + mockServer.port(), SdkHttpMethod.POST); |
| 244 | + HttpExecuteResponse response = client.prepareRequest( |
| 245 | + HttpExecuteRequest.builder() |
| 246 | + .request(request) |
| 247 | + .contentStreamProvider(request.contentStreamProvider() |
| 248 | + .orElse(null)) |
| 249 | + .build()) |
| 250 | + .call(); |
| 251 | + validateResponseStatusCode(response, expectedStatusCode); |
| 252 | + verifyRequestCount(expectedRequestCount); |
| 253 | + } |
| 254 | + |
| 255 | + private void verifyRequestCount(int expectedCount) { |
| 256 | + mockServer.verify(expectedCount, |
| 257 | + postRequestedFor(urlEqualTo("/")) |
| 258 | + .withHeader("Host", containing("localhost"))); |
| 259 | + } |
| 260 | + |
| 261 | + private void validateResponseStatusCode(HttpExecuteResponse response, int expectedStatusCode) throws IOException { |
| 262 | + response.responseBody().ifPresent(IoUtils::drainInputStream); |
| 263 | + assertThat(response.httpResponse().statusCode()).isEqualTo(expectedStatusCode); |
| 264 | + } |
| 265 | + |
221 | 266 | protected void testForResponseCode(int returnCode) throws Exception { |
222 | 267 | testForResponseCode(returnCode, SdkHttpMethod.POST); |
223 | 268 | } |
|
0 commit comments