diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/http/StandardHttpHeaders.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/http/StandardHttpHeaders.java index 9c8363e3535..cd21c14fd44 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/http/StandardHttpHeaders.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/http/StandardHttpHeaders.java @@ -47,8 +47,4 @@ public List headers(String key) { public Map> headers() { return Collections.unmodifiableMap(headers); } - - public Map> getHeaders() { - return headers; - } } diff --git a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/StandardHttpClientTest.java b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/StandardHttpClientTest.java index 0bb7daad816..73f4e6fa1bf 100644 --- a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/StandardHttpClientTest.java +++ b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/StandardHttpClientTest.java @@ -20,6 +20,9 @@ import org.awaitility.Awaitility; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.mockito.Mockito; import java.io.IOException; @@ -28,11 +31,14 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Arrays; +import java.util.Collections; +import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.IntStream; +import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -231,29 +237,32 @@ void testRequestTimeout() { @Test void testMultiValueHeader() { - TestHttpResponse response = new TestHttpResponse(); - response.getHeaders().put("header", Arrays.asList("a", "b")); - + TestHttpResponse response = new TestHttpResponse<>(Collections.singletonMap("header", Arrays.asList("a", "b"))); assertEquals("a,b", response.header("header")); } @Test - void testRetryAfterParsing() { - TestHttpResponse response = new TestHttpResponse(); - - //default to 0 - assertEquals(0, StandardHttpClient.retryAfterMillis(response)); + void retryAfterWithNoHeaderDefaultsToZero() { + assertEquals(0, StandardHttpClient.retryAfterMillis(new TestHttpResponse<>())); - response.getHeaders().put(StandardHttpHeaders.RETRY_AFTER, Arrays.asList("2")); - assertEquals(2000, StandardHttpClient.retryAfterMillis(response)); + } - response.getHeaders().put(StandardHttpHeaders.RETRY_AFTER, Arrays.asList("invalid")); - assertEquals(0, StandardHttpClient.retryAfterMillis(response)); + @ParameterizedTest(name = "{index}: retryAfter={0} has a retry interval between {1} and {2} milliseconds") + @MethodSource("testRetryAfterParsingData") + void testRetryAfterParsing(List retryAfter, int lowerBound, int upperBound) { + assertThat(StandardHttpClient.retryAfterMillis(new TestHttpResponse<>(Collections.singletonMap("Retry-After", retryAfter)))) + .isGreaterThanOrEqualTo(lowerBound) + .isLessThanOrEqualTo(upperBound); + } - response.getHeaders().put(StandardHttpHeaders.RETRY_AFTER, - Arrays.asList(ZonedDateTime.now().plusSeconds(10).format(DateTimeFormatter.RFC_1123_DATE_TIME))); - long after = StandardHttpClient.retryAfterMillis(response); - assertTrue(after > 1000 && after <= 10000); + public static Stream testRetryAfterParsingData() { + return Stream.of( + Arguments.of(Collections.emptyList(), 0, 0), + Arguments.of(Collections.singletonList("2"), 2000, 2000), + Arguments.of(Collections.singletonList("invalid"), 0, 0), + Arguments.of( + Collections.singletonList(ZonedDateTime.now().plusSeconds(10).format(DateTimeFormatter.RFC_1123_DATE_TIME)), 1001, + 10000)); } } diff --git a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/TestHttpResponse.java b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/TestHttpResponse.java index 90449388901..7270b3da124 100644 --- a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/TestHttpResponse.java +++ b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/TestHttpResponse.java @@ -16,6 +16,8 @@ package io.fabric8.kubernetes.client.http; import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; import java.util.Optional; /** @@ -30,6 +32,14 @@ public class TestHttpResponse extends StandardHttpHeaders implements HttpResp private HttpRequest request; private HttpResponse previousResponse; + public TestHttpResponse() { + super(); + } + + public TestHttpResponse(Map> headers) { + super(headers); + } + @Override public int code() { return code;