|
| 1 | +/* |
| 2 | + * Copyright 2017 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | +package com.adobe.aio.exception.feign; |
| 13 | + |
| 14 | +import static com.adobe.aio.util.feign.FeignTestUtils.DEFAULT_RETRY_AFTER_SECONDS_STR; |
| 15 | +import static com.adobe.aio.util.feign.FeignTestUtils.create429Response; |
| 16 | +import static com.adobe.aio.util.feign.FeignTestUtils.createResponseWithEmptyHeaders; |
| 17 | +import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | + |
| 21 | +import feign.FeignException; |
| 22 | +import feign.Response; |
| 23 | +import java.time.ZonedDateTime; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +class RetryAfterErrorTest { |
| 29 | + |
| 30 | + private static final String TEST_REQUEST_ID = "test-request-id"; |
| 31 | + private static final String TEST_METHOD = "testMethod"; |
| 32 | + private static final Logger logger = LoggerFactory.getLogger(RetryAfterErrorTest.class); |
| 33 | + |
| 34 | + @Test |
| 35 | + void testRetryAfterErrorCreation() { |
| 36 | + Response response = create429Response(DEFAULT_RETRY_AFTER_SECONDS_STR); |
| 37 | + RetryAfterError retryAfterError = createRetryAfterError(response, DEFAULT_RETRY_AFTER_SECONDS_STR); |
| 38 | + |
| 39 | + // Test basic properties |
| 40 | + assertBasicProperties(retryAfterError); |
| 41 | + |
| 42 | + // Test retry-after functionality (delay-seconds format) |
| 43 | + assertEquals(60L, retryAfterError.getRetryAfterInSeconds()); |
| 44 | + |
| 45 | + // Test request ID |
| 46 | + assertRequestIdProperties(retryAfterError); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void testRetryAfterErrorWithoutRetryAfter() { |
| 51 | + Response response = createResponseWithEmptyHeaders(429, "Too Many Requests"); |
| 52 | + RetryAfterError retryAfterError = createRetryAfterError(response, null); |
| 53 | + |
| 54 | + // Test retry-after functionality when not available |
| 55 | + assertEquals(0L, retryAfterError.getRetryAfterInSeconds()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testRetryAfterErrorWithInvalidRetryAfter() { |
| 60 | + Response response = createResponseWithEmptyHeaders(429, "Too Many Requests"); |
| 61 | + RetryAfterError retryAfterError = createRetryAfterError(response, "invalid"); |
| 62 | + |
| 63 | + // Test retry-after functionality with invalid value |
| 64 | + assertEquals(0L, retryAfterError.getRetryAfterInSeconds()); // Should return 0 for invalid values |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testRetryAfterErrorWithHttpDate() { |
| 69 | + Response response = createResponseWithEmptyHeaders(429, "Too Many Requests"); |
| 70 | + String httpDate = "Fri, 31 Dec 1999 23:59:59 GMT"; |
| 71 | + RetryAfterError retryAfterError = createRetryAfterError(response, httpDate); |
| 72 | + |
| 73 | + // Test with HTTP-date format (past date) |
| 74 | + assertEquals(0L, retryAfterError.getRetryAfterInSeconds()); // Past date should return 0 |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void testRetryAfterErrorWithFutureHttpDate() { |
| 79 | + Response response = createResponseWithEmptyHeaders(429, "Too Many Requests"); |
| 80 | + // Add a day to get a future date |
| 81 | + ZonedDateTime futureDate = ZonedDateTime.now().plusDays(1); |
| 82 | + |
| 83 | + // Format using RFC 1123 formatter |
| 84 | + String formattedFutureDate = futureDate.format(RFC_1123_DATE_TIME); |
| 85 | + logger.info("Future date: {}", formattedFutureDate); |
| 86 | + |
| 87 | + RetryAfterError retryAfterError = createRetryAfterError(response, formattedFutureDate); |
| 88 | + |
| 89 | + // Should return a positive number of seconds until the future date |
| 90 | + long retrySeconds = retryAfterError.getRetryAfterInSeconds(); |
| 91 | + assertTrue(retrySeconds > 0, "Should return positive seconds for future date"); |
| 92 | + assertEquals(24 * 60 * 60, retrySeconds); |
| 93 | + } |
| 94 | + |
| 95 | + // Helper methods for creating test objects and assertions |
| 96 | + |
| 97 | + private RetryAfterError createRetryAfterError(Response response, String retryAfter) { |
| 98 | + FeignException originalException = FeignException.errorStatus(TEST_METHOD, response); |
| 99 | + return new RetryAfterError(response, originalException, TEST_REQUEST_ID, retryAfter); |
| 100 | + } |
| 101 | + |
| 102 | + private void assertBasicProperties(RetryAfterError retryAfterError) { |
| 103 | + assertEquals(429, retryAfterError.status()); |
| 104 | + assertTrue(retryAfterError.getMessage().contains("Rate limit exceeded")); |
| 105 | + assertTrue(retryAfterError.getMessage().contains(TEST_REQUEST_ID)); |
| 106 | + assertTrue(retryAfterError.getMessage().contains(DEFAULT_RETRY_AFTER_SECONDS_STR)); |
| 107 | + } |
| 108 | + |
| 109 | + private void assertRequestIdProperties(RetryAfterError retryAfterError) { |
| 110 | + assertTrue(retryAfterError.getUpStreamRequestId().isPresent()); |
| 111 | + assertEquals(RetryAfterErrorTest.TEST_REQUEST_ID, retryAfterError.getUpStreamRequestId().get()); |
| 112 | + } |
| 113 | +} |
0 commit comments