提交 79ded1ca 编写于 作者: J Juergen Hoeller

MockHttpServletResponse.setIntHeader supports 'Content-Length' header as well

Issue: SPR-13752
(cherry picked from commit a4f5c46f)
上级 fad0c958
/*
* 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.
......@@ -506,11 +506,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 {
......
/*
* 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.
......@@ -122,6 +122,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";
......@@ -221,20 +228,14 @@ public class MockHttpServletResponseTests {
assertEquals(redirectUrl, response.getRedirectedUrl());
}
/**
* 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);
......
/*
* Copyright 2002-2013 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.
......@@ -71,7 +71,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
private boolean charset = false;
private final ByteArrayOutputStream content = new ByteArrayOutputStream();
private final ByteArrayOutputStream content = new ByteArrayOutputStream(1024);
private final ServletOutputStream outputStream = new ResponseServletOutputStream(this.content);
......@@ -189,8 +189,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
public String getContentAsString() throws UnsupportedEncodingException {
flushBuffer();
return (this.characterEncoding != null) ?
this.content.toString(this.characterEncoding) : this.content.toString();
return (this.characterEncoding != null ?
this.content.toString(this.characterEncoding) : this.content.toString());
}
@Override
......@@ -218,8 +218,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
if (contentType != null) {
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
if (charsetIndex != -1) {
String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
this.characterEncoding = encoding;
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
this.charset = true;
}
updateContentTypeHeader();
......@@ -416,11 +415,13 @@ public class MockHttpServletResponse implements HttpServletResponse {
}
@Override
@Deprecated
public String encodeUrl(String url) {
return encodeURL(url);
}
@Override
@Deprecated
public String encodeRedirectUrl(String url) {
return encodeRedirectURL(url);
}
......@@ -505,11 +506,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 {
......@@ -534,14 +536,15 @@ public class MockHttpServletResponse implements HttpServletResponse {
@Override
public void setStatus(int status) {
if(!this.isCommitted()) {
if (!this.isCommitted()) {
this.status = status;
}
}
@Override
@Deprecated
public void setStatus(int status, String errorMessage) {
if(!this.isCommitted()) {
if (!this.isCommitted()) {
this.status = status;
this.errorMessage = errorMessage;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册