Skip to content

Commit c237338

Browse files
committed
Merge pull request #24632 from sada-sigsci/sse_content_type
Closes gh-24632
2 parents b2fe494 + 7e03728 commit c237338

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitter.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,10 +39,7 @@
3939
*/
4040
public class SseEmitter extends ResponseBodyEmitter {
4141

42-
static final MediaType TEXT_PLAIN = new MediaType("text", "plain", StandardCharsets.UTF_8);
43-
44-
static final MediaType TEXT_EVENTSTREAM = new MediaType("text", "event-stream", StandardCharsets.UTF_8);
45-
42+
private static final MediaType TEXT_PLAIN = new MediaType("text", "plain", StandardCharsets.UTF_8);
4643

4744
/**
4845
* Create a new SseEmitter instance.
@@ -70,7 +67,7 @@ protected void extendResponse(ServerHttpResponse outputMessage) {
7067

7168
HttpHeaders headers = outputMessage.getHeaders();
7269
if (headers.getContentType() == null) {
73-
headers.setContentType(TEXT_EVENTSTREAM);
70+
headers.setContentType(MediaType.TEXT_EVENT_STREAM);
7471
}
7572
}
7673

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyEmitterReturnValueHandlerTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public void sseEmitter() throws Exception {
211211
emitter.send(SseEmitter.event().
212212
comment("a test").name("update").id("1").reconnectTime(5000L).data(bean1).data(bean2));
213213

214-
assertThat(this.response.getContentType()).isEqualTo("text/event-stream;charset=UTF-8");
214+
assertThat(this.response.getContentType()).isEqualTo("text/event-stream");
215215
assertThat(this.response.getContentAsString()).isEqualTo((":a test\n" +
216216
"event:update\n" +
217217
"id:1\n" +
@@ -238,7 +238,7 @@ public void responseBodyFlux() throws Exception {
238238
processor.onNext("baz");
239239
processor.onComplete();
240240

241-
assertThat(this.response.getContentType()).isEqualTo("text/event-stream;charset=UTF-8");
241+
assertThat(this.response.getContentType()).isEqualTo("text/event-stream");
242242
assertThat(this.response.getContentAsString()).isEqualTo("data:foo\n\ndata:bar\n\ndata:baz\n\n");
243243
}
244244

@@ -272,7 +272,7 @@ public void responseEntitySse() throws Exception {
272272

273273
assertThat(this.request.isAsyncStarted()).isTrue();
274274
assertThat(this.response.getStatus()).isEqualTo(200);
275-
assertThat(this.response.getContentType()).isEqualTo("text/event-stream;charset=UTF-8");
275+
assertThat(this.response.getContentType()).isEqualTo("text/event-stream");
276276
assertThat(this.response.getHeader("foo")).isEqualTo("bar");
277277
}
278278

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitterTests.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
package org.springframework.web.servlet.mvc.method.annotation;
1717

1818
import java.io.IOException;
19+
import java.nio.charset.StandardCharsets;
1920
import java.util.ArrayList;
2021
import java.util.List;
2122
import java.util.function.Consumer;
@@ -35,6 +36,9 @@
3536
*/
3637
public class SseEmitterTests {
3738

39+
private static final MediaType TEXT_PLAIN_UTF8 = new MediaType("text", "plain", StandardCharsets.UTF_8);
40+
41+
3842
private SseEmitter emitter;
3943

4044
private TestHandler handler;
@@ -52,18 +56,18 @@ public void setup() throws IOException {
5256
public void send() throws Exception {
5357
this.emitter.send("foo");
5458
this.handler.assertSentObjectCount(3);
55-
this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN);
59+
this.handler.assertObject(0, "data:", TEXT_PLAIN_UTF8);
5660
this.handler.assertObject(1, "foo");
57-
this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN);
61+
this.handler.assertObject(2, "\n\n", TEXT_PLAIN_UTF8);
5862
}
5963

6064
@Test
6165
public void sendWithMediaType() throws Exception {
6266
this.emitter.send("foo", MediaType.TEXT_PLAIN);
6367
this.handler.assertSentObjectCount(3);
64-
this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN);
68+
this.handler.assertObject(0, "data:", TEXT_PLAIN_UTF8);
6569
this.handler.assertObject(1, "foo", MediaType.TEXT_PLAIN);
66-
this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN);
70+
this.handler.assertObject(2, "\n\n", TEXT_PLAIN_UTF8);
6771
}
6872

6973
@Test
@@ -76,40 +80,40 @@ public void sendEventEmpty() throws Exception {
7680
public void sendEventWithDataLine() throws Exception {
7781
this.emitter.send(event().data("foo"));
7882
this.handler.assertSentObjectCount(3);
79-
this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN);
83+
this.handler.assertObject(0, "data:", TEXT_PLAIN_UTF8);
8084
this.handler.assertObject(1, "foo");
81-
this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN);
85+
this.handler.assertObject(2, "\n\n", TEXT_PLAIN_UTF8);
8286
}
8387

8488
@Test
8589
public void sendEventWithTwoDataLines() throws Exception {
8690
this.emitter.send(event().data("foo").data("bar"));
8791
this.handler.assertSentObjectCount(5);
88-
this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN);
92+
this.handler.assertObject(0, "data:", TEXT_PLAIN_UTF8);
8993
this.handler.assertObject(1, "foo");
90-
this.handler.assertObject(2, "\ndata:", SseEmitter.TEXT_PLAIN);
94+
this.handler.assertObject(2, "\ndata:", TEXT_PLAIN_UTF8);
9195
this.handler.assertObject(3, "bar");
92-
this.handler.assertObject(4, "\n\n", SseEmitter.TEXT_PLAIN);
96+
this.handler.assertObject(4, "\n\n", TEXT_PLAIN_UTF8);
9397
}
9498

9599
@Test
96100
public void sendEventFull() throws Exception {
97101
this.emitter.send(event().comment("blah").name("test").reconnectTime(5000L).id("1").data("foo"));
98102
this.handler.assertSentObjectCount(3);
99-
this.handler.assertObject(0, ":blah\nevent:test\nretry:5000\nid:1\ndata:", SseEmitter.TEXT_PLAIN);
103+
this.handler.assertObject(0, ":blah\nevent:test\nretry:5000\nid:1\ndata:", TEXT_PLAIN_UTF8);
100104
this.handler.assertObject(1, "foo");
101-
this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN);
105+
this.handler.assertObject(2, "\n\n", TEXT_PLAIN_UTF8);
102106
}
103107

104108
@Test
105109
public void sendEventFullWithTwoDataLinesInTheMiddle() throws Exception {
106110
this.emitter.send(event().comment("blah").data("foo").data("bar").name("test").reconnectTime(5000L).id("1"));
107111
this.handler.assertSentObjectCount(5);
108-
this.handler.assertObject(0, ":blah\ndata:", SseEmitter.TEXT_PLAIN);
112+
this.handler.assertObject(0, ":blah\ndata:", TEXT_PLAIN_UTF8);
109113
this.handler.assertObject(1, "foo");
110-
this.handler.assertObject(2, "\ndata:", SseEmitter.TEXT_PLAIN);
114+
this.handler.assertObject(2, "\ndata:", TEXT_PLAIN_UTF8);
111115
this.handler.assertObject(3, "bar");
112-
this.handler.assertObject(4, "\nevent:test\nretry:5000\nid:1\n\n", SseEmitter.TEXT_PLAIN);
116+
this.handler.assertObject(4, "\nevent:test\nretry:5000\nid:1\n\n", TEXT_PLAIN_UTF8);
113117
}
114118

115119

0 commit comments

Comments
 (0)