1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
package org .springframework .web .servlet .mvc .method .annotation ;
17
17
18
18
import java .io .IOException ;
19
+ import java .nio .charset .StandardCharsets ;
19
20
import java .util .ArrayList ;
20
21
import java .util .List ;
21
22
import java .util .function .Consumer ;
35
36
*/
36
37
public class SseEmitterTests {
37
38
39
+ private static final MediaType TEXT_PLAIN_UTF8 = new MediaType ("text" , "plain" , StandardCharsets .UTF_8 );
40
+
41
+
38
42
private SseEmitter emitter ;
39
43
40
44
private TestHandler handler ;
@@ -52,18 +56,18 @@ public void setup() throws IOException {
52
56
public void send () throws Exception {
53
57
this .emitter .send ("foo" );
54
58
this .handler .assertSentObjectCount (3 );
55
- this .handler .assertObject (0 , "data:" , SseEmitter . TEXT_PLAIN );
59
+ this .handler .assertObject (0 , "data:" , TEXT_PLAIN_UTF8 );
56
60
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 );
58
62
}
59
63
60
64
@ Test
61
65
public void sendWithMediaType () throws Exception {
62
66
this .emitter .send ("foo" , MediaType .TEXT_PLAIN );
63
67
this .handler .assertSentObjectCount (3 );
64
- this .handler .assertObject (0 , "data:" , SseEmitter . TEXT_PLAIN );
68
+ this .handler .assertObject (0 , "data:" , TEXT_PLAIN_UTF8 );
65
69
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 );
67
71
}
68
72
69
73
@ Test
@@ -76,40 +80,40 @@ public void sendEventEmpty() throws Exception {
76
80
public void sendEventWithDataLine () throws Exception {
77
81
this .emitter .send (event ().data ("foo" ));
78
82
this .handler .assertSentObjectCount (3 );
79
- this .handler .assertObject (0 , "data:" , SseEmitter . TEXT_PLAIN );
83
+ this .handler .assertObject (0 , "data:" , TEXT_PLAIN_UTF8 );
80
84
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 );
82
86
}
83
87
84
88
@ Test
85
89
public void sendEventWithTwoDataLines () throws Exception {
86
90
this .emitter .send (event ().data ("foo" ).data ("bar" ));
87
91
this .handler .assertSentObjectCount (5 );
88
- this .handler .assertObject (0 , "data:" , SseEmitter . TEXT_PLAIN );
92
+ this .handler .assertObject (0 , "data:" , TEXT_PLAIN_UTF8 );
89
93
this .handler .assertObject (1 , "foo" );
90
- this .handler .assertObject (2 , "\n data:" , SseEmitter . TEXT_PLAIN );
94
+ this .handler .assertObject (2 , "\n data:" , TEXT_PLAIN_UTF8 );
91
95
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 );
93
97
}
94
98
95
99
@ Test
96
100
public void sendEventFull () throws Exception {
97
101
this .emitter .send (event ().comment ("blah" ).name ("test" ).reconnectTime (5000L ).id ("1" ).data ("foo" ));
98
102
this .handler .assertSentObjectCount (3 );
99
- this .handler .assertObject (0 , ":blah\n event:test\n retry:5000\n id:1\n data:" , SseEmitter . TEXT_PLAIN );
103
+ this .handler .assertObject (0 , ":blah\n event:test\n retry:5000\n id:1\n data:" , TEXT_PLAIN_UTF8 );
100
104
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 );
102
106
}
103
107
104
108
@ Test
105
109
public void sendEventFullWithTwoDataLinesInTheMiddle () throws Exception {
106
110
this .emitter .send (event ().comment ("blah" ).data ("foo" ).data ("bar" ).name ("test" ).reconnectTime (5000L ).id ("1" ));
107
111
this .handler .assertSentObjectCount (5 );
108
- this .handler .assertObject (0 , ":blah\n data:" , SseEmitter . TEXT_PLAIN );
112
+ this .handler .assertObject (0 , ":blah\n data:" , TEXT_PLAIN_UTF8 );
109
113
this .handler .assertObject (1 , "foo" );
110
- this .handler .assertObject (2 , "\n data:" , SseEmitter . TEXT_PLAIN );
114
+ this .handler .assertObject (2 , "\n data:" , TEXT_PLAIN_UTF8 );
111
115
this .handler .assertObject (3 , "bar" );
112
- this .handler .assertObject (4 , "\n event:test\n retry:5000\n id:1\n \n " , SseEmitter . TEXT_PLAIN );
116
+ this .handler .assertObject (4 , "\n event:test\n retry:5000\n id:1\n \n " , TEXT_PLAIN_UTF8 );
113
117
}
114
118
115
119
0 commit comments