Improve DateHeaders in MockServletRequest/Response

Prior to this change, calling the `setDateHeader` method on a
Spring Test MockHttpServletResponse instance would just store the given
long value in a Map, not writing it as a formatted date String.
Also, calling `getDateHeader` on a MockHttpServletRequest would not
support date strings and could not parse those values.

This can be problematic when testing features related to date headers
such as "Expires", "If-Modified-Since", "Last-Modified", etc.

This commit adds formatting and parsing capabilities to Servlet Mocks
for date strings in HTTP headers.

When formatting dates to Strings, the date format used is the one
preferred by the HTTP RFC. When parsing date Strings, multiple date
formats are supported for better compatibility.

Issue: SPR-11912
This commit is contained in:
Brian Clozel
2015-07-22 13:39:19 +02:00
parent 3c799e6e05
commit 43e36e2dee
5 changed files with 198 additions and 42 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
@@ -40,11 +41,17 @@ import static org.junit.Assert.*;
* @author Mark Fisher
* @author Rossen Stoyanchev
* @author Sam Brannen
* @author Brian Clozel
* @author Jakub Narloch
*/
public class MockHttpServletRequestTests {
private static final String HOST = "Host";
private static final String CONTENT_TYPE = "Content-Type";
private static final String IF_MODIFIED_SINCE = "If-Modified-Since";
private MockHttpServletRequest request = new MockHttpServletRequest();
@@ -69,7 +76,7 @@ public class MockHttpServletRequestTests {
String contentType = "test/plain";
request.setContentType(contentType);
assertEquals(contentType, request.getContentType());
assertEquals(contentType, request.getHeader("Content-Type"));
assertEquals(contentType, request.getHeader(CONTENT_TYPE));
assertNull(request.getCharacterEncoding());
}
@@ -78,7 +85,7 @@ public class MockHttpServletRequestTests {
String contentType = "test/plain;charset=UTF-8";
request.setContentType(contentType);
assertEquals(contentType, request.getContentType());
assertEquals(contentType, request.getHeader("Content-Type"));
assertEquals(contentType, request.getHeader(CONTENT_TYPE));
assertEquals("UTF-8", request.getCharacterEncoding());
}
@@ -87,7 +94,7 @@ public class MockHttpServletRequestTests {
String contentType = "test/plain";
request.addHeader("Content-Type", contentType);
assertEquals(contentType, request.getContentType());
assertEquals(contentType, request.getHeader("Content-Type"));
assertEquals(contentType, request.getHeader(CONTENT_TYPE));
assertNull(request.getCharacterEncoding());
}
@@ -96,7 +103,7 @@ public class MockHttpServletRequestTests {
String contentType = "test/plain;charset=UTF-8";
request.addHeader("Content-Type", contentType);
assertEquals(contentType, request.getContentType());
assertEquals(contentType, request.getHeader("Content-Type"));
assertEquals(contentType, request.getHeader(CONTENT_TYPE));
assertEquals("UTF-8", request.getCharacterEncoding());
}
@@ -107,7 +114,7 @@ public class MockHttpServletRequestTests {
String contentType = "test/plain;charset=\"utf-8\";foo=\"charset=bar\";foocharset=bar;foo=bar";
request.addHeader("Content-Type", contentType);
assertEquals(contentType, request.getContentType());
assertEquals(contentType, request.getHeader("Content-Type"));
assertEquals(contentType, request.getHeader(CONTENT_TYPE));
assertEquals("UTF-8", request.getCharacterEncoding());
}
@@ -116,7 +123,7 @@ public class MockHttpServletRequestTests {
request.setContentType("test/plain");
request.setCharacterEncoding("UTF-8");
assertEquals("test/plain", request.getContentType());
assertEquals("test/plain;charset=UTF-8", request.getHeader("Content-Type"));
assertEquals("test/plain;charset=UTF-8", request.getHeader(CONTENT_TYPE));
assertEquals("UTF-8", request.getCharacterEncoding());
}
@@ -125,7 +132,7 @@ public class MockHttpServletRequestTests {
request.setCharacterEncoding("UTF-8");
request.setContentType("test/plain");
assertEquals("test/plain", request.getContentType());
assertEquals("test/plain;charset=UTF-8", request.getHeader("Content-Type"));
assertEquals("test/plain;charset=UTF-8", request.getHeader(CONTENT_TYPE));
assertEquals("UTF-8", request.getCharacterEncoding());
}
@@ -378,6 +385,44 @@ public class MockHttpServletRequestTests {
assertTrue(request.isSecure());
}
@Test
public void httpHeaderDate() throws Exception {
Date date = new Date();
request.addHeader(IF_MODIFIED_SINCE, date);
assertEquals(date.getTime(), request.getDateHeader(IF_MODIFIED_SINCE));
}
@Test
public void httpHeaderTimestamp() throws Exception {
long timestamp = new Date().getTime();
request.addHeader(IF_MODIFIED_SINCE, timestamp);
assertEquals(timestamp, request.getDateHeader(IF_MODIFIED_SINCE));
}
@Test
public void httpHeaderRfcFormatedDate() throws Exception {
request.addHeader(IF_MODIFIED_SINCE, "Tue, 21 Jul 2015 10:00:00 GMT");
assertEquals(1437472800000L, request.getDateHeader(IF_MODIFIED_SINCE));
}
@Test
public void httpHeaderFirstVariantFormatedDate() throws Exception {
request.addHeader(IF_MODIFIED_SINCE, "Tue, 21-Jul-15 10:00:00 GMT");
assertEquals(1437472800000L, request.getDateHeader(IF_MODIFIED_SINCE));
}
@Test
public void httpHeaderSecondVariantFormatedDate() throws Exception {
request.addHeader(IF_MODIFIED_SINCE, "Tue Jul 21 10:00:00 2015");
assertEquals(1437472800000L, request.getDateHeader(IF_MODIFIED_SINCE));
}
@Test(expected = IllegalArgumentException.class)
public void httpHeaderFormatedDateError() throws Exception {
request.addHeader(IF_MODIFIED_SINCE, "This is not a date");
request.getDateHeader(IF_MODIFIED_SINCE);
}
private void assertEqualEnumerations(Enumeration<?> enum1, Enumeration<?> enum2) {
assertNotNull(enum1);
assertNotNull(enum2);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package org.springframework.mock.web;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import javax.servlet.http.HttpServletResponse;
import org.junit.Test;
@@ -238,6 +239,20 @@ public class MockHttpServletResponseTests {
assertEquals(redirectUrl, response.getRedirectedUrl());
}
@Test
public void setDateHeader() {
response.setDateHeader("Last-Modified", 1437472800000L);
assertEquals("Tue, 21 Jul 2015 10:00:00 GMT", response.getHeader("Last-Modified"));
}
@Test
public void addDateHeader() {
response.addDateHeader("Last-Modified", 1437472800000L);
response.addDateHeader("Last-Modified", 1437472801000L);
assertEquals("Tue, 21 Jul 2015 10:00:00 GMT", response.getHeaders("Last-Modified").get(0));
assertEquals("Tue, 21 Jul 2015 10:00:01 GMT", response.getHeaders("Last-Modified").get(1));
}
/**
* SPR-10414
*/