MockHttpServletResponse.setIntHeader supports 'Content-Length' header as well
Issue: SPR-13752
This commit is contained in:
parent
4d4d2e2966
commit
a4f5c46fed
|
@ -154,7 +154,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
* <p>If {@code false}, {@link #getCharacterEncoding()} will return a default encoding value.
|
||||
*/
|
||||
public boolean isCharset() {
|
||||
return charset;
|
||||
return this.charset;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -552,11 +552,12 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
|
||||
private boolean setSpecialHeader(String name, Object value) {
|
||||
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) {
|
||||
setContentType((String) value);
|
||||
setContentType(value.toString());
|
||||
return true;
|
||||
}
|
||||
else if (CONTENT_LENGTH_HEADER.equalsIgnoreCase(name)) {
|
||||
setContentLength(Integer.parseInt((String) value));
|
||||
setContentLength(value instanceof Number ? ((Number) value).intValue() :
|
||||
Integer.parseInt(value.toString()));
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -19,7 +19,6 @@ 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;
|
||||
|
@ -92,9 +91,7 @@ public class MockHttpServletResponseTests {
|
|||
assertEquals("UTF-8", response.getCharacterEncoding());
|
||||
}
|
||||
|
||||
// SPR-12677
|
||||
|
||||
@Test
|
||||
@Test // SPR-12677
|
||||
public void contentTypeHeaderWithMoreComplexCharsetSyntax() {
|
||||
String contentType = "test/plain;charset=\"utf-8\";foo=\"charset=bar\";foocharset=bar;foo=bar";
|
||||
response.setHeader("Content-Type", contentType);
|
||||
|
@ -141,6 +138,13 @@ public class MockHttpServletResponseTests {
|
|||
assertEquals("66", response.getHeader("Content-Length"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contentLengthIntHeader() {
|
||||
response.addIntHeader("Content-Length", 66);
|
||||
assertEquals(66, response.getContentLength());
|
||||
assertEquals("66", response.getHeader("Content-Length"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpHeaderNameCasingIsPreserved() throws Exception {
|
||||
final String headerName = "Header1";
|
||||
|
@ -269,20 +273,14 @@ public class MockHttpServletResponseTests {
|
|||
response.getDateHeader("Last-Modified");
|
||||
}
|
||||
|
||||
/**
|
||||
* SPR-10414
|
||||
*/
|
||||
@Test
|
||||
@Test // SPR-10414
|
||||
public void modifyStatusAfterSendError() throws IOException {
|
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
assertEquals(response.getStatus(),HttpServletResponse.SC_NOT_FOUND);
|
||||
}
|
||||
|
||||
/**
|
||||
* SPR-10414
|
||||
*/
|
||||
@Test
|
||||
@Test // SPR-10414
|
||||
@SuppressWarnings("deprecation")
|
||||
public void modifyStatusMessageAfterSendError() throws IOException {
|
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||
|
|
|
@ -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;
|
||||
|
@ -32,7 +33,6 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
@ -490,16 +490,30 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
|
||||
@Override
|
||||
public void setDateHeader(String name, long value) {
|
||||
setHeaderValue(name, formatDate(value));
|
||||
}
|
||||
|
||||
public long getDateHeader(String name) {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
|
||||
dateFormat.setTimeZone(GMT);
|
||||
setHeaderValue(name, dateFormat.format(new Date(value)));
|
||||
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));
|
||||
}
|
||||
|
||||
private String formatDate(long date) {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
|
||||
dateFormat.setTimeZone(GMT);
|
||||
setHeaderValue(name, dateFormat.format(new Date(value)));
|
||||
return dateFormat.format(new Date(date));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -538,11 +552,12 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
|
||||
private boolean setSpecialHeader(String name, Object value) {
|
||||
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) {
|
||||
setContentType((String) value);
|
||||
setContentType(value.toString());
|
||||
return true;
|
||||
}
|
||||
else if (CONTENT_LENGTH_HEADER.equalsIgnoreCase(name)) {
|
||||
setContentLength(Integer.parseInt((String) value));
|
||||
setContentLength(value instanceof Number ? ((Number) value).intValue() :
|
||||
Integer.parseInt(value.toString()));
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
|
|
Loading…
Reference in New Issue