Add MockHttpServletResponse.getDateHeader
This change adds a new `getDateHeader` method that converts date header Strings to long values - making tests more readable. This feature is also documented in the "what's new section" for 4.2.
This commit is contained in:
@@ -23,6 +23,7 @@ import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.io.Writer;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -493,6 +494,17 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
setHeaderValue(name, formatDate(value));
|
||||
}
|
||||
|
||||
public long getDateHeader(String name) {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
|
||||
dateFormat.setTimeZone(GMT);
|
||||
try {
|
||||
return dateFormat.parse(getHeader(name)).getTime();
|
||||
} catch (ParseException exception) {
|
||||
throw new IllegalArgumentException(
|
||||
"Value for header '" + name + "' is not a valid Date: " + getHeader(name));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addDateHeader(String name, long value) {
|
||||
addHeaderValue(name, formatDate(value));
|
||||
|
||||
Reference in New Issue
Block a user