Merge pull request #24632 from sada-sigsci/sse_content_type
Closes gh-24632
This commit is contained in:
commit
c237338d8f
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -39,10 +39,7 @@ import org.springframework.util.StringUtils;
|
|||
*/
|
||||
public class SseEmitter extends ResponseBodyEmitter {
|
||||
|
||||
static final MediaType TEXT_PLAIN = new MediaType("text", "plain", StandardCharsets.UTF_8);
|
||||
|
||||
static final MediaType TEXT_EVENTSTREAM = new MediaType("text", "event-stream", StandardCharsets.UTF_8);
|
||||
|
||||
private static final MediaType TEXT_PLAIN = new MediaType("text", "plain", StandardCharsets.UTF_8);
|
||||
|
||||
/**
|
||||
* Create a new SseEmitter instance.
|
||||
|
@ -70,7 +67,7 @@ public class SseEmitter extends ResponseBodyEmitter {
|
|||
|
||||
HttpHeaders headers = outputMessage.getHeaders();
|
||||
if (headers.getContentType() == null) {
|
||||
headers.setContentType(TEXT_EVENTSTREAM);
|
||||
headers.setContentType(MediaType.TEXT_EVENT_STREAM);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -211,7 +211,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
|||
emitter.send(SseEmitter.event().
|
||||
comment("a test").name("update").id("1").reconnectTime(5000L).data(bean1).data(bean2));
|
||||
|
||||
assertThat(this.response.getContentType()).isEqualTo("text/event-stream;charset=UTF-8");
|
||||
assertThat(this.response.getContentType()).isEqualTo("text/event-stream");
|
||||
assertThat(this.response.getContentAsString()).isEqualTo((":a test\n" +
|
||||
"event:update\n" +
|
||||
"id:1\n" +
|
||||
|
@ -238,7 +238,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
|||
processor.onNext("baz");
|
||||
processor.onComplete();
|
||||
|
||||
assertThat(this.response.getContentType()).isEqualTo("text/event-stream;charset=UTF-8");
|
||||
assertThat(this.response.getContentType()).isEqualTo("text/event-stream");
|
||||
assertThat(this.response.getContentAsString()).isEqualTo("data:foo\n\ndata:bar\n\ndata:baz\n\n");
|
||||
}
|
||||
|
||||
|
@ -272,7 +272,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
|||
|
||||
assertThat(this.request.isAsyncStarted()).isTrue();
|
||||
assertThat(this.response.getStatus()).isEqualTo(200);
|
||||
assertThat(this.response.getContentType()).isEqualTo("text/event-stream;charset=UTF-8");
|
||||
assertThat(this.response.getContentType()).isEqualTo("text/event-stream");
|
||||
assertThat(this.response.getHeader("foo")).isEqualTo("bar");
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -16,6 +16,7 @@
|
|||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
@ -35,6 +36,9 @@ import static org.springframework.web.servlet.mvc.method.annotation.SseEmitter.e
|
|||
*/
|
||||
public class SseEmitterTests {
|
||||
|
||||
private static final MediaType TEXT_PLAIN_UTF8 = new MediaType("text", "plain", StandardCharsets.UTF_8);
|
||||
|
||||
|
||||
private SseEmitter emitter;
|
||||
|
||||
private TestHandler handler;
|
||||
|
@ -52,18 +56,18 @@ public class SseEmitterTests {
|
|||
public void send() throws Exception {
|
||||
this.emitter.send("foo");
|
||||
this.handler.assertSentObjectCount(3);
|
||||
this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN);
|
||||
this.handler.assertObject(0, "data:", TEXT_PLAIN_UTF8);
|
||||
this.handler.assertObject(1, "foo");
|
||||
this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN);
|
||||
this.handler.assertObject(2, "\n\n", TEXT_PLAIN_UTF8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendWithMediaType() throws Exception {
|
||||
this.emitter.send("foo", MediaType.TEXT_PLAIN);
|
||||
this.handler.assertSentObjectCount(3);
|
||||
this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN);
|
||||
this.handler.assertObject(0, "data:", TEXT_PLAIN_UTF8);
|
||||
this.handler.assertObject(1, "foo", MediaType.TEXT_PLAIN);
|
||||
this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN);
|
||||
this.handler.assertObject(2, "\n\n", TEXT_PLAIN_UTF8);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -76,40 +80,40 @@ public class SseEmitterTests {
|
|||
public void sendEventWithDataLine() throws Exception {
|
||||
this.emitter.send(event().data("foo"));
|
||||
this.handler.assertSentObjectCount(3);
|
||||
this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN);
|
||||
this.handler.assertObject(0, "data:", TEXT_PLAIN_UTF8);
|
||||
this.handler.assertObject(1, "foo");
|
||||
this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN);
|
||||
this.handler.assertObject(2, "\n\n", TEXT_PLAIN_UTF8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendEventWithTwoDataLines() throws Exception {
|
||||
this.emitter.send(event().data("foo").data("bar"));
|
||||
this.handler.assertSentObjectCount(5);
|
||||
this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN);
|
||||
this.handler.assertObject(0, "data:", TEXT_PLAIN_UTF8);
|
||||
this.handler.assertObject(1, "foo");
|
||||
this.handler.assertObject(2, "\ndata:", SseEmitter.TEXT_PLAIN);
|
||||
this.handler.assertObject(2, "\ndata:", TEXT_PLAIN_UTF8);
|
||||
this.handler.assertObject(3, "bar");
|
||||
this.handler.assertObject(4, "\n\n", SseEmitter.TEXT_PLAIN);
|
||||
this.handler.assertObject(4, "\n\n", TEXT_PLAIN_UTF8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendEventFull() throws Exception {
|
||||
this.emitter.send(event().comment("blah").name("test").reconnectTime(5000L).id("1").data("foo"));
|
||||
this.handler.assertSentObjectCount(3);
|
||||
this.handler.assertObject(0, ":blah\nevent:test\nretry:5000\nid:1\ndata:", SseEmitter.TEXT_PLAIN);
|
||||
this.handler.assertObject(0, ":blah\nevent:test\nretry:5000\nid:1\ndata:", TEXT_PLAIN_UTF8);
|
||||
this.handler.assertObject(1, "foo");
|
||||
this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN);
|
||||
this.handler.assertObject(2, "\n\n", TEXT_PLAIN_UTF8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendEventFullWithTwoDataLinesInTheMiddle() throws Exception {
|
||||
this.emitter.send(event().comment("blah").data("foo").data("bar").name("test").reconnectTime(5000L).id("1"));
|
||||
this.handler.assertSentObjectCount(5);
|
||||
this.handler.assertObject(0, ":blah\ndata:", SseEmitter.TEXT_PLAIN);
|
||||
this.handler.assertObject(0, ":blah\ndata:", TEXT_PLAIN_UTF8);
|
||||
this.handler.assertObject(1, "foo");
|
||||
this.handler.assertObject(2, "\ndata:", SseEmitter.TEXT_PLAIN);
|
||||
this.handler.assertObject(2, "\ndata:", TEXT_PLAIN_UTF8);
|
||||
this.handler.assertObject(3, "bar");
|
||||
this.handler.assertObject(4, "\nevent:test\nretry:5000\nid:1\n\n", SseEmitter.TEXT_PLAIN);
|
||||
this.handler.assertObject(4, "\nevent:test\nretry:5000\nid:1\n\n", TEXT_PLAIN_UTF8);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue