Skip to content

[client] Fix HttpClient support for reading empty stream #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ private <T> Stream<T> stream(BodyReader<T> bodyReader) {
final HttpResponse<Stream<String>> res = handler(HttpResponse.BodyHandlers.ofLines());
this.httpResponse = res;
checkResponse(res);
return res.body().map(bodyReader::readBody);
return res.body().filter(line -> !line.isEmpty()).map(bodyReader::readBody);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ void get_stream_as() {
assertThat(first.name).isEqualTo("one");
}

@Test
void get_stream_as_when_empty() {
final HttpResponse<Stream<SimpleData>> res = clientContext.request()
.path("hello").path("streamEmpty")
.GET()
.asStream(SimpleData.class);

assertThat(res.statusCode()).isEqualTo(200);
final List<SimpleData> data = res.body().collect(Collectors.toList());
assertThat(data).isEmpty();
}

@Test
void get_stream_NotFoundException() {
clientContext.metrics(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ private void routes(JavalinDefaultRouting cfg) {
controller.stream(ctx);
});

cfg.get("/hello/streamEmpty", ctx -> {
ctx.status(200);
controller.streamEmpty(ctx);
});

cfg.get("/hello/{id}/{date}", ctx -> {
ctx.status(200);
final int id = asInt(ctx.pathParam("id"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ void stream(Context context) {
context.result(content);
}

@Get("streamEmpty")
void streamEmpty(Context context) {
// simulate x-json-stream response with empty stream
context.header("content-type", "application/x-json-stream");
context.result("\n");
}

/**
* Return the Hello DTO.
*
Expand Down