Support multi-line comments in Server Sent Events

Prior to this commit, comments sent with Server Sent Events could break
the wire format when sent over the network when comments contained line
breaks.
While comments are mainly used for sending keepalive messages, they can
also be used for sending debug data. This commit ensures that line
breaks are properly handled in comments.

Fixes gh-36866
This commit is contained in:
Brian Clozel
2026-06-03 11:13:32 +02:00
parent fe27ad24d5
commit 86a68a77c4
4 changed files with 64 additions and 7 deletions
@@ -22,6 +22,7 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
@@ -44,6 +45,15 @@ class ServerSentEventTests {
ServerSentEvent.<String>builder().event("first" + newLine + "second").build());
}
@ParameterizedTest(name = "{1}")
@MethodSource("newLineCharacters")
void supportMultiLineComments(String newLine, String description) {
ServerSentEvent<String> event = ServerSentEvent.<String>builder()
.comment("foo" + newLine + "bar" + newLine + "baz").data("payload").build();
assertThat(event.format()).isEqualTo(":foo\n:bar\n:baz\ndata:");
}
private static Stream<Arguments> newLineCharacters() {
return Stream.of(
Arguments.of("\n", "LF"),