Skip to content

Commit

Permalink
sidestep NPE in the case of total connection failure
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Dec 16, 2023
1 parent f298362 commit fa54e54
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package BehaviorTests;

import kong.unirest.core.*;
import net.bytebuddy.implementation.bind.annotation.IgnoreForBinding;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down
6 changes: 5 additions & 1 deletion unirest/src/main/java/kong/unirest/core/RetryStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,16 @@ public Standard(int maxAttempts){

@Override
public boolean isRetryable(HttpResponse response) {
return RETRY_CODES.contains(response.getStatus())
return response != null &&
RETRY_CODES.contains(response.getStatus())
&& response.getHeaders().containsKey(RETRY_AFTER);
}

@Override
public long getWaitTime(HttpResponse response) {
if(response == null){
return 0;
}
String value = response.getHeaders().getFirst(RETRY_AFTER);
return parseToMillies(value);
}
Expand Down
15 changes: 15 additions & 0 deletions unirest/src/test/java/kong/unirest/core/RetryStrategyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ void parseHttpDateFormat() {
assertEquals(1000, parseToMillies("Wed, 21 Oct 2015 07:28:01 GMT"));
}

@Test
void nullRespoinseIsAFalse() {
assertFalse(new RetryStrategy.Standard(1).isRetryable(null));
}

@Test
void nullRespoinseIsZeroSeconds() {
assertEquals(0, new RetryStrategy.Standard(1).getWaitTime(null));
}

@Test
void unparseableRetruIsZeroSeconds() {
assertEquals(0, parseToMillies("Love Shack Baby"));
}

private long parseToMillies(String s) {
Headers h = new Headers();
h.add("Retry-After", s);
Expand Down

0 comments on commit fa54e54

Please sign in to comment.