Fix SSE with indenting serializer in WebMvc.fn

This commit ensures that HTTP headers like "text/event-stream"
are correctly forwarded to the converter used in
SseServerResponse for proper pretty print handling.

Close gh-30277
This commit is contained in:
Sébastien Deleuze
2023-04-07 11:25:47 +02:00
parent 310344cf61
commit b5b115e52c
2 changed files with 38 additions and 4 deletions

View File

@@ -33,6 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Arjen Poutsma
* @author Sebastien Deleuze
*/
class SseServerResponseTests {
@@ -89,6 +90,33 @@ class SseServerResponseTests {
assertThat(this.mockResponse.getContentAsString()).isEqualTo(expected);
}
@Test
void sendObjectWithPrettyPrint() throws Exception {
Person person = new Person("John Doe", 42);
ServerResponse response = ServerResponse.sse(sse -> {
try {
sse.send(person);
}
catch (IOException ex) {
throw new UncheckedIOException(ex);
}
});
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setPrettyPrint(true);
ServerResponse.Context context = () -> Collections.singletonList(converter);
ModelAndView mav = response.writeTo(this.mockRequest, this.mockResponse, context);
assertThat(mav).isNull();
String expected = "data:{\n" +
"data: \"name\" : \"John Doe\",\n" +
"data: \"age\" : 42\n" +
"data:}\n" +
"\n";
assertThat(this.mockResponse.getContentAsString()).isEqualTo(expected);
}
@Test
void builder() throws Exception {
ServerResponse response = ServerResponse.sse(sse -> {