提交 59d80ec1 编写于 作者: R Rob Winch 提交者: Sam Brannen

Fix minor issue in MockHttpServletRequest

Previously MockHttpServletRequest#sendRedirect did not set the HTTP status
or the Location header. This does not conform to the HttpServletRequest
interface.

MockHttpServletRequest will now:

  - Set the HTTP status to 302 on sendRedirect
  - Set the Location header on sendRedirect
  - Ensure the Location header and getRedirectedUrl are kept in synch

Issue: SPR-9594
上级 67a05e41
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 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.
...@@ -54,6 +54,8 @@ public class MockHttpServletResponse implements HttpServletResponse { ...@@ -54,6 +54,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
private static final String CONTENT_LENGTH_HEADER = "Content-Length"; private static final String CONTENT_LENGTH_HEADER = "Content-Length";
private static final String LOCATION_HEADER = "Location";
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// ServletResponse properties // ServletResponse properties
//--------------------------------------------------------------------- //---------------------------------------------------------------------
...@@ -95,8 +97,6 @@ public class MockHttpServletResponse implements HttpServletResponse { ...@@ -95,8 +97,6 @@ public class MockHttpServletResponse implements HttpServletResponse {
private String errorMessage; private String errorMessage;
private String redirectedUrl;
private String forwardedUrl; private String forwardedUrl;
private final List<String> includedUrls = new ArrayList<String>(); private final List<String> includedUrls = new ArrayList<String>();
...@@ -306,7 +306,7 @@ public class MockHttpServletResponse implements HttpServletResponse { ...@@ -306,7 +306,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
/** /**
* Return the primary value for the given header as a String, if any. * Return the primary value for the given header as a String, if any.
* Will return the first value in case of multiple values. * Will return the first value in case of multiple values.
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse. * <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
* As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility. * As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
* Consider using {@link #getHeaderValue(String)} for raw Object access. * Consider using {@link #getHeaderValue(String)} for raw Object access.
* @param name the name of the header * @param name the name of the header
...@@ -319,7 +319,7 @@ public class MockHttpServletResponse implements HttpServletResponse { ...@@ -319,7 +319,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
/** /**
* Return all values for the given header as a List of Strings. * Return all values for the given header as a List of Strings.
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse. * <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
* As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility. * As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
* Consider using {@link #getHeaderValues(String)} for raw Object access. * Consider using {@link #getHeaderValues(String)} for raw Object access.
* @param name the name of the header * @param name the name of the header
...@@ -374,7 +374,7 @@ public class MockHttpServletResponse implements HttpServletResponse { ...@@ -374,7 +374,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
* returning the given URL String as-is. * returning the given URL String as-is.
* <p>Can be overridden in subclasses, appending a session id or the like * <p>Can be overridden in subclasses, appending a session id or the like
* in a redirect-specific fashion. For general URL encoding rules, * in a redirect-specific fashion. For general URL encoding rules,
* override the common {@link #encodeURL} method instead, appyling * override the common {@link #encodeURL} method instead, applying
* to redirect URLs as well as to general URLs. * to redirect URLs as well as to general URLs.
*/ */
public String encodeRedirectURL(String url) { public String encodeRedirectURL(String url) {
...@@ -411,12 +411,13 @@ public class MockHttpServletResponse implements HttpServletResponse { ...@@ -411,12 +411,13 @@ public class MockHttpServletResponse implements HttpServletResponse {
throw new IllegalStateException("Cannot send redirect - response is already committed"); throw new IllegalStateException("Cannot send redirect - response is already committed");
} }
Assert.notNull(url, "Redirect URL must not be null"); Assert.notNull(url, "Redirect URL must not be null");
this.redirectedUrl = url; setHeader(LOCATION_HEADER, url);
setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
setCommitted(true); setCommitted(true);
} }
public String getRedirectedUrl() { public String getRedirectedUrl() {
return this.redirectedUrl; return getHeader(LOCATION_HEADER);
} }
public void setDateHeader(String name, long value) { public void setDateHeader(String name, long value) {
......
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 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.
...@@ -16,43 +16,54 @@ ...@@ -16,43 +16,54 @@
package org.springframework.mock.web; package org.springframework.mock.web;
import static org.junit.Assert.*;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Set; import java.util.Set;
import junit.framework.TestCase; import javax.servlet.http.HttpServletResponse;
import org.junit.Test;
import org.springframework.web.util.WebUtils; import org.springframework.web.util.WebUtils;
/** /**
* Unit tests for {@link MockHttpServletResponse}.
*
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rick Evans * @author Rick Evans
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Rob Winch
* @author Sam Brannen
* @since 19.02.2006 * @since 19.02.2006
*/ */
public class MockHttpServletResponseTests extends TestCase { public class MockHttpServletResponseTests {
private MockHttpServletResponse response = new MockHttpServletResponse();
public void testSetContentType() {
@Test
public void setContentType() {
String contentType = "test/plain"; String contentType = "test/plain";
MockHttpServletResponse response = new MockHttpServletResponse();
response.setContentType(contentType); response.setContentType(contentType);
assertEquals(contentType, response.getContentType()); assertEquals(contentType, response.getContentType());
assertEquals(contentType, response.getHeader("Content-Type")); assertEquals(contentType, response.getHeader("Content-Type"));
assertEquals(WebUtils.DEFAULT_CHARACTER_ENCODING, response.getCharacterEncoding()); assertEquals(WebUtils.DEFAULT_CHARACTER_ENCODING, response.getCharacterEncoding());
} }
public void testSetContentTypeUTF8() { @Test
public void setContentTypeUTF8() {
String contentType = "test/plain;charset=UTF-8"; String contentType = "test/plain;charset=UTF-8";
MockHttpServletResponse response = new MockHttpServletResponse();
response.setContentType(contentType); response.setContentType(contentType);
assertEquals("UTF-8", response.getCharacterEncoding()); assertEquals("UTF-8", response.getCharacterEncoding());
assertEquals(contentType, response.getContentType()); assertEquals(contentType, response.getContentType());
assertEquals(contentType, response.getHeader("Content-Type")); assertEquals(contentType, response.getHeader("Content-Type"));
} }
public void testContentTypeHeader() { @Test
public void contentTypeHeader() {
String contentType = "test/plain"; String contentType = "test/plain";
MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader("Content-Type", contentType); response.addHeader("Content-Type", contentType);
assertEquals(contentType, response.getContentType()); assertEquals(contentType, response.getContentType());
assertEquals(contentType, response.getHeader("Content-Type")); assertEquals(contentType, response.getHeader("Content-Type"));
...@@ -65,9 +76,9 @@ public class MockHttpServletResponseTests extends TestCase { ...@@ -65,9 +76,9 @@ public class MockHttpServletResponseTests extends TestCase {
assertEquals(WebUtils.DEFAULT_CHARACTER_ENCODING, response.getCharacterEncoding()); assertEquals(WebUtils.DEFAULT_CHARACTER_ENCODING, response.getCharacterEncoding());
} }
public void testContentTypeHeaderUTF8() { @Test
public void contentTypeHeaderUTF8() {
String contentType = "test/plain;charset=UTF-8"; String contentType = "test/plain;charset=UTF-8";
MockHttpServletResponse response = new MockHttpServletResponse();
response.setHeader("Content-Type", contentType); response.setHeader("Content-Type", contentType);
assertEquals(contentType, response.getContentType()); assertEquals(contentType, response.getContentType());
assertEquals(contentType, response.getHeader("Content-Type")); assertEquals(contentType, response.getHeader("Content-Type"));
...@@ -80,8 +91,8 @@ public class MockHttpServletResponseTests extends TestCase { ...@@ -80,8 +91,8 @@ public class MockHttpServletResponseTests extends TestCase {
assertEquals("UTF-8", response.getCharacterEncoding()); assertEquals("UTF-8", response.getCharacterEncoding());
} }
public void testSetContentTypeThenCharacterEncoding() { @Test
MockHttpServletResponse response = new MockHttpServletResponse(); public void setContentTypeThenCharacterEncoding() {
response.setContentType("test/plain"); response.setContentType("test/plain");
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
assertEquals("test/plain", response.getContentType()); assertEquals("test/plain", response.getContentType());
...@@ -89,8 +100,8 @@ public class MockHttpServletResponseTests extends TestCase { ...@@ -89,8 +100,8 @@ public class MockHttpServletResponseTests extends TestCase {
assertEquals("UTF-8", response.getCharacterEncoding()); assertEquals("UTF-8", response.getCharacterEncoding());
} }
public void testSetCharacterEncodingThenContentType() { @Test
MockHttpServletResponse response = new MockHttpServletResponse(); public void setCharacterEncodingThenContentType() {
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
response.setContentType("test/plain"); response.setContentType("test/plain");
assertEquals("test/plain", response.getContentType()); assertEquals("test/plain", response.getContentType());
...@@ -98,24 +109,23 @@ public class MockHttpServletResponseTests extends TestCase { ...@@ -98,24 +109,23 @@ public class MockHttpServletResponseTests extends TestCase {
assertEquals("UTF-8", response.getCharacterEncoding()); assertEquals("UTF-8", response.getCharacterEncoding());
} }
public void testContentLength() { @Test
MockHttpServletResponse response = new MockHttpServletResponse(); public void contentLength() {
response.setContentLength(66); response.setContentLength(66);
assertEquals(66, response.getContentLength()); assertEquals(66, response.getContentLength());
assertEquals("66", response.getHeader("Content-Length")); assertEquals("66", response.getHeader("Content-Length"));
} }
public void testContentLengthHeader() { @Test
MockHttpServletResponse response = new MockHttpServletResponse(); public void contentLengthHeader() {
response.addHeader("Content-Length", "66"); response.addHeader("Content-Length", "66");
assertEquals(66,response.getContentLength()); assertEquals(66, response.getContentLength());
assertEquals("66", response.getHeader("Content-Length")); assertEquals("66", response.getHeader("Content-Length"));
} }
public void testHttpHeaderNameCasingIsPreserved() throws Exception {
final String headerName = "Header1";
MockHttpServletResponse response = new MockHttpServletResponse(); @Test
public void httpHeaderNameCasingIsPreserved() throws Exception {
final String headerName = "Header1";
response.addHeader(headerName, "value1"); response.addHeader(headerName, "value1");
Set<String> responseHeaders = response.getHeaderNames(); Set<String> responseHeaders = response.getHeaderNames();
assertNotNull(responseHeaders); assertNotNull(responseHeaders);
...@@ -123,8 +133,8 @@ public class MockHttpServletResponseTests extends TestCase { ...@@ -123,8 +133,8 @@ public class MockHttpServletResponseTests extends TestCase {
assertEquals("HTTP header casing not being preserved", headerName, responseHeaders.iterator().next()); assertEquals("HTTP header casing not being preserved", headerName, responseHeaders.iterator().next());
} }
public void testServletOutputStreamCommittedWhenBufferSizeExceeded() throws IOException { @Test
MockHttpServletResponse response = new MockHttpServletResponse(); public void servletOutputStreamCommittedWhenBufferSizeExceeded() throws IOException {
assertFalse(response.isCommitted()); assertFalse(response.isCommitted());
response.getOutputStream().write('X'); response.getOutputStream().write('X');
assertFalse(response.isCommitted()); assertFalse(response.isCommitted());
...@@ -134,8 +144,8 @@ public class MockHttpServletResponseTests extends TestCase { ...@@ -134,8 +144,8 @@ public class MockHttpServletResponseTests extends TestCase {
assertEquals(size + 1, response.getContentAsByteArray().length); assertEquals(size + 1, response.getContentAsByteArray().length);
} }
public void testServletOutputStreamCommittedOnFlushBuffer() throws IOException { @Test
MockHttpServletResponse response = new MockHttpServletResponse(); public void servletOutputStreamCommittedOnFlushBuffer() throws IOException {
assertFalse(response.isCommitted()); assertFalse(response.isCommitted());
response.getOutputStream().write('X'); response.getOutputStream().write('X');
assertFalse(response.isCommitted()); assertFalse(response.isCommitted());
...@@ -144,8 +154,8 @@ public class MockHttpServletResponseTests extends TestCase { ...@@ -144,8 +154,8 @@ public class MockHttpServletResponseTests extends TestCase {
assertEquals(1, response.getContentAsByteArray().length); assertEquals(1, response.getContentAsByteArray().length);
} }
public void testServletWriterCommittedWhenBufferSizeExceeded() throws IOException { @Test
MockHttpServletResponse response = new MockHttpServletResponse(); public void servletWriterCommittedWhenBufferSizeExceeded() throws IOException {
assertFalse(response.isCommitted()); assertFalse(response.isCommitted());
response.getWriter().write("X"); response.getWriter().write("X");
assertFalse(response.isCommitted()); assertFalse(response.isCommitted());
...@@ -157,8 +167,8 @@ public class MockHttpServletResponseTests extends TestCase { ...@@ -157,8 +167,8 @@ public class MockHttpServletResponseTests extends TestCase {
assertEquals(size + 1, response.getContentAsByteArray().length); assertEquals(size + 1, response.getContentAsByteArray().length);
} }
public void testServletOutputStreamCommittedOnOutputStreamFlush() throws IOException { @Test
MockHttpServletResponse response = new MockHttpServletResponse(); public void servletOutputStreamCommittedOnOutputStreamFlush() throws IOException {
assertFalse(response.isCommitted()); assertFalse(response.isCommitted());
response.getOutputStream().write('X'); response.getOutputStream().write('X');
assertFalse(response.isCommitted()); assertFalse(response.isCommitted());
...@@ -167,8 +177,8 @@ public class MockHttpServletResponseTests extends TestCase { ...@@ -167,8 +177,8 @@ public class MockHttpServletResponseTests extends TestCase {
assertEquals(1, response.getContentAsByteArray().length); assertEquals(1, response.getContentAsByteArray().length);
} }
public void testServletWriterCommittedOnWriterFlush() throws IOException { @Test
MockHttpServletResponse response = new MockHttpServletResponse(); public void servletWriterCommittedOnWriterFlush() throws IOException {
assertFalse(response.isCommitted()); assertFalse(response.isCommitted());
response.getWriter().write("X"); response.getWriter().write("X");
assertFalse(response.isCommitted()); assertFalse(response.isCommitted());
...@@ -177,22 +187,39 @@ public class MockHttpServletResponseTests extends TestCase { ...@@ -177,22 +187,39 @@ public class MockHttpServletResponseTests extends TestCase {
assertEquals(1, response.getContentAsByteArray().length); assertEquals(1, response.getContentAsByteArray().length);
} }
public void testServletWriterAutoFlushedForString() throws IOException { @Test
MockHttpServletResponse response = new MockHttpServletResponse(); public void servletWriterAutoFlushedForString() throws IOException {
response.getWriter().write("X"); response.getWriter().write("X");
assertEquals("X", response.getContentAsString()); assertEquals("X", response.getContentAsString());
} }
public void testServletWriterAutoFlushedForChar() throws IOException { @Test
MockHttpServletResponse response = new MockHttpServletResponse(); public void servletWriterAutoFlushedForChar() throws IOException {
response.getWriter().write('X'); response.getWriter().write('X');
assertEquals("X", response.getContentAsString()); assertEquals("X", response.getContentAsString());
} }
public void testServletWriterAutoFlushedForCharArray() throws IOException { @Test
MockHttpServletResponse response = new MockHttpServletResponse(); public void servletWriterAutoFlushedForCharArray() throws IOException {
response.getWriter().write("XY".toCharArray()); response.getWriter().write("XY".toCharArray());
assertEquals("XY", response.getContentAsString()); assertEquals("XY", response.getContentAsString());
} }
@Test
public void sendRedirect() throws IOException {
String redirectUrl = "/redirect";
response.sendRedirect(redirectUrl);
assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus());
assertEquals(redirectUrl, response.getHeader("Location"));
assertEquals(redirectUrl, response.getRedirectedUrl());
assertTrue(response.isCommitted());
}
@Test
public void locationHeaderUpdatesGetRedirectedUrl() {
String redirectUrl = "/redirect";
response.setHeader("Location", redirectUrl);
assertEquals(redirectUrl, response.getRedirectedUrl());
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册