Skip to content

Commit 089c8f7

Browse files
authored
Merge pull request #622 from avaje/fix/client-stream-empty
[client] Fix HttpClient support for reading empty stream
2 parents 825d35a + 64eeef1 commit 089c8f7

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

http-client/src/main/java/io/avaje/http/client/DHttpClientRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ private <T> Stream<T> stream(BodyReader<T> bodyReader) {
585585
final HttpResponse<Stream<String>> res = handler(HttpResponse.BodyHandlers.ofLines());
586586
this.httpResponse = res;
587587
checkResponse(res);
588-
return res.body().map(bodyReader::readBody);
588+
return res.body().filter(line -> !line.isEmpty()).map(bodyReader::readBody);
589589
}
590590

591591
@Override

http-client/src/test/java/io/avaje/http/client/HelloControllerTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,18 @@ void get_stream_as() {
271271
assertThat(first.name).isEqualTo("one");
272272
}
273273

274+
@Test
275+
void get_stream_as_when_empty() {
276+
final HttpResponse<Stream<SimpleData>> res = clientContext.request()
277+
.path("hello").path("streamEmpty")
278+
.GET()
279+
.asStream(SimpleData.class);
280+
281+
assertThat(res.statusCode()).isEqualTo(200);
282+
final List<SimpleData> data = res.body().collect(Collectors.toList());
283+
assertThat(data).isEmpty();
284+
}
285+
274286
@Test
275287
void get_stream_NotFoundException() {
276288
clientContext.metrics(true);

http-client/src/test/java/org/example/webserver/HelloController$Route.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ private void routes(JavalinDefaultRouting cfg) {
5252
controller.stream(ctx);
5353
});
5454

55+
cfg.get("/hello/streamEmpty", ctx -> {
56+
ctx.status(200);
57+
controller.streamEmpty(ctx);
58+
});
59+
5560
cfg.get("/hello/{id}/{date}", ctx -> {
5661
ctx.status(200);
5762
final int id = asInt(ctx.pathParam("id"));

http-client/src/test/java/org/example/webserver/HelloController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ void stream(Context context) {
6363
context.result(content);
6464
}
6565

66+
@Get("streamEmpty")
67+
void streamEmpty(Context context) {
68+
// simulate x-json-stream response with empty stream
69+
context.header("content-type", "application/x-json-stream");
70+
context.result("\n");
71+
}
72+
6673
/**
6774
* Return the Hello DTO.
6875
*

0 commit comments

Comments
 (0)