Skip to content

Commit

Permalink
tests for retry-after
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins authored and manusa committed May 22, 2023
1 parent 125e838 commit 1284872
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ private <V> CompletableFuture<V> retryWithExponentialBackoff(
});
}

private long retryAfterMillis(HttpResponse<?> httpResponse) {
String retryAfter = httpResponse.header("Retry-After");
static long retryAfterMillis(HttpResponse<?> httpResponse) {
String retryAfter = httpResponse.header(StandardHttpHeaders.RETRY_AFTER);
if (retryAfter != null) {
try {
return Integer.parseInt(retryAfter) * 1000L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class StandardHttpHeaders implements HttpHeaders {
public static final String CONTENT_LENGTH = "Content-Length";
public static final String EXPECT = "Expect";
public static final String EXPECT_CONTINUE = "100-continue";
public static final String RETRY_AFTER = "Retry-After";

private final Map<String, List<String>> headers;

Expand All @@ -46,4 +47,8 @@ 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 @@ -25,6 +25,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -226,4 +229,31 @@ void testRequestTimeout() {
.isInstanceOf(ExecutionException.class).hasCauseInstanceOf(TimeoutException.class);
}

@Test
void testMultiValueHeader() {
TestHttpResponse<Void> response = new TestHttpResponse<Void>();
response.getHeaders().put("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));

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));

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);
}

}

0 comments on commit 1284872

Please sign in to comment.