Skip to content

Commit

Permalink
issue #506 have thenConsume pass on a basic response with no body so …
Browse files Browse the repository at this point in the history
…that other things like retry logic and interceptors can function properly
  • Loading branch information
ryber committed Dec 16, 2023
1 parent 81acd37 commit 55f937a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
20 changes: 20 additions & 0 deletions unirest-bdd-tests/src/test/java/BehaviorTests/RetryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -167,6 +168,18 @@ void canCustomizeRetrySignal() {
MockServer.assertRequestCount(3);
}

@Test
void whenBodyIsConsumed() {
MockServer.retryTimes(10, 429, .01);

var consumer = new ConsumingCounter();

Unirest.get(MockServer.GET)
.thenConsume(consumer);

assertEquals(10, consumer.callCount);
}

private void clearFile(Path path) {
try {
Files.delete(path);
Expand All @@ -191,4 +204,11 @@ private <R> R doWithRetry(int status, Function<HttpRequest, HttpResponse<R>> bod
return response.getBody();
}

private class ConsumingCounter implements Consumer<RawResponse> {
int callCount = 0;
@Override
public void accept(RawResponse rawResponse) {
callCount++;
}
}
}
2 changes: 1 addition & 1 deletion unirest/src/main/java/kong/unirest/core/BaseRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ private <T> CompletableFuture<HttpResponse<T>> requestAsync(HttpRequest request,
private Function<RawResponse, HttpResponse<Object>> getConsumer(Consumer<RawResponse> consumer) {
return r -> {
consumer.accept(r);
return null;
return new BasicResponse<>(r);
};
}

Expand Down
3 changes: 1 addition & 2 deletions unirest/src/main/java/kong/unirest/core/RetryStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ public Standard(int maxAttempts){

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

Expand Down

0 comments on commit 55f937a

Please sign in to comment.