提交 915350d0 编写于 作者: J Juergen Hoeller

Reliable last-modified timestamp tests on Windows

上级 118d093e
...@@ -49,15 +49,12 @@ import org.springframework.web.server.adapter.DefaultServerWebExchange; ...@@ -49,15 +49,12 @@ import org.springframework.web.server.adapter.DefaultServerWebExchange;
import org.springframework.web.server.session.DefaultWebSessionManager; import org.springframework.web.server.session.DefaultWebSessionManager;
import org.springframework.web.server.session.WebSessionManager; import org.springframework.web.server.session.WebSessionManager;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertNull; import static org.springframework.web.reactive.HandlerMapping.*;
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;
/** /**
* Unit tests for {@link ResourceWebHandler}. * Unit tests for {@link ResourceWebHandler}.
*
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
*/ */
public class ResourceWebHandlerTests { public class ResourceWebHandlerTests {
...@@ -75,7 +72,6 @@ public class ResourceWebHandlerTests { ...@@ -75,7 +72,6 @@ public class ResourceWebHandlerTests {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
List<Resource> paths = new ArrayList<>(2); List<Resource> paths = new ArrayList<>(2);
paths.add(new ClassPathResource("test/", getClass())); paths.add(new ClassPathResource("test/", getClass()));
paths.add(new ClassPathResource("testalternatepath/", getClass())); paths.add(new ClassPathResource("testalternatepath/", getClass()));
...@@ -90,6 +86,7 @@ public class ResourceWebHandlerTests { ...@@ -90,6 +86,7 @@ public class ResourceWebHandlerTests {
this.exchange = new DefaultServerWebExchange(this.request, this.response, this.sessionManager); this.exchange = new DefaultServerWebExchange(this.request, this.response, this.sessionManager);
} }
@Test @Test
public void getResource() throws Exception { public void getResource() throws Exception {
this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
...@@ -100,7 +97,7 @@ public class ResourceWebHandlerTests { ...@@ -100,7 +97,7 @@ public class ResourceWebHandlerTests {
assertEquals(17, headers.getContentLength()); assertEquals(17, headers.getContentLength());
assertEquals("max-age=3600", headers.getCacheControl()); assertEquals("max-age=3600", headers.getCacheControl());
assertTrue(headers.containsKey("Last-Modified")); 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("bytes", headers.getFirst("Accept-Ranges"));
assertEquals(1, headers.get("Accept-Ranges").size()); assertEquals(1, headers.get("Accept-Ranges").size());
assertResponseBody("h1 { color:red; }"); assertResponseBody("h1 { color:red; }");
...@@ -118,7 +115,7 @@ public class ResourceWebHandlerTests { ...@@ -118,7 +115,7 @@ public class ResourceWebHandlerTests {
assertEquals(17, headers.getContentLength()); assertEquals(17, headers.getContentLength());
assertEquals("max-age=3600", headers.getCacheControl()); assertEquals("max-age=3600", headers.getCacheControl());
assertTrue(headers.containsKey("Last-Modified")); 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("bytes", headers.getFirst("Accept-Ranges"));
assertEquals(1, headers.get("Accept-Ranges").size()); assertEquals(1, headers.get("Accept-Ranges").size());
assertNull(this.response.getBody()); assertNull(this.response.getBody());
...@@ -142,7 +139,7 @@ public class ResourceWebHandlerTests { ...@@ -142,7 +139,7 @@ public class ResourceWebHandlerTests {
assertEquals("no-store", this.response.getHeaders().getCacheControl()); assertEquals("no-store", this.response.getHeaders().getCacheControl());
assertTrue(this.response.getHeaders().containsKey("Last-Modified")); 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("bytes", this.response.getHeaders().getFirst("Accept-Ranges"));
assertEquals(1, this.response.getHeaders().get("Accept-Ranges").size()); assertEquals(1, this.response.getHeaders().get("Accept-Ranges").size());
} }
...@@ -172,7 +169,7 @@ public class ResourceWebHandlerTests { ...@@ -172,7 +169,7 @@ public class ResourceWebHandlerTests {
assertEquals(MediaType.TEXT_HTML, headers.getContentType()); assertEquals(MediaType.TEXT_HTML, headers.getContentType());
assertEquals("max-age=3600", headers.getCacheControl()); assertEquals("max-age=3600", headers.getCacheControl());
assertTrue(headers.containsKey("Last-Modified")); 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("bytes", headers.getFirst("Accept-Ranges"));
assertEquals(1, headers.get("Accept-Ranges").size()); assertEquals(1, headers.get("Accept-Ranges").size());
} }
...@@ -187,7 +184,7 @@ public class ResourceWebHandlerTests { ...@@ -187,7 +184,7 @@ public class ResourceWebHandlerTests {
assertEquals(17, headers.getContentLength()); assertEquals(17, headers.getContentLength());
assertEquals("max-age=3600", headers.getCacheControl()); assertEquals("max-age=3600", headers.getCacheControl());
assertTrue(headers.containsKey("Last-Modified")); 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("bytes", headers.getFirst("Accept-Ranges"));
assertEquals(1, headers.get("Accept-Ranges").size()); assertEquals(1, headers.get("Accept-Ranges").size());
assertResponseBody("h1 { color:red; }"); assertResponseBody("h1 { color:red; }");
...@@ -545,7 +542,6 @@ public class ResourceWebHandlerTests { ...@@ -545,7 +542,6 @@ public class ResourceWebHandlerTests {
TestSubscriber.subscribe(this.response.getBody()) TestSubscriber.subscribe(this.response.getBody())
.assertValuesWith(buf -> { .assertValuesWith(buf -> {
String content = DataBufferTestUtils.dumpString(buf, StandardCharsets.UTF_8); String content = DataBufferTestUtils.dumpString(buf, StandardCharsets.UTF_8);
String[] ranges = StringUtils.tokenizeToStringArray(content, "\r\n", false, true); String[] ranges = StringUtils.tokenizeToStringArray(content, "\r\n", false, true);
...@@ -563,20 +559,19 @@ public class ResourceWebHandlerTests { ...@@ -563,20 +559,19 @@ public class ResourceWebHandlerTests {
assertEquals("Content-Type: text/plain", ranges[9]); assertEquals("Content-Type: text/plain", ranges[9]);
assertEquals("Content-Range: bytes 8-9/10", ranges[10]); assertEquals("Content-Range: bytes 8-9/10", ranges[10]);
assertEquals("t.", ranges[11]); assertEquals("t.", ranges[11]);
}); });
} }
@Test // SPR-14005 @Test // SPR-14005
public void doOverwriteExistingCacheControlHeaders() throws Exception { public void doOverwriteExistingCacheControlHeaders() throws Exception {
this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
this.response.getHeaders().setCacheControl(CacheControl.noStore().getHeaderValue()); this.response.getHeaders().setCacheControl(CacheControl.noStore().getHeaderValue());
this.handler.handle(this.exchange).blockMillis(5000); this.handler.handle(this.exchange).blockMillis(5000);
assertEquals("max-age=3600", this.response.getHeaders().getCacheControl()); assertEquals("max-age=3600", this.response.getHeaders().getCacheControl());
} }
private long resourceLastModified(String resourceName) throws IOException { private long resourceLastModified(String resourceName) throws IOException {
return new ClassPathResource(resourceName, getClass()).getFile().lastModified(); return new ClassPathResource(resourceName, getClass()).getFile().lastModified();
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册