49
49
import org .springframework .web .server .session .DefaultWebSessionManager ;
50
50
import org .springframework .web .server .session .WebSessionManager ;
51
51
52
- import static org .junit .Assert .assertEquals ;
53
- import static org .junit .Assert .assertNull ;
54
- import static org .junit .Assert .assertSame ;
55
- import static org .junit .Assert .assertTrue ;
56
- import static org .junit .Assert .fail ;
57
- import static org .springframework .web .reactive .HandlerMapping .PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE ;
52
+ import static org .junit .Assert .*;
53
+ import static org .springframework .web .reactive .HandlerMapping .*;
58
54
59
55
/**
60
56
* Unit tests for {@link ResourceWebHandler}.
57
+ *
61
58
* @author Rossen Stoyanchev
62
59
*/
63
60
public class ResourceWebHandlerTests {
@@ -75,7 +72,6 @@ public class ResourceWebHandlerTests {
75
72
76
73
@ Before
77
74
public void setUp () throws Exception {
78
-
79
75
List <Resource > paths = new ArrayList <>(2 );
80
76
paths .add (new ClassPathResource ("test/" , getClass ()));
81
77
paths .add (new ClassPathResource ("testalternatepath/" , getClass ()));
@@ -90,6 +86,7 @@ public void setUp() throws Exception {
90
86
this .exchange = new DefaultServerWebExchange (this .request , this .response , this .sessionManager );
91
87
}
92
88
89
+
93
90
@ Test
94
91
public void getResource () throws Exception {
95
92
this .exchange .getAttributes ().put (PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE , "foo.css" );
@@ -100,7 +97,7 @@ public void getResource() throws Exception {
100
97
assertEquals (17 , headers .getContentLength ());
101
98
assertEquals ("max-age=3600" , headers .getCacheControl ());
102
99
assertTrue (headers .containsKey ("Last-Modified" ));
103
- assertEquals (headers .getLastModified (), resourceLastModifiedDate ("test/foo.css" ));
100
+ assertEquals (headers .getLastModified () / 1000 , resourceLastModifiedDate ("test/foo.css" ) / 1000 );
104
101
assertEquals ("bytes" , headers .getFirst ("Accept-Ranges" ));
105
102
assertEquals (1 , headers .get ("Accept-Ranges" ).size ());
106
103
assertResponseBody ("h1 { color:red; }" );
@@ -118,7 +115,7 @@ public void getResourceHttpHeader() throws Exception {
118
115
assertEquals (17 , headers .getContentLength ());
119
116
assertEquals ("max-age=3600" , headers .getCacheControl ());
120
117
assertTrue (headers .containsKey ("Last-Modified" ));
121
- assertEquals (headers .getLastModified (), resourceLastModifiedDate ("test/foo.css" ));
118
+ assertEquals (headers .getLastModified () / 1000 , resourceLastModifiedDate ("test/foo.css" ) / 1000 );
122
119
assertEquals ("bytes" , headers .getFirst ("Accept-Ranges" ));
123
120
assertEquals (1 , headers .get ("Accept-Ranges" ).size ());
124
121
assertNull (this .response .getBody ());
@@ -142,7 +139,7 @@ public void getResourceNoCache() throws Exception {
142
139
143
140
assertEquals ("no-store" , this .response .getHeaders ().getCacheControl ());
144
141
assertTrue (this .response .getHeaders ().containsKey ("Last-Modified" ));
145
- assertEquals (this .response .getHeaders ().getLastModified (), resourceLastModifiedDate ("test/foo.css" ));
142
+ assertEquals (this .response .getHeaders ().getLastModified () / 1000 , resourceLastModifiedDate ("test/foo.css" ) / 1000 );
146
143
assertEquals ("bytes" , this .response .getHeaders ().getFirst ("Accept-Ranges" ));
147
144
assertEquals (1 , this .response .getHeaders ().get ("Accept-Ranges" ).size ());
148
145
}
@@ -172,7 +169,7 @@ public void getResourceWithHtmlMediaType() throws Exception {
172
169
assertEquals (MediaType .TEXT_HTML , headers .getContentType ());
173
170
assertEquals ("max-age=3600" , headers .getCacheControl ());
174
171
assertTrue (headers .containsKey ("Last-Modified" ));
175
- assertEquals (headers .getLastModified (), resourceLastModifiedDate ("test/foo.html" ));
172
+ assertEquals (headers .getLastModified () / 1000 , resourceLastModifiedDate ("test/foo.html" ) / 1000 );
176
173
assertEquals ("bytes" , headers .getFirst ("Accept-Ranges" ));
177
174
assertEquals (1 , headers .get ("Accept-Ranges" ).size ());
178
175
}
@@ -187,7 +184,7 @@ public void getResourceFromAlternatePath() throws Exception {
187
184
assertEquals (17 , headers .getContentLength ());
188
185
assertEquals ("max-age=3600" , headers .getCacheControl ());
189
186
assertTrue (headers .containsKey ("Last-Modified" ));
190
- assertEquals (headers .getLastModified (), resourceLastModifiedDate ("testalternatepath/baz.css" ));
187
+ assertEquals (headers .getLastModified () / 1000 , resourceLastModifiedDate ("testalternatepath/baz.css" ) / 1000 );
191
188
assertEquals ("bytes" , headers .getFirst ("Accept-Ranges" ));
192
189
assertEquals (1 , headers .get ("Accept-Ranges" ).size ());
193
190
assertResponseBody ("h1 { color:red; }" );
@@ -545,7 +542,6 @@ public void partialContentMultipleByteRanges() throws Exception {
545
542
546
543
TestSubscriber .subscribe (this .response .getBody ())
547
544
.assertValuesWith (buf -> {
548
-
549
545
String content = DataBufferTestUtils .dumpString (buf , StandardCharsets .UTF_8 );
550
546
String [] ranges = StringUtils .tokenizeToStringArray (content , "\r \n " , false , true );
551
547
@@ -563,20 +559,19 @@ public void partialContentMultipleByteRanges() throws Exception {
563
559
assertEquals ("Content-Type: text/plain" , ranges [9 ]);
564
560
assertEquals ("Content-Range: bytes 8-9/10" , ranges [10 ]);
565
561
assertEquals ("t." , ranges [11 ]);
566
-
567
562
});
568
563
}
569
564
570
- @ Test // SPR-14005
565
+ @ Test // SPR-14005
571
566
public void doOverwriteExistingCacheControlHeaders () throws Exception {
572
567
this .exchange .getAttributes ().put (PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE , "foo.css" );
573
568
this .response .getHeaders ().setCacheControl (CacheControl .noStore ().getHeaderValue ());
574
-
575
569
this .handler .handle (this .exchange ).blockMillis (5000 );
576
570
577
571
assertEquals ("max-age=3600" , this .response .getHeaders ().getCacheControl ());
578
572
}
579
573
574
+
580
575
private long resourceLastModified (String resourceName ) throws IOException {
581
576
return new ClassPathResource (resourceName , getClass ()).getFile ().lastModified ();
582
577
}
0 commit comments