23
23
import java .util .Map ;
24
24
25
25
import com .fasterxml .jackson .databind .ObjectMapper ;
26
+ import org .junit .Before ;
26
27
import org .junit .Test ;
27
28
import org .reactivestreams .Publisher ;
28
29
import reactor .core .publisher .Flux ;
31
32
32
33
import org .springframework .core .ResolvableType ;
33
34
import org .springframework .core .io .buffer .AbstractDataBufferAllocatingTestCase ;
35
+ import org .springframework .core .io .buffer .DataBufferUtils ;
36
+ import org .springframework .core .io .buffer .support .DataBufferTestUtils ;
34
37
import org .springframework .http .MediaType ;
35
38
import org .springframework .http .codec .json .Jackson2JsonEncoder ;
36
39
import org .springframework .http .converter .json .Jackson2ObjectMapperBuilder ;
45
48
* @author Sebastien Deleuze
46
49
* @author Rossen Stoyanchev
47
50
*/
51
+ @ SuppressWarnings ("rawtypes" )
48
52
public class ServerSentEventHttpMessageWriterTests extends AbstractDataBufferAllocatingTestCase {
49
53
50
54
private static final Map <String , Object > HINTS = Collections .emptyMap ();
51
55
52
56
private ServerSentEventHttpMessageWriter messageWriter =
53
57
new ServerSentEventHttpMessageWriter (new Jackson2JsonEncoder ());
54
58
59
+ private MockServerHttpResponse outputMessage ;
60
+
61
+
62
+ @ Before
63
+ public void setUp () {
64
+ this .outputMessage = new MockServerHttpResponse (this .bufferFactory );
65
+ }
66
+
67
+
55
68
56
69
@ Test
57
70
public void canWrite () {
@@ -72,53 +85,44 @@ public void writeServerSentEvent() {
72
85
.comment ("bla\n bla bla\n bla bla bla" ).retry (Duration .ofMillis (123L )).build ();
73
86
74
87
Mono <ServerSentEvent > source = Mono .just (event );
75
- MockServerHttpResponse outputMessage = new MockServerHttpResponse ();
76
88
testWrite (source , outputMessage , ServerSentEvent .class );
77
89
78
- StepVerifier .create (outputMessage .getBodyAsString ())
79
- .expectNext ("id:c42\n event:foo\n retry:123\n :bla\n :bla bla\n :bla bla bla\n data:bar\n \n " )
90
+ StepVerifier .create (outputMessage .getBody ())
91
+ .consumeNextWith (stringConsumer ("id:c42\n event:foo\n retry:123\n :bla\n :bla bla\n :bla bla bla\n data:" ))
92
+ .consumeNextWith (stringConsumer ("bar\n " ))
93
+ .consumeNextWith (stringConsumer ("\n " ))
80
94
.expectComplete ()
81
95
.verify ();
82
96
}
83
97
84
- @ Test
85
- @ SuppressWarnings ("rawtypes" )
86
- public void writeServerSentEventError () {
87
- ServerSentEvent <?> event = ServerSentEvent .builder ().data ("bar" ).id ("c42" ).event ("foo" )
88
- .comment ("bla\n bla bla\n bla bla bla" ).retry (Duration .ofMillis (123L )).build ();
89
-
90
- Flux <ServerSentEvent > source = Flux .concat (
91
- Flux .just (event ),
92
- Flux .error (new RuntimeException ()));
93
- MockServerHttpResponse outputMessage = new MockServerHttpResponse ();
94
-
95
- Mono <Void > result = this .messageWriter .write (source , forClass (ServerSentEvent .class ),
96
- MediaType .TEXT_EVENT_STREAM , outputMessage , HINTS );
97
-
98
- StepVerifier .create (result )
99
- .verifyError (RuntimeException .class );
100
- }
101
-
102
98
@ Test
103
99
public void writeString () {
104
100
Flux <String > source = Flux .just ("foo" , "bar" );
105
- MockServerHttpResponse outputMessage = new MockServerHttpResponse ();
106
101
testWrite (source , outputMessage , String .class );
107
102
108
- StepVerifier .create (outputMessage .getBodyAsString ())
109
- .expectNext ("data:foo\n \n data:bar\n \n " )
103
+ StepVerifier .create (outputMessage .getBody ())
104
+ .consumeNextWith (stringConsumer ("data:" ))
105
+ .consumeNextWith (stringConsumer ("foo\n " ))
106
+ .consumeNextWith (stringConsumer ("\n " ))
107
+ .consumeNextWith (stringConsumer ("data:" ))
108
+ .consumeNextWith (stringConsumer ("bar\n " ))
109
+ .consumeNextWith (stringConsumer ("\n " ))
110
110
.expectComplete ()
111
111
.verify ();
112
112
}
113
113
114
114
@ Test
115
115
public void writeMultiLineString () {
116
116
Flux <String > source = Flux .just ("foo\n bar" , "foo\n baz" );
117
- MockServerHttpResponse outputMessage = new MockServerHttpResponse ();
118
117
testWrite (source , outputMessage , String .class );
119
118
120
- StepVerifier .create (outputMessage .getBodyAsString ())
121
- .expectNext ("data:foo\n data:bar\n \n data:foo\n data:baz\n \n " )
119
+ StepVerifier .create (outputMessage .getBody ())
120
+ .consumeNextWith (stringConsumer ("data:" ))
121
+ .consumeNextWith (stringConsumer ("foo\n data:bar\n " ))
122
+ .consumeNextWith (stringConsumer ("\n " ))
123
+ .consumeNextWith (stringConsumer ("data:" ))
124
+ .consumeNextWith (stringConsumer ("foo\n data:baz\n " ))
125
+ .consumeNextWith (stringConsumer ("\n " ))
122
126
.expectComplete ()
123
127
.verify ();
124
128
}
@@ -128,22 +132,36 @@ public void writeStringWithCustomCharset() {
128
132
Flux <String > source = Flux .just ("\u00A3 " );
129
133
Charset charset = StandardCharsets .ISO_8859_1 ;
130
134
MediaType mediaType = new MediaType ("text" , "event-stream" , charset );
131
- MockServerHttpResponse outputMessage = new MockServerHttpResponse ();
132
135
testWrite (source , mediaType , outputMessage , String .class );
133
136
134
137
assertEquals (mediaType , outputMessage .getHeaders ().getContentType ());
135
- StepVerifier .create (outputMessage .getBodyAsString ()).expectNext ("data:\u00A3 \n \n " ).verifyComplete ();
138
+ StepVerifier .create (outputMessage .getBody ())
139
+ .consumeNextWith (stringConsumer ("data:" ))
140
+ .consumeNextWith (dataBuffer -> {
141
+ String value =
142
+ DataBufferTestUtils .dumpString (dataBuffer , charset );
143
+ DataBufferUtils .release (dataBuffer );
144
+ assertEquals ("\u00A3 \n " , value );
145
+ })
146
+ .consumeNextWith (stringConsumer ("\n " ))
147
+ .expectComplete ()
148
+ .verify ();
136
149
}
137
150
138
151
@ Test
139
152
public void writePojo () {
140
153
Flux <Pojo > source = Flux .just (new Pojo ("foofoo" , "barbar" ), new Pojo ("foofoofoo" , "barbarbar" ));
141
- MockServerHttpResponse outputMessage = new MockServerHttpResponse ();
142
154
testWrite (source , outputMessage , Pojo .class );
143
155
144
- StepVerifier .create (outputMessage .getBodyAsString ())
145
- .expectNext ("data:{\" foo\" :\" foofoo\" ,\" bar\" :\" barbar\" }\n \n " +
146
- "data:{\" foo\" :\" foofoofoo\" ,\" bar\" :\" barbarbar\" }\n \n " )
156
+ StepVerifier .create (outputMessage .getBody ())
157
+ .consumeNextWith (stringConsumer ("data:" ))
158
+ .consumeNextWith (stringConsumer ("{\" foo\" :\" foofoo\" ,\" bar\" :\" barbar\" }" ))
159
+ .consumeNextWith (stringConsumer ("\n " ))
160
+ .consumeNextWith (stringConsumer ("\n " ))
161
+ .consumeNextWith (stringConsumer ("data:" ))
162
+ .consumeNextWith (stringConsumer ("{\" foo\" :\" foofoofoo\" ,\" bar\" :\" barbarbar\" }" ))
163
+ .consumeNextWith (stringConsumer ("\n " ))
164
+ .consumeNextWith (stringConsumer ("\n " ))
147
165
.expectComplete ()
148
166
.verify ();
149
167
}
@@ -154,16 +172,21 @@ public void writePojoWithPrettyPrint() {
154
172
this .messageWriter = new ServerSentEventHttpMessageWriter (new Jackson2JsonEncoder (mapper ));
155
173
156
174
Flux <Pojo > source = Flux .just (new Pojo ("foofoo" , "barbar" ), new Pojo ("foofoofoo" , "barbarbar" ));
157
- MockServerHttpResponse outputMessage = new MockServerHttpResponse ();
158
175
testWrite (source , outputMessage , Pojo .class );
159
176
160
- StepVerifier .create (outputMessage .getBodyAsString ())
161
- .expectNext ("data:{\n " +
177
+ StepVerifier .create (outputMessage .getBody ())
178
+ .consumeNextWith (stringConsumer ("data:" ))
179
+ .consumeNextWith (stringConsumer ("{\n " +
162
180
"data: \" foo\" : \" foofoo\" ,\n " +
163
- "data: \" bar\" : \" barbar\" \n " + "data:}\n \n " +
164
- "data:{\n " +
181
+ "data: \" bar\" : \" barbar\" \n " + "data:}" ))
182
+ .consumeNextWith (stringConsumer ("\n " ))
183
+ .consumeNextWith (stringConsumer ("\n " ))
184
+ .consumeNextWith (stringConsumer ("data:" ))
185
+ .consumeNextWith (stringConsumer ("{\n " +
165
186
"data: \" foo\" : \" foofoofoo\" ,\n " +
166
- "data: \" bar\" : \" barbarbar\" \n " + "data:}\n \n " )
187
+ "data: \" bar\" : \" barbarbar\" \n " + "data:}" ))
188
+ .consumeNextWith (stringConsumer ("\n " ))
189
+ .consumeNextWith (stringConsumer ("\n " ))
167
190
.expectComplete ()
168
191
.verify ();
169
192
}
@@ -173,13 +196,35 @@ public void writePojoWithCustomEncoding() {
173
196
Flux <Pojo > source = Flux .just (new Pojo ("foo\uD834 \uDD1E " , "bar\uD834 \uDD1E " ));
174
197
Charset charset = StandardCharsets .UTF_16LE ;
175
198
MediaType mediaType = new MediaType ("text" , "event-stream" , charset );
176
- MockServerHttpResponse outputMessage = new MockServerHttpResponse ();
177
199
testWrite (source , mediaType , outputMessage , Pojo .class );
178
200
179
201
assertEquals (mediaType , outputMessage .getHeaders ().getContentType ());
180
- StepVerifier .create (outputMessage .getBodyAsString ())
181
- .expectNext ("data:{\" foo\" :\" foo\uD834 \uDD1E \" ,\" bar\" :\" bar\uD834 \uDD1E \" }\n \n " )
182
- .verifyComplete ();
202
+ StepVerifier .create (outputMessage .getBody ())
203
+ .consumeNextWith (dataBuffer1 -> {
204
+ String value1 =
205
+ DataBufferTestUtils .dumpString (dataBuffer1 , charset );
206
+ DataBufferUtils .release (dataBuffer1 );
207
+ assertEquals ("data:" , value1 );
208
+ })
209
+ .consumeNextWith (dataBuffer -> {
210
+ String value = DataBufferTestUtils .dumpString (dataBuffer , charset );
211
+ DataBufferUtils .release (dataBuffer );
212
+ assertEquals ("{\" foo\" :\" foo\uD834 \uDD1E \" ,\" bar\" :\" bar\uD834 \uDD1E \" }" , value );
213
+ })
214
+ .consumeNextWith (dataBuffer2 -> {
215
+ String value2 =
216
+ DataBufferTestUtils .dumpString (dataBuffer2 , charset );
217
+ DataBufferUtils .release (dataBuffer2 );
218
+ assertEquals ("\n " , value2 );
219
+ })
220
+ .consumeNextWith (dataBuffer3 -> {
221
+ String value3 =
222
+ DataBufferTestUtils .dumpString (dataBuffer3 , charset );
223
+ DataBufferUtils .release (dataBuffer3 );
224
+ assertEquals ("\n " , value3 );
225
+ })
226
+ .expectComplete ()
227
+ .verify ();
183
228
}
184
229
185
230
0 commit comments