Skip to content

Commit 7e03728

Browse files
committed
Polishing contribution
See gh-24632
1 parent cc1f578 commit 7e03728

File tree

2 files changed

+21
-17
lines changed
  • spring-webmvc/src

2 files changed

+21
-17
lines changed

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

Lines changed: 2 additions & 2 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,7 +39,7 @@
3939
*/
4040
public class SseEmitter extends ResponseBodyEmitter {
4141

42-
static final MediaType TEXT_PLAIN = new MediaType("text", "plain", StandardCharsets.UTF_8);
42+
private static final MediaType TEXT_PLAIN = new MediaType("text", "plain", StandardCharsets.UTF_8);
4343

4444
/**
4545
* Create a new SseEmitter instance.

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)