MockHttpServletResponse.getDateHeader returns -1 for non-existent header
Includes consistent getDateHeader results in both MockHttpServletResponse variants (spring-test and spring-web) Issue: SPR-16160
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.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
@@ -517,27 +518,33 @@ 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 ex) {
|
||||
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));
|
||||
}
|
||||
|
||||
public long getDateHeader(String name) {
|
||||
String headerValue = getHeader(name);
|
||||
if (headerValue == null) {
|
||||
return -1;
|
||||
}
|
||||
try {
|
||||
return newDateFormat().parse(getHeader(name)).getTime();
|
||||
}
|
||||
catch (ParseException ex) {
|
||||
throw new IllegalArgumentException(
|
||||
"Value for header '" + name + "' is not a valid Date: " + headerValue);
|
||||
}
|
||||
}
|
||||
|
||||
private String formatDate(long date) {
|
||||
return newDateFormat().format(new Date(date));
|
||||
}
|
||||
|
||||
private DateFormat newDateFormat() {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
|
||||
dateFormat.setTimeZone(GMT);
|
||||
return dateFormat.format(new Date(date));
|
||||
return dateFormat;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user