Browse Source

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

Issue: SPR-13752
(cherry picked from commit a4f5c46)
pull/931/head
Juergen Hoeller 10 years ago
parent
commit
79ded1ca72
  1. 7
      spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java
  2. 19
      spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java
  3. 23
      spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java

7
spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { private boolean setSpecialHeader(String name, Object value) {
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) { if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) {
setContentType((String) value); setContentType(value.toString());
return true; return true;
} }
else if (CONTENT_LENGTH_HEADER.equalsIgnoreCase(name)) { 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; return true;
} }
else { else {

19
spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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")); 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 @Test
public void httpHeaderNameCasingIsPreserved() throws Exception { public void httpHeaderNameCasingIsPreserved() throws Exception {
final String headerName = "Header1"; final String headerName = "Header1";
@ -221,20 +228,14 @@ public class MockHttpServletResponseTests {
assertEquals(redirectUrl, response.getRedirectedUrl()); assertEquals(redirectUrl, response.getRedirectedUrl());
} }
/** @Test // SPR-10414
* SPR-10414
*/
@Test
public void modifyStatusAfterSendError() throws IOException { public void modifyStatusAfterSendError() throws IOException {
response.sendError(HttpServletResponse.SC_NOT_FOUND); response.sendError(HttpServletResponse.SC_NOT_FOUND);
response.setStatus(HttpServletResponse.SC_OK); response.setStatus(HttpServletResponse.SC_OK);
assertEquals(response.getStatus(),HttpServletResponse.SC_NOT_FOUND); assertEquals(response.getStatus(),HttpServletResponse.SC_NOT_FOUND);
} }
/** @Test // SPR-10414
* SPR-10414
*/
@Test
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void modifyStatusMessageAfterSendError() throws IOException { public void modifyStatusMessageAfterSendError() throws IOException {
response.sendError(HttpServletResponse.SC_NOT_FOUND); response.sendError(HttpServletResponse.SC_NOT_FOUND);

23
spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 boolean charset = false;
private final ByteArrayOutputStream content = new ByteArrayOutputStream(); private final ByteArrayOutputStream content = new ByteArrayOutputStream(1024);
private final ServletOutputStream outputStream = new ResponseServletOutputStream(this.content); private final ServletOutputStream outputStream = new ResponseServletOutputStream(this.content);
@ -189,8 +189,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
public String getContentAsString() throws UnsupportedEncodingException { public String getContentAsString() throws UnsupportedEncodingException {
flushBuffer(); flushBuffer();
return (this.characterEncoding != null) ? return (this.characterEncoding != null ?
this.content.toString(this.characterEncoding) : this.content.toString(); this.content.toString(this.characterEncoding) : this.content.toString());
} }
@Override @Override
@ -218,8 +218,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
if (contentType != null) { if (contentType != null) {
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
if (charsetIndex != -1) { if (charsetIndex != -1) {
String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
this.characterEncoding = encoding;
this.charset = true; this.charset = true;
} }
updateContentTypeHeader(); updateContentTypeHeader();
@ -416,11 +415,13 @@ public class MockHttpServletResponse implements HttpServletResponse {
} }
@Override @Override
@Deprecated
public String encodeUrl(String url) { public String encodeUrl(String url) {
return encodeURL(url); return encodeURL(url);
} }
@Override @Override
@Deprecated
public String encodeRedirectUrl(String url) { public String encodeRedirectUrl(String url) {
return encodeRedirectURL(url); return encodeRedirectURL(url);
} }
@ -505,11 +506,12 @@ public class MockHttpServletResponse implements HttpServletResponse {
private boolean setSpecialHeader(String name, Object value) { private boolean setSpecialHeader(String name, Object value) {
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) { if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) {
setContentType((String) value); setContentType(value.toString());
return true; return true;
} }
else if (CONTENT_LENGTH_HEADER.equalsIgnoreCase(name)) { 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; return true;
} }
else { else {
@ -534,14 +536,15 @@ public class MockHttpServletResponse implements HttpServletResponse {
@Override @Override
public void setStatus(int status) { public void setStatus(int status) {
if(!this.isCommitted()) { if (!this.isCommitted()) {
this.status = status; this.status = status;
} }
} }
@Override @Override
@Deprecated
public void setStatus(int status, String errorMessage) { public void setStatus(int status, String errorMessage) {
if(!this.isCommitted()) { if (!this.isCommitted()) {
this.status = status; this.status = status;
this.errorMessage = errorMessage; this.errorMessage = errorMessage;
} }

Loading…
Cancel
Save