Skip to content

Commit

Permalink
refactor: don't expose StandardHttpHeaders internal Map
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed May 23, 2023
1 parent 1284872 commit 3633ea6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,4 @@ public List<String> headers(String key) {
public Map<String, List<String>> headers() {
return Collections.unmodifiableMap(headers);
}

public Map<String, List<String>> getHeaders() {
return headers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -231,29 +237,32 @@ void testRequestTimeout() {

@Test
void testMultiValueHeader() {
TestHttpResponse<Void> response = new TestHttpResponse<Void>();
response.getHeaders().put("header", Arrays.asList("a", "b"));

TestHttpResponse<Void> response = new TestHttpResponse<>(Collections.singletonMap("header", Arrays.asList("a", "b")));
assertEquals("a,b", response.header("header"));
}

@Test
void testRetryAfterParsing() {
TestHttpResponse<Void> response = new TestHttpResponse<Void>();

//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<String> 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<Arguments> 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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -30,6 +32,14 @@ public class TestHttpResponse<T> extends StandardHttpHeaders implements HttpResp
private HttpRequest request;
private HttpResponse<T> previousResponse;

public TestHttpResponse() {
super();
}

public TestHttpResponse(Map<String, List<String>> headers) {
super(headers);
}

@Override
public int code() {
return code;
Expand Down

0 comments on commit 3633ea6

Please sign in to comment.