22
22
23
23
import org .junit .Before ;
24
24
import org .junit .Test ;
25
+ import reactor .core .publisher .EmitterProcessor ;
26
+ import reactor .core .publisher .Flux ;
25
27
26
28
import org .springframework .core .MethodParameter ;
29
+ import org .springframework .core .ResolvableType ;
27
30
import org .springframework .http .ResponseEntity ;
28
31
import org .springframework .http .converter .HttpMessageConverter ;
29
32
import org .springframework .http .converter .StringHttpMessageConverter ;
@@ -95,6 +98,13 @@ public void supportsReturnTypes() throws Exception {
95
98
96
99
assertTrue (this .handler .supportsReturnType (
97
100
on (TestController .class ).resolveReturnType (ResponseEntity .class , ResponseBodyEmitter .class )));
101
+
102
+ assertTrue (this .handler .supportsReturnType (
103
+ on (TestController .class ).resolveReturnType (Flux .class , String .class )));
104
+
105
+ assertTrue (this .handler .supportsReturnType (
106
+ on (TestController .class ).resolveReturnType (forClassWithGenerics (ResponseEntity .class ,
107
+ forClassWithGenerics (Flux .class , String .class )))));
98
108
}
99
109
100
110
@ Test
@@ -103,8 +113,8 @@ public void doesNotSupportReturnTypes() throws Exception {
103
113
assertFalse (this .handler .supportsReturnType (
104
114
on (TestController .class ).resolveReturnType (ResponseEntity .class , String .class )));
105
115
106
- assertFalse (this .handler .supportsReturnType (on ( TestController . class )
107
- .resolveReturnType (forClassWithGenerics (ResponseEntity .class ,
116
+ assertFalse (this .handler .supportsReturnType (
117
+ on ( TestController . class ) .resolveReturnType (forClassWithGenerics (ResponseEntity .class ,
108
118
forClassWithGenerics (AtomicReference .class , String .class )))));
109
119
110
120
assertFalse (this .handler .supportsReturnType (
@@ -195,6 +205,27 @@ public void sseEmitter() throws Exception {
195
205
"\n " , this .response .getContentAsString ());
196
206
}
197
207
208
+ @ Test
209
+ public void responseBodyFlux () throws Exception {
210
+
211
+ this .request .addHeader ("Accept" , "text/event-stream" );
212
+
213
+ MethodParameter type = on (TestController .class ).resolveReturnType (Flux .class , String .class );
214
+ EmitterProcessor <String > processor = EmitterProcessor .create ();
215
+ this .handler .handleReturnValue (processor , type , this .mavContainer , this .webRequest );
216
+
217
+ assertTrue (this .request .isAsyncStarted ());
218
+ assertEquals (200 , this .response .getStatus ());
219
+ assertEquals ("text/event-stream;charset=UTF-8" , this .response .getContentType ());
220
+
221
+ processor .onNext ("foo" );
222
+ processor .onNext ("bar" );
223
+ processor .onNext ("baz" );
224
+ processor .onComplete ();
225
+
226
+ assertEquals ("data:foo\n \n data:bar\n \n data:baz\n \n " , this .response .getContentAsString ());
227
+ }
228
+
198
229
@ Test
199
230
public void responseEntitySse () throws Exception {
200
231
MethodParameter type = on (TestController .class ).resolveReturnType (ResponseEntity .class , SseEmitter .class );
@@ -218,6 +249,27 @@ public void responseEntitySseNoContent() throws Exception {
218
249
assertEquals (Collections .singletonList ("bar" ), this .response .getHeaders ("foo" ));
219
250
}
220
251
252
+ @ Test
253
+ public void responseEntityFlux () throws Exception {
254
+
255
+ EmitterProcessor <String > processor = EmitterProcessor .create ();
256
+ ResponseEntity <Flux <String >> entity = ResponseEntity .ok ().body (processor );
257
+ ResolvableType bodyType = forClassWithGenerics (Flux .class , String .class );
258
+ MethodParameter type = on (TestController .class ).resolveReturnType (ResponseEntity .class , bodyType );
259
+ this .handler .handleReturnValue (entity , type , this .mavContainer , this .webRequest );
260
+
261
+ assertTrue (this .request .isAsyncStarted ());
262
+ assertEquals (200 , this .response .getStatus ());
263
+ assertEquals ("text/plain" , this .response .getContentType ());
264
+
265
+ processor .onNext ("foo" );
266
+ processor .onNext ("bar" );
267
+ processor .onNext ("baz" );
268
+ processor .onComplete ();
269
+
270
+ assertEquals ("foobarbaz" , this .response .getContentAsString ());
271
+ }
272
+
221
273
222
274
@ SuppressWarnings ("unused" )
223
275
private static class TestController {
@@ -236,6 +288,10 @@ private static class TestController {
236
288
237
289
private ResponseEntity h7 () { return null ; }
238
290
291
+ private Flux <String > h8 () { return null ; }
292
+
293
+ private ResponseEntity <Flux <String >> h9 () { return null ; }
294
+
239
295
}
240
296
241
297
0 commit comments