diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java index ef1dfcf6cce3405106ab2249cc16be4f1f1e68f6..0880fad40736c620f9694bfcb6c87721d9afd5fe 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java @@ -49,15 +49,12 @@ import org.springframework.web.server.adapter.DefaultServerWebExchange; import org.springframework.web.server.session.DefaultWebSessionManager; import org.springframework.web.server.session.WebSessionManager; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.springframework.web.reactive.HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE; +import static org.junit.Assert.*; +import static org.springframework.web.reactive.HandlerMapping.*; /** * Unit tests for {@link ResourceWebHandler}. + * * @author Rossen Stoyanchev */ public class ResourceWebHandlerTests { @@ -75,7 +72,6 @@ public class ResourceWebHandlerTests { @Before public void setUp() throws Exception { - List paths = new ArrayList<>(2); paths.add(new ClassPathResource("test/", getClass())); paths.add(new ClassPathResource("testalternatepath/", getClass())); @@ -90,6 +86,7 @@ public class ResourceWebHandlerTests { this.exchange = new DefaultServerWebExchange(this.request, this.response, this.sessionManager); } + @Test public void getResource() throws Exception { this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); @@ -100,7 +97,7 @@ public class ResourceWebHandlerTests { assertEquals(17, headers.getContentLength()); assertEquals("max-age=3600", headers.getCacheControl()); assertTrue(headers.containsKey("Last-Modified")); - assertEquals(headers.getLastModified(), resourceLastModifiedDate("test/foo.css")); + assertEquals(headers.getLastModified() / 1000, resourceLastModifiedDate("test/foo.css") / 1000); assertEquals("bytes", headers.getFirst("Accept-Ranges")); assertEquals(1, headers.get("Accept-Ranges").size()); assertResponseBody("h1 { color:red; }"); @@ -118,7 +115,7 @@ public class ResourceWebHandlerTests { assertEquals(17, headers.getContentLength()); assertEquals("max-age=3600", headers.getCacheControl()); assertTrue(headers.containsKey("Last-Modified")); - assertEquals(headers.getLastModified(), resourceLastModifiedDate("test/foo.css")); + assertEquals(headers.getLastModified() / 1000, resourceLastModifiedDate("test/foo.css") / 1000); assertEquals("bytes", headers.getFirst("Accept-Ranges")); assertEquals(1, headers.get("Accept-Ranges").size()); assertNull(this.response.getBody()); @@ -142,7 +139,7 @@ public class ResourceWebHandlerTests { assertEquals("no-store", this.response.getHeaders().getCacheControl()); assertTrue(this.response.getHeaders().containsKey("Last-Modified")); - assertEquals(this.response.getHeaders().getLastModified(), resourceLastModifiedDate("test/foo.css")); + assertEquals(this.response.getHeaders().getLastModified() / 1000, resourceLastModifiedDate("test/foo.css") / 1000); assertEquals("bytes", this.response.getHeaders().getFirst("Accept-Ranges")); assertEquals(1, this.response.getHeaders().get("Accept-Ranges").size()); } @@ -172,7 +169,7 @@ public class ResourceWebHandlerTests { assertEquals(MediaType.TEXT_HTML, headers.getContentType()); assertEquals("max-age=3600", headers.getCacheControl()); assertTrue(headers.containsKey("Last-Modified")); - assertEquals(headers.getLastModified(), resourceLastModifiedDate("test/foo.html")); + assertEquals(headers.getLastModified() / 1000, resourceLastModifiedDate("test/foo.html") / 1000); assertEquals("bytes", headers.getFirst("Accept-Ranges")); assertEquals(1, headers.get("Accept-Ranges").size()); } @@ -187,7 +184,7 @@ public class ResourceWebHandlerTests { assertEquals(17, headers.getContentLength()); assertEquals("max-age=3600", headers.getCacheControl()); assertTrue(headers.containsKey("Last-Modified")); - assertEquals(headers.getLastModified(), resourceLastModifiedDate("testalternatepath/baz.css")); + assertEquals(headers.getLastModified() / 1000, resourceLastModifiedDate("testalternatepath/baz.css") / 1000); assertEquals("bytes", headers.getFirst("Accept-Ranges")); assertEquals(1, headers.get("Accept-Ranges").size()); assertResponseBody("h1 { color:red; }"); @@ -545,7 +542,6 @@ public class ResourceWebHandlerTests { TestSubscriber.subscribe(this.response.getBody()) .assertValuesWith(buf -> { - String content = DataBufferTestUtils.dumpString(buf, StandardCharsets.UTF_8); String[] ranges = StringUtils.tokenizeToStringArray(content, "\r\n", false, true); @@ -563,20 +559,19 @@ public class ResourceWebHandlerTests { assertEquals("Content-Type: text/plain", ranges[9]); assertEquals("Content-Range: bytes 8-9/10", ranges[10]); assertEquals("t.", ranges[11]); - }); } - @Test // SPR-14005 + @Test // SPR-14005 public void doOverwriteExistingCacheControlHeaders() throws Exception { this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); this.response.getHeaders().setCacheControl(CacheControl.noStore().getHeaderValue()); - this.handler.handle(this.exchange).blockMillis(5000); assertEquals("max-age=3600", this.response.getHeaders().getCacheControl()); } + private long resourceLastModified(String resourceName) throws IOException { return new ClassPathResource(resourceName, getClass()).getFile().lastModified(); }