20
20
import java .net .URI ;
21
21
import java .nio .charset .Charset ;
22
22
import java .text .SimpleDateFormat ;
23
+ import java .util .ArrayList ;
23
24
import java .util .Collections ;
24
25
import java .util .Date ;
26
+ import java .util .List ;
25
27
import java .util .Locale ;
26
28
import java .util .TimeZone ;
27
29
48
50
import org .springframework .web .context .request .ServletWebRequest ;
49
51
import org .springframework .web .method .support .ModelAndViewContainer ;
50
52
51
- import static org .junit .Assert .*;
52
- import static org .mockito .BDDMockito .*;
53
- import static org .springframework .web .servlet .HandlerMapping .*;
53
+ import static org .junit .Assert .assertEquals ;
54
+ import static org .junit .Assert .assertFalse ;
55
+ import static org .junit .Assert .assertTrue ;
56
+ import static org .junit .Assert .fail ;
57
+ import static org .mockito .BDDMockito .any ;
58
+ import static org .mockito .BDDMockito .eq ;
59
+ import static org .mockito .BDDMockito .given ;
60
+ import static org .mockito .BDDMockito .isA ;
61
+ import static org .mockito .BDDMockito .mock ;
62
+ import static org .mockito .BDDMockito .reset ;
63
+ import static org .mockito .BDDMockito .verify ;
64
+ import static org .springframework .web .servlet .HandlerMapping .PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE ;
54
65
55
66
/**
56
67
* Test fixture for {@link HttpEntityMethodProcessor} delegating to a mock
@@ -96,11 +107,14 @@ public void setUp() throws Exception {
96
107
97
108
messageConverter = mock (HttpMessageConverter .class );
98
109
given (messageConverter .getSupportedMediaTypes ()).willReturn (Collections .singletonList (MediaType .TEXT_PLAIN ));
99
-
100
- processor = new HttpEntityMethodProcessor (Collections .<HttpMessageConverter <?>>singletonList (messageConverter ));
110
+ List <HttpMessageConverter <?>> converters = new ArrayList <>();
111
+ converters .add (messageConverter );
112
+ processor = new HttpEntityMethodProcessor (converters );
101
113
reset (messageConverter );
102
114
103
- Method handle1 = getClass ().getMethod ("handle1" , HttpEntity .class , ResponseEntity .class , Integer .TYPE , RequestEntity .class );
115
+ Method handle1 = getClass ().getMethod ("handle1" , HttpEntity .class , ResponseEntity .class ,
116
+ Integer .TYPE , RequestEntity .class );
117
+
104
118
paramHttpEntity = new MethodParameter (handle1 , 0 );
105
119
paramRequestEntity = new MethodParameter (handle1 , 3 );
106
120
paramResponseEntity = new MethodParameter (handle1 , 1 );
@@ -205,14 +219,12 @@ public void resolveArgumentNoContentType() throws Exception {
205
219
@ Test
206
220
public void handleReturnValue () throws Exception {
207
221
String body = "Foo" ;
208
- ResponseEntity <String > returnValue = new ResponseEntity <String >(body , HttpStatus .OK );
222
+ ResponseEntity <String > returnValue = new ResponseEntity <>(body , HttpStatus .OK );
209
223
210
224
MediaType accepted = MediaType .TEXT_PLAIN ;
211
225
servletRequest .addHeader ("Accept" , accepted .toString ());
212
226
213
- given (messageConverter .canWrite (String .class , null )).willReturn (true );
214
- given (messageConverter .getSupportedMediaTypes ()).willReturn (Collections .singletonList (MediaType .TEXT_PLAIN ));
215
- given (messageConverter .canWrite (String .class , accepted )).willReturn (true );
227
+ initStringMessageConversion (accepted );
216
228
217
229
processor .handleReturnValue (returnValue , returnTypeResponseEntity , mavContainer , webRequest );
218
230
@@ -223,7 +235,7 @@ public void handleReturnValue() throws Exception {
223
235
@ Test
224
236
public void handleReturnValueProduces () throws Exception {
225
237
String body = "Foo" ;
226
- ResponseEntity <String > returnValue = new ResponseEntity <String >(body , HttpStatus .OK );
238
+ ResponseEntity <String > returnValue = new ResponseEntity <>(body , HttpStatus .OK );
227
239
228
240
servletRequest .addHeader ("Accept" , "text/*" );
229
241
servletRequest .setAttribute (PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE , Collections .singleton (MediaType .TEXT_HTML ));
@@ -263,7 +275,7 @@ public void handleReturnValueWithResponseBodyAdvice() throws Exception {
263
275
@ Test (expected = HttpMediaTypeNotAcceptableException .class )
264
276
public void handleReturnValueNotAcceptable () throws Exception {
265
277
String body = "Foo" ;
266
- ResponseEntity <String > returnValue = new ResponseEntity <String >(body , HttpStatus .OK );
278
+ ResponseEntity <String > returnValue = new ResponseEntity <>(body , HttpStatus .OK );
267
279
268
280
MediaType accepted = MediaType .APPLICATION_ATOM_XML ;
269
281
servletRequest .addHeader ("Accept" , accepted .toString ());
@@ -280,7 +292,7 @@ public void handleReturnValueNotAcceptable() throws Exception {
280
292
@ Test (expected = HttpMediaTypeNotAcceptableException .class )
281
293
public void handleReturnValueNotAcceptableProduces () throws Exception {
282
294
String body = "Foo" ;
283
- ResponseEntity <String > returnValue = new ResponseEntity <String >(body , HttpStatus .OK );
295
+ ResponseEntity <String > returnValue = new ResponseEntity <>(body , HttpStatus .OK );
284
296
285
297
MediaType accepted = MediaType .TEXT_PLAIN ;
286
298
servletRequest .addHeader ("Accept" , accepted .toString ());
@@ -298,18 +310,18 @@ public void handleReturnValueNotAcceptableProduces() throws Exception {
298
310
299
311
@ Test (expected =HttpMediaTypeNotAcceptableException .class )
300
312
public void handleReturnValueNotAcceptableParseError () throws Exception {
301
- ResponseEntity <String > returnValue = new ResponseEntity <String >("Body" , HttpStatus .ACCEPTED );
313
+ ResponseEntity <String > returnValue = new ResponseEntity <>("Body" , HttpStatus .ACCEPTED );
302
314
servletRequest .addHeader ("Accept" , "01" );
303
315
304
316
processor .handleReturnValue (returnValue , returnTypeResponseEntity , mavContainer , webRequest );
305
317
fail ("Expected exception" );
306
318
}
307
319
308
320
@ Test
309
- public void responseHeaderNoBody () throws Exception {
321
+ public void handleReturnValueResponseHeaderNoBody () throws Exception {
310
322
HttpHeaders headers = new HttpHeaders ();
311
323
headers .set ("headerName" , "headerValue" );
312
- ResponseEntity <String > returnValue = new ResponseEntity <String >(headers , HttpStatus .ACCEPTED );
324
+ ResponseEntity <String > returnValue = new ResponseEntity <>(headers , HttpStatus .ACCEPTED );
313
325
314
326
processor .handleReturnValue (returnValue , returnTypeResponseEntity , mavContainer , webRequest );
315
327
@@ -318,15 +330,12 @@ public void responseHeaderNoBody() throws Exception {
318
330
}
319
331
320
332
@ Test
321
- public void responseHeaderAndBody () throws Exception {
333
+ public void handleReturnValueResponseHeaderAndBody () throws Exception {
322
334
HttpHeaders responseHeaders = new HttpHeaders ();
323
335
responseHeaders .set ("header" , "headerValue" );
324
- ResponseEntity <String > returnValue = new ResponseEntity <String >("body" , responseHeaders , HttpStatus .ACCEPTED );
325
-
326
- given (messageConverter .canWrite (String .class , null )).willReturn (true );
327
- given (messageConverter .getSupportedMediaTypes ()).willReturn (Collections .singletonList (MediaType .TEXT_PLAIN ));
328
- given (messageConverter .canWrite (String .class , MediaType .TEXT_PLAIN )).willReturn (true );
336
+ ResponseEntity <String > returnValue = new ResponseEntity <>("body" , responseHeaders , HttpStatus .ACCEPTED );
329
337
338
+ initStringMessageConversion (MediaType .TEXT_PLAIN );
330
339
processor .handleReturnValue (returnValue , returnTypeResponseEntity , mavContainer , webRequest );
331
340
332
341
ArgumentCaptor <HttpOutputMessage > outputMessage = ArgumentCaptor .forClass (HttpOutputMessage .class );
@@ -336,18 +345,15 @@ public void responseHeaderAndBody() throws Exception {
336
345
}
337
346
338
347
@ Test
339
- public void handleReturnTypeLastModified () throws Exception {
348
+ public void handleReturnValueLastModified () throws Exception {
340
349
long currentTime = new Date ().getTime ();
341
350
long oneMinuteAgo = currentTime - (1000 * 60 );
342
351
servletRequest .addHeader (HttpHeaders .IF_MODIFIED_SINCE , dateFormat .format (currentTime ));
343
352
HttpHeaders responseHeaders = new HttpHeaders ();
344
353
responseHeaders .setDate (HttpHeaders .LAST_MODIFIED , oneMinuteAgo );
345
- ResponseEntity <String > returnValue = new ResponseEntity <String >("body" , responseHeaders , HttpStatus .OK );
346
-
347
- given (messageConverter .canWrite (String .class , null )).willReturn (true );
348
- given (messageConverter .getSupportedMediaTypes ()).willReturn (Collections .singletonList (MediaType .TEXT_PLAIN ));
349
- given (messageConverter .canWrite (String .class , MediaType .TEXT_PLAIN )).willReturn (true );
354
+ ResponseEntity <String > returnValue = new ResponseEntity <>("body" , responseHeaders , HttpStatus .OK );
350
355
356
+ initStringMessageConversion (MediaType .TEXT_PLAIN );
351
357
processor .handleReturnValue (returnValue , returnTypeResponseEntity , mavContainer , webRequest );
352
358
353
359
assertResponseNotModified ();
@@ -356,17 +362,14 @@ public void handleReturnTypeLastModified() throws Exception {
356
362
}
357
363
358
364
@ Test
359
- public void handleReturnTypeEtag () throws Exception {
365
+ public void handleReturnValueEtag () throws Exception {
360
366
String etagValue = "\" deadb33f8badf00d\" " ;
361
367
servletRequest .addHeader (HttpHeaders .IF_NONE_MATCH , etagValue );
362
368
HttpHeaders responseHeaders = new HttpHeaders ();
363
369
responseHeaders .set (HttpHeaders .ETAG , etagValue );
364
- ResponseEntity <String > returnValue = new ResponseEntity <String >("body" , responseHeaders , HttpStatus .OK );
365
-
366
- given (messageConverter .canWrite (String .class , null )).willReturn (true );
367
- given (messageConverter .getSupportedMediaTypes ()).willReturn (Collections .singletonList (MediaType .TEXT_PLAIN ));
368
- given (messageConverter .canWrite (String .class , MediaType .TEXT_PLAIN )).willReturn (true );
370
+ ResponseEntity <String > returnValue = new ResponseEntity <>("body" , responseHeaders , HttpStatus .OK );
369
371
372
+ initStringMessageConversion (MediaType .TEXT_PLAIN );
370
373
processor .handleReturnValue (returnValue , returnTypeResponseEntity , mavContainer , webRequest );
371
374
372
375
assertResponseNotModified ();
@@ -375,7 +378,7 @@ public void handleReturnTypeEtag() throws Exception {
375
378
}
376
379
377
380
@ Test
378
- public void handleReturnTypeETagAndLastModified () throws Exception {
381
+ public void handleReturnValueETagAndLastModified () throws Exception {
379
382
long currentTime = new Date ().getTime ();
380
383
long oneMinuteAgo = currentTime - (1000 * 60 );
381
384
String etagValue = "\" deadb33f8badf00d\" " ;
@@ -384,12 +387,9 @@ public void handleReturnTypeETagAndLastModified() throws Exception {
384
387
HttpHeaders responseHeaders = new HttpHeaders ();
385
388
responseHeaders .setDate (HttpHeaders .LAST_MODIFIED , oneMinuteAgo );
386
389
responseHeaders .set (HttpHeaders .ETAG , etagValue );
387
- ResponseEntity <String > returnValue = new ResponseEntity <String >("body" , responseHeaders , HttpStatus .OK );
388
-
389
- given (messageConverter .canWrite (String .class , null )).willReturn (true );
390
- given (messageConverter .getSupportedMediaTypes ()).willReturn (Collections .singletonList (MediaType .TEXT_PLAIN ));
391
- given (messageConverter .canWrite (String .class , MediaType .TEXT_PLAIN )).willReturn (true );
390
+ ResponseEntity <String > returnValue = new ResponseEntity <>("body" , responseHeaders , HttpStatus .OK );
392
391
392
+ initStringMessageConversion (MediaType .TEXT_PLAIN );
393
393
processor .handleReturnValue (returnValue , returnTypeResponseEntity , mavContainer , webRequest );
394
394
395
395
assertResponseNotModified ();
@@ -400,19 +400,16 @@ public void handleReturnTypeETagAndLastModified() throws Exception {
400
400
}
401
401
402
402
@ Test
403
- public void handleReturnTypeNotModified () throws Exception {
403
+ public void handleReturnValueNotModified () throws Exception {
404
404
long currentTime = new Date ().getTime ();
405
405
long oneMinuteAgo = currentTime - (1000 * 60 );
406
406
String etagValue = "\" deadb33f8badf00d\" " ;
407
407
HttpHeaders responseHeaders = new HttpHeaders ();
408
408
responseHeaders .setDate (HttpHeaders .LAST_MODIFIED , oneMinuteAgo );
409
409
responseHeaders .set (HttpHeaders .ETAG , etagValue );
410
- ResponseEntity <String > returnValue = new ResponseEntity <String >("body" , responseHeaders , HttpStatus .NOT_MODIFIED );
411
-
412
- given (messageConverter .canWrite (String .class , null )).willReturn (true );
413
- given (messageConverter .getSupportedMediaTypes ()).willReturn (Collections .singletonList (MediaType .TEXT_PLAIN ));
414
- given (messageConverter .canWrite (String .class , MediaType .TEXT_PLAIN )).willReturn (true );
410
+ ResponseEntity <String > returnValue = new ResponseEntity <>("body" , responseHeaders , HttpStatus .NOT_MODIFIED );
415
411
412
+ initStringMessageConversion (MediaType .TEXT_PLAIN );
416
413
processor .handleReturnValue (returnValue , returnTypeResponseEntity , mavContainer , webRequest );
417
414
418
415
assertResponseNotModified ();
@@ -422,14 +419,8 @@ public void handleReturnTypeNotModified() throws Exception {
422
419
assertEquals (etagValue , servletResponse .getHeader (HttpHeaders .ETAG ));
423
420
}
424
421
425
- private void assertResponseNotModified () {
426
- assertTrue (mavContainer .isRequestHandled ());
427
- assertEquals (HttpStatus .NOT_MODIFIED .value (), servletResponse .getStatus ());
428
- assertEquals (0 , servletResponse .getContentAsByteArray ().length );
429
- }
430
-
431
422
@ Test
432
- public void handleReturnTypeChangedETagAndLastModified () throws Exception {
423
+ public void handleReturnValueChangedETagAndLastModified () throws Exception {
433
424
long currentTime = new Date ().getTime ();
434
425
long oneMinuteAgo = currentTime - (1000 * 60 );
435
426
String etagValue = "\" deadb33f8badf00d\" " ;
@@ -439,12 +430,9 @@ public void handleReturnTypeChangedETagAndLastModified() throws Exception {
439
430
HttpHeaders responseHeaders = new HttpHeaders ();
440
431
responseHeaders .setDate (HttpHeaders .LAST_MODIFIED , oneMinuteAgo );
441
432
responseHeaders .set (HttpHeaders .ETAG , changedEtagValue );
442
- ResponseEntity <String > returnValue = new ResponseEntity <String >("body" , responseHeaders , HttpStatus .OK );
443
-
444
- given (messageConverter .canWrite (String .class , null )).willReturn (true );
445
- given (messageConverter .getSupportedMediaTypes ()).willReturn (Collections .singletonList (MediaType .TEXT_PLAIN ));
446
- given (messageConverter .canWrite (String .class , MediaType .TEXT_PLAIN )).willReturn (true );
433
+ ResponseEntity <String > returnValue = new ResponseEntity <>("body" , responseHeaders , HttpStatus .OK );
447
434
435
+ initStringMessageConversion (MediaType .TEXT_PLAIN );
448
436
processor .handleReturnValue (returnValue , returnTypeResponseEntity , mavContainer , webRequest );
449
437
450
438
assertTrue (mavContainer .isRequestHandled ());
@@ -458,19 +446,16 @@ public void handleReturnTypeChangedETagAndLastModified() throws Exception {
458
446
459
447
// SPR-13496
460
448
@ Test
461
- public void handleReturnTypePostRequestWithIfNotModified () throws Exception {
449
+ public void handleReturnValuePostRequestWithIfNotModified () throws Exception {
462
450
String wildcardValue = "*" ;
463
451
String etagValue = "\" some-etag\" " ;
464
452
servletRequest .setMethod ("POST" );
465
453
servletRequest .addHeader (HttpHeaders .IF_NONE_MATCH , wildcardValue );
466
454
HttpHeaders responseHeaders = new HttpHeaders ();
467
455
responseHeaders .set (HttpHeaders .ETAG , etagValue );
468
- ResponseEntity <String > returnValue = new ResponseEntity <String >("body" , responseHeaders , HttpStatus .OK );
469
-
470
- given (messageConverter .canWrite (String .class , null )).willReturn (true );
471
- given (messageConverter .getSupportedMediaTypes ()).willReturn (Collections .singletonList (MediaType .TEXT_PLAIN ));
472
- given (messageConverter .canWrite (String .class , MediaType .TEXT_PLAIN )).willReturn (true );
456
+ ResponseEntity <String > returnValue = new ResponseEntity <>("body" , responseHeaders , HttpStatus .OK );
473
457
458
+ initStringMessageConversion (MediaType .TEXT_PLAIN );
474
459
processor .handleReturnValue (returnValue , returnTypeResponseEntity , mavContainer , webRequest );
475
460
476
461
assertResponseOkWithBody ("body" );
@@ -480,18 +465,15 @@ public void handleReturnTypePostRequestWithIfNotModified() throws Exception {
480
465
481
466
// SPR-13626
482
467
@ Test
483
- public void handleReturnTypeGetIfNoneMatchWildcard () throws Exception {
468
+ public void handleReturnValueGetIfNoneMatchWildcard () throws Exception {
484
469
String wildcardValue = "*" ;
485
470
String etagValue = "\" some-etag\" " ;
486
471
servletRequest .addHeader (HttpHeaders .IF_NONE_MATCH , wildcardValue );
487
472
HttpHeaders responseHeaders = new HttpHeaders ();
488
473
responseHeaders .set (HttpHeaders .ETAG , etagValue );
489
- ResponseEntity <String > returnValue = new ResponseEntity <String >("body" , responseHeaders , HttpStatus .OK );
490
-
491
- given (messageConverter .canWrite (String .class , null )).willReturn (true );
492
- given (messageConverter .getSupportedMediaTypes ()).willReturn (Collections .singletonList (MediaType .TEXT_PLAIN ));
493
- given (messageConverter .canWrite (String .class , MediaType .TEXT_PLAIN )).willReturn (true );
474
+ ResponseEntity <String > returnValue = new ResponseEntity <>("body" , responseHeaders , HttpStatus .OK );
494
475
476
+ initStringMessageConversion (MediaType .TEXT_PLAIN );
495
477
processor .handleReturnValue (returnValue , returnTypeResponseEntity , mavContainer , webRequest );
496
478
497
479
assertResponseOkWithBody ("body" );
@@ -501,18 +483,15 @@ public void handleReturnTypeGetIfNoneMatchWildcard() throws Exception {
501
483
502
484
// SPR-13626
503
485
@ Test
504
- public void handleReturnTypeIfNoneMatchIfMatch () throws Exception {
486
+ public void handleReturnValueIfNoneMatchIfMatch () throws Exception {
505
487
String etagValue = "\" some-etag\" " ;
506
488
servletRequest .addHeader (HttpHeaders .IF_NONE_MATCH , etagValue );
507
489
servletRequest .addHeader (HttpHeaders .IF_MATCH , "ifmatch" );
508
490
HttpHeaders responseHeaders = new HttpHeaders ();
509
491
responseHeaders .set (HttpHeaders .ETAG , etagValue );
510
- ResponseEntity <String > returnValue = new ResponseEntity <String >("body" , responseHeaders , HttpStatus .OK );
511
-
512
- given (messageConverter .canWrite (String .class , null )).willReturn (true );
513
- given (messageConverter .getSupportedMediaTypes ()).willReturn (Collections .singletonList (MediaType .TEXT_PLAIN ));
514
- given (messageConverter .canWrite (String .class , MediaType .TEXT_PLAIN )).willReturn (true );
492
+ ResponseEntity <String > returnValue = new ResponseEntity <>("body" , responseHeaders , HttpStatus .OK );
515
493
494
+ initStringMessageConversion (MediaType .TEXT_PLAIN );
516
495
processor .handleReturnValue (returnValue , returnTypeResponseEntity , mavContainer , webRequest );
517
496
518
497
assertResponseOkWithBody ("body" );
@@ -522,30 +501,39 @@ public void handleReturnTypeIfNoneMatchIfMatch() throws Exception {
522
501
523
502
// SPR-13626
524
503
@ Test
525
- public void handleReturnTypeIfNoneMatchIfUnmodifiedSince () throws Exception {
504
+ public void handleReturnValueIfNoneMatchIfUnmodifiedSince () throws Exception {
526
505
String etagValue = "\" some-etag\" " ;
527
506
servletRequest .addHeader (HttpHeaders .IF_NONE_MATCH , etagValue );
528
507
servletRequest .addHeader (HttpHeaders .IF_UNMODIFIED_SINCE , dateFormat .format (new Date ().getTime ()));
529
508
HttpHeaders responseHeaders = new HttpHeaders ();
530
509
responseHeaders .set (HttpHeaders .ETAG , etagValue );
531
- ResponseEntity <String > returnValue = new ResponseEntity <String >("body" , responseHeaders , HttpStatus .OK );
532
-
533
- given (messageConverter .canWrite (String .class , null )).willReturn (true );
534
- given (messageConverter .getSupportedMediaTypes ()).willReturn (Collections .singletonList (MediaType .TEXT_PLAIN ));
535
- given (messageConverter .canWrite (String .class , MediaType .TEXT_PLAIN )).willReturn (true );
510
+ ResponseEntity <String > returnValue = new ResponseEntity <>("body" , responseHeaders , HttpStatus .OK );
536
511
512
+ initStringMessageConversion (MediaType .TEXT_PLAIN );
537
513
processor .handleReturnValue (returnValue , returnTypeResponseEntity , mavContainer , webRequest );
538
514
539
515
assertResponseOkWithBody ("body" );
540
516
assertEquals (1 , servletResponse .getHeaderValues (HttpHeaders .ETAG ).size ());
541
517
assertEquals (etagValue , servletResponse .getHeader (HttpHeaders .ETAG ));
542
518
}
543
519
520
+ private void initStringMessageConversion (MediaType accepted ) {
521
+ given (messageConverter .canWrite (String .class , null )).willReturn (true );
522
+ given (messageConverter .getSupportedMediaTypes ()).willReturn (Collections .singletonList (MediaType .TEXT_PLAIN ));
523
+ given (messageConverter .canWrite (String .class , accepted )).willReturn (true );
524
+ }
525
+
526
+ private void assertResponseNotModified () {
527
+ assertTrue (mavContainer .isRequestHandled ());
528
+ assertEquals (HttpStatus .NOT_MODIFIED .value (), servletResponse .getStatus ());
529
+ assertEquals (0 , servletResponse .getContentAsByteArray ().length );
530
+ }
531
+
544
532
private void assertResponseOkWithBody (String body ) throws Exception {
545
533
assertTrue (mavContainer .isRequestHandled ());
546
534
assertEquals (HttpStatus .OK .value (), servletResponse .getStatus ());
547
535
ArgumentCaptor <HttpOutputMessage > outputMessage = ArgumentCaptor .forClass (HttpOutputMessage .class );
548
- verify (messageConverter ).write (eq (" body" ), eq (MediaType .TEXT_PLAIN ), outputMessage .capture ());
536
+ verify (messageConverter ).write (eq (body ), eq (MediaType .TEXT_PLAIN ), outputMessage .capture ());
549
537
}
550
538
551
539
@ SuppressWarnings ("unused" )
0 commit comments