@@ -542,6 +542,59 @@ public void shouldHandleValidatorHeadersInPutResponses() throws Exception {
542
542
assertConditionalResponse (HttpStatus .OK , "body" , etagValue , -1 );
543
543
}
544
544
545
+ @ Test
546
+ public void varyHeader () throws Exception {
547
+ String [] entityValues = {"Accept-Language" , "User-Agent" };
548
+ String [] existingValues = {};
549
+ String [] expected = {"Accept-Language, User-Agent" };
550
+ testVaryHeader (entityValues , existingValues , expected );
551
+ }
552
+
553
+ @ Test
554
+ public void varyHeaderWithExistingWildcard () throws Exception {
555
+ String [] entityValues = {"Accept-Language" };
556
+ String [] existingValues = {"*" };
557
+ String [] expected = {"*" };
558
+ testVaryHeader (entityValues , existingValues , expected );
559
+ }
560
+
561
+ @ Test
562
+ public void varyHeaderWithExistingCommaValues () throws Exception {
563
+ String [] entityValues = {"Accept-Language" , "User-Agent" };
564
+ String [] existingValues = {"Accept-Encoding" , "Accept-Language" };
565
+ String [] expected = {"Accept-Encoding" , "Accept-Language" , "User-Agent" };
566
+ testVaryHeader (entityValues , existingValues , expected );
567
+ }
568
+
569
+ @ Test
570
+ public void varyHeaderWithExistingCommaSeparatedValues () throws Exception {
571
+ String [] entityValues = {"Accept-Language" , "User-Agent" };
572
+ String [] existingValues = {"Accept-Encoding, Accept-Language" };
573
+ String [] expected = {"Accept-Encoding, Accept-Language" , "User-Agent" };
574
+ testVaryHeader (entityValues , existingValues , expected );
575
+ }
576
+
577
+ @ Test
578
+ public void handleReturnValueVaryHeader () throws Exception {
579
+ String [] entityValues = {"Accept-Language" , "User-Agent" };
580
+ String [] existingValues = {"Accept-Encoding, Accept-Language" };
581
+ String [] expected = {"Accept-Encoding, Accept-Language" , "User-Agent" };
582
+ testVaryHeader (entityValues , existingValues , expected );
583
+ }
584
+
585
+
586
+ private void testVaryHeader (String [] entityValues , String [] existingValues , String [] expected ) throws Exception {
587
+ ResponseEntity <String > returnValue = ResponseEntity .ok ().varyBy (entityValues ).body ("Foo" );
588
+ for (String value : existingValues ) {
589
+ servletResponse .addHeader ("Vary" , value );
590
+ }
591
+ initStringMessageConversion (MediaType .TEXT_PLAIN );
592
+ processor .handleReturnValue (returnValue , returnTypeResponseEntity , mavContainer , webRequest );
593
+
594
+ assertTrue (mavContainer .isRequestHandled ());
595
+ assertEquals (Arrays .asList (expected ), servletResponse .getHeaders ("Vary" ));
596
+ verify (stringHttpMessageConverter ).write (eq ("Foo" ), eq (MediaType .TEXT_PLAIN ), isA (HttpOutputMessage .class ));
597
+ }
545
598
546
599
private void initStringMessageConversion (MediaType accepted ) {
547
600
given (stringHttpMessageConverter .canWrite (String .class , null )).willReturn (true );
@@ -554,8 +607,7 @@ private void assertResponseBody(String body) throws Exception {
554
607
verify (stringHttpMessageConverter ).write (eq (body ), eq (MediaType .TEXT_PLAIN ), outputMessage .capture ());
555
608
}
556
609
557
- private void assertConditionalResponse (HttpStatus status , String body ,
558
- String etag , long lastModified ) throws Exception {
610
+ private void assertConditionalResponse (HttpStatus status , String body , String etag , long lastModified ) throws Exception {
559
611
assertEquals (status .value (), servletResponse .getStatus ());
560
612
assertTrue (mavContainer .isRequestHandled ());
561
613
if (body != null ) {
0 commit comments