Browse Source

Consistent getDateHeader checks in spring-webmvc unit tests

Issue: SPR-16160
pull/1587/head
Juergen Hoeller 7 years ago
parent
commit
a75dd2dd3e
  1. 4
      spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java
  2. 13
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java
  3. 51
      spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

4
spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java

@ -178,7 +178,7 @@ public class DispatcherServletTests { @@ -178,7 +178,7 @@ public class DispatcherServletTests {
MockHttpServletResponse response = new MockHttpServletResponse();
simpleDispatcherServlet.service(request, response);
assertTrue("Not forwarded", response.getForwardedUrl() == null);
assertEquals("Wed, 1 Apr 2015 00:00:00 GMT", response.getHeader("Last-Modified"));
assertEquals("Wed, 01 Apr 2015 00:00:00 GMT", response.getHeader("Last-Modified"));
}
@Test
@ -208,7 +208,7 @@ public class DispatcherServletTests { @@ -208,7 +208,7 @@ public class DispatcherServletTests {
assertTrue(request.getAttribute("test3") != null);
assertTrue(request.getAttribute("test3x") != null);
assertTrue(request.getAttribute("test3y") != null);
assertEquals("Wed, 1 Apr 2015 00:00:01 GMT", response.getHeader("Last-Modified"));
assertEquals("Wed, 01 Apr 2015 00:00:01 GMT", response.getHeader("Last-Modified"));
}
@Test

13
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.web.servlet.mvc.method.annotation;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.nio.charset.StandardCharsets;
@ -56,8 +57,7 @@ import static java.time.Instant.*; @@ -56,8 +57,7 @@ import static java.time.Instant.*;
import static java.time.format.DateTimeFormatter.*;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;
import static org.springframework.http.MediaType.TEXT_PLAIN;
import static org.springframework.http.MediaType.*;
import static org.springframework.web.servlet.HandlerMapping.*;
/**
@ -618,13 +618,13 @@ public class HttpEntityMethodProcessorMockTests { @@ -618,13 +618,13 @@ public class HttpEntityMethodProcessorMockTests {
given(stringHttpMessageConverter.canWrite(String.class, accepted)).willReturn(true);
}
private void assertResponseBody(String body) throws Exception {
private void assertResponseBody(String body) throws IOException {
ArgumentCaptor<HttpOutputMessage> outputMessage = ArgumentCaptor.forClass(HttpOutputMessage.class);
verify(stringHttpMessageConverter).write(eq(body), eq(TEXT_PLAIN), outputMessage.capture());
}
private void assertConditionalResponse(HttpStatus status, String body, String etag,
long lastModified) throws Exception {
private void assertConditionalResponse(HttpStatus status, String body, String etag, long lastModified)
throws IOException {
assertEquals(status.value(), servletResponse.getStatus());
assertTrue(mavContainer.isRequestHandled());
@ -640,8 +640,7 @@ public class HttpEntityMethodProcessorMockTests { @@ -640,8 +640,7 @@ public class HttpEntityMethodProcessorMockTests {
}
if (lastModified != -1) {
assertEquals(1, servletResponse.getHeaderValues(HttpHeaders.LAST_MODIFIED).size());
assertEquals(RFC_1123_DATE_TIME.format(ofEpochMilli(lastModified).atZone(GMT)),
servletResponse.getHeader(HttpHeaders.LAST_MODIFIED));
assertEquals(lastModified / 1000, servletResponse.getDateHeader(HttpHeaders.LAST_MODIFIED) / 1000);
}
}

51
spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

@ -17,9 +17,6 @@ @@ -17,9 +17,6 @@
package org.springframework.web.servlet.resource;
import java.io.IOException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -45,15 +42,10 @@ import org.springframework.web.accept.ContentNegotiationManager; @@ -45,15 +42,10 @@ import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
import org.springframework.web.servlet.HandlerMapping;
import static java.time.format.DateTimeFormatter.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
/**
* Unit tests for ResourceHttpRequestHandler.
* Unit tests for {@link ResourceHttpRequestHandler}.
*
* @author Keith Donald
* @author Jeremy Grelle
@ -96,7 +88,7 @@ public class ResourceHttpRequestHandlerTests { @@ -96,7 +88,7 @@ public class ResourceHttpRequestHandlerTests {
assertEquals(17, this.response.getContentLength());
assertEquals("max-age=3600", this.response.getHeader("Cache-Control"));
assertTrue(this.response.containsHeader("Last-Modified"));
assertEquals(this.response.getHeader("Last-Modified"), resourceLastModifiedDate("test/foo.css"));
assertEquals(resourceLastModified("test/foo.css") / 1000, this.response.getDateHeader("Last-Modified") / 1000);
assertEquals("bytes", this.response.getHeader("Accept-Ranges"));
assertEquals(1, this.response.getHeaders("Accept-Ranges").size());
assertEquals("h1 { color:red; }", this.response.getContentAsString());
@ -113,7 +105,7 @@ public class ResourceHttpRequestHandlerTests { @@ -113,7 +105,7 @@ public class ResourceHttpRequestHandlerTests {
assertEquals(17, this.response.getContentLength());
assertEquals("max-age=3600", this.response.getHeader("Cache-Control"));
assertTrue(this.response.containsHeader("Last-Modified"));
assertEquals(this.response.getHeader("Last-Modified"), resourceLastModifiedDate("test/foo.css"));
assertEquals(resourceLastModified("test/foo.css") / 1000, this.response.getDateHeader("Last-Modified") / 1000);
assertEquals("bytes", this.response.getHeader("Accept-Ranges"));
assertEquals(1, this.response.getHeaders("Accept-Ranges").size());
assertEquals(0, this.response.getContentAsByteArray().length);
@ -137,7 +129,7 @@ public class ResourceHttpRequestHandlerTests { @@ -137,7 +129,7 @@ public class ResourceHttpRequestHandlerTests {
assertEquals("no-store", this.response.getHeader("Cache-Control"));
assertTrue(this.response.containsHeader("Last-Modified"));
assertEquals(this.response.getHeader("Last-Modified"), resourceLastModifiedDate("test/foo.css"));
assertEquals(resourceLastModified("test/foo.css") / 1000, this.response.getDateHeader("Last-Modified") / 1000);
assertEquals("bytes", this.response.getHeader("Accept-Ranges"));
assertEquals(1, this.response.getHeaders("Accept-Ranges").size());
}
@ -168,9 +160,9 @@ public class ResourceHttpRequestHandlerTests { @@ -168,9 +160,9 @@ public class ResourceHttpRequestHandlerTests {
this.handler.handleRequest(this.request, this.response);
assertEquals("max-age=3600, must-revalidate", this.response.getHeader("Cache-Control"));
assertTrue(dateHeaderAsLong("Expires") >= System.currentTimeMillis() - 1000 + (3600 * 1000));
assertTrue(this.response.getDateHeader("Expires") >= System.currentTimeMillis() - 1000 + (3600 * 1000));
assertTrue(this.response.containsHeader("Last-Modified"));
assertEquals(this.response.getHeader("Last-Modified"), resourceLastModifiedDate("test/foo.css"));
assertEquals(resourceLastModified("test/foo.css") / 1000, this.response.getDateHeader("Last-Modified") / 1000);
assertEquals("bytes", this.response.getHeader("Accept-Ranges"));
assertEquals(1, this.response.getHeaders("Accept-Ranges").size());
}
@ -188,9 +180,9 @@ public class ResourceHttpRequestHandlerTests { @@ -188,9 +180,9 @@ public class ResourceHttpRequestHandlerTests {
assertEquals("no-cache", this.response.getHeader("Pragma"));
assertThat(this.response.getHeaderValues("Cache-Control"), Matchers.iterableWithSize(1));
assertEquals("no-cache", this.response.getHeader("Cache-Control"));
assertTrue(dateHeaderAsLong("Expires") <= System.currentTimeMillis());
assertTrue(this.response.getDateHeader("Expires") <= System.currentTimeMillis());
assertTrue(this.response.containsHeader("Last-Modified"));
assertEquals(dateHeaderAsLong("Last-Modified") / 1000, resourceLastModified("test/foo.css") / 1000);
assertEquals(resourceLastModified("test/foo.css") / 1000, this.response.getDateHeader("Last-Modified") / 1000);
assertEquals("bytes", this.response.getHeader("Accept-Ranges"));
assertEquals(1, this.response.getHeaders("Accept-Ranges").size());
}
@ -203,7 +195,7 @@ public class ResourceHttpRequestHandlerTests { @@ -203,7 +195,7 @@ public class ResourceHttpRequestHandlerTests {
assertEquals("text/html", this.response.getContentType());
assertEquals("max-age=3600", this.response.getHeader("Cache-Control"));
assertTrue(this.response.containsHeader("Last-Modified"));
assertEquals(this.response.getHeader("Last-Modified"), resourceLastModifiedDate("test/foo.html"));
assertEquals(resourceLastModified("test/foo.html") / 1000, this.response.getDateHeader("Last-Modified") / 1000);
assertEquals("bytes", this.response.getHeader("Accept-Ranges"));
assertEquals(1, this.response.getHeaders("Accept-Ranges").size());
}
@ -217,7 +209,8 @@ public class ResourceHttpRequestHandlerTests { @@ -217,7 +209,8 @@ public class ResourceHttpRequestHandlerTests {
assertEquals(17, this.response.getContentLength());
assertEquals("max-age=3600", this.response.getHeader("Cache-Control"));
assertTrue(this.response.containsHeader("Last-Modified"));
assertEquals(this.response.getHeader("Last-Modified"), resourceLastModifiedDate("testalternatepath/baz.css"));
assertEquals(resourceLastModified("testalternatepath/baz.css") / 1000,
this.response.getDateHeader("Last-Modified") / 1000);
assertEquals("bytes", this.response.getHeader("Accept-Ranges"));
assertEquals(1, this.response.getHeaders("Accept-Ranges").size());
assertEquals("h1 { color:red; }", this.response.getContentAsString());
@ -241,7 +234,7 @@ public class ResourceHttpRequestHandlerTests { @@ -241,7 +234,7 @@ public class ResourceHttpRequestHandlerTests {
assertEquals("function foo() { console.log(\"hello world\"); }", this.response.getContentAsString());
}
@Test // SPR-13658
@Test // SPR-13658
public void getResourceWithRegisteredMediaType() throws Exception {
ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
factory.addMediaType("bar", new MediaType("foo", "bar"));
@ -262,7 +255,7 @@ public class ResourceHttpRequestHandlerTests { @@ -262,7 +255,7 @@ public class ResourceHttpRequestHandlerTests {
assertEquals("h1 { color:red; }", this.response.getContentAsString());
}
@Test // SPR-14577
@Test // SPR-14577
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
factory.setFavorPathExtension(false);
@ -283,18 +276,16 @@ public class ResourceHttpRequestHandlerTests { @@ -283,18 +276,16 @@ public class ResourceHttpRequestHandlerTests {
assertEquals("text/html", this.response.getContentType());
}
@Test // SPR-14368
@Test // SPR-14368
public void getResourceWithMediaTypeResolvedThroughServletContext() throws Exception {
MockServletContext servletContext = new MockServletContext() {
@Override
public String getMimeType(String filePath) {
return "foo/bar";
}
@Override
public String getVirtualServerName() {
return null;
return "";
}
};
@ -619,20 +610,10 @@ public class ResourceHttpRequestHandlerTests { @@ -619,20 +610,10 @@ public class ResourceHttpRequestHandlerTests {
}
private long dateHeaderAsLong(String responseHeaderName) throws Exception {
String header = this.response.getHeader(responseHeaderName);
return ZonedDateTime.parse(header, RFC_1123_DATE_TIME).toInstant().toEpochMilli();
}
private long resourceLastModified(String resourceName) throws IOException {
return new ClassPathResource(resourceName, getClass()).getFile().lastModified();
}
private String resourceLastModifiedDate(String resourceName) throws IOException {
long lastModified = new ClassPathResource(resourceName, getClass()).getFile().lastModified();
return RFC_1123_DATE_TIME.format(Instant.ofEpochMilli(lastModified).atZone(ZoneId.of("GMT")));
}
private static class TestServletContext extends MockServletContext {

Loading…
Cancel
Save