Skip to content

Commit 55f937a

Browse files
committed
issue #506 have thenConsume pass on a basic response with no body so that other things like retry logic and interceptors can function properly
1 parent 81acd37 commit 55f937a

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

unirest-bdd-tests/src/test/java/BehaviorTests/RetryTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.nio.file.StandardCopyOption;
3838
import java.util.Arrays;
3939
import java.util.List;
40+
import java.util.function.Consumer;
4041
import java.util.function.Function;
4142

4243
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -167,6 +168,18 @@ void canCustomizeRetrySignal() {
167168
MockServer.assertRequestCount(3);
168169
}
169170

171+
@Test
172+
void whenBodyIsConsumed() {
173+
MockServer.retryTimes(10, 429, .01);
174+
175+
var consumer = new ConsumingCounter();
176+
177+
Unirest.get(MockServer.GET)
178+
.thenConsume(consumer);
179+
180+
assertEquals(10, consumer.callCount);
181+
}
182+
170183
private void clearFile(Path path) {
171184
try {
172185
Files.delete(path);
@@ -191,4 +204,11 @@ private <R> R doWithRetry(int status, Function<HttpRequest, HttpResponse<R>> bod
191204
return response.getBody();
192205
}
193206

207+
private class ConsumingCounter implements Consumer<RawResponse> {
208+
int callCount = 0;
209+
@Override
210+
public void accept(RawResponse rawResponse) {
211+
callCount++;
212+
}
213+
}
194214
}

unirest/src/main/java/kong/unirest/core/BaseRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ private <T> CompletableFuture<HttpResponse<T>> requestAsync(HttpRequest request,
384384
private Function<RawResponse, HttpResponse<Object>> getConsumer(Consumer<RawResponse> consumer) {
385385
return r -> {
386386
consumer.accept(r);
387-
return null;
387+
return new BasicResponse<>(r);
388388
};
389389
}
390390

unirest/src/main/java/kong/unirest/core/RetryStrategy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ public Standard(int maxAttempts){
8787

8888
@Override
8989
public boolean isRetryable(HttpResponse response) {
90-
return response != null &&
91-
RETRY_CODES.contains(response.getStatus())
90+
return response != null && RETRY_CODES.contains(response.getStatus())
9291
&& response.getHeaders().containsKey(RETRY_AFTER);
9392
}
9493

0 commit comments

Comments
 (0)