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
This commit is contained in:
parent
67a05e4185
commit
59d80ec19e
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* 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");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -54,6 +54,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
|
||||
private static final String CONTENT_LENGTH_HEADER = "Content-Length";
|
||||
|
||||
private static final String LOCATION_HEADER = "Location";
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// ServletResponse properties
|
||||
//---------------------------------------------------------------------
|
||||
|
|
@ -95,8 +97,6 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
|
||||
private String errorMessage;
|
||||
|
||||
private String redirectedUrl;
|
||||
|
||||
private String forwardedUrl;
|
||||
|
||||
private final List<String> includedUrls = new ArrayList<String>();
|
||||
|
|
@ -306,7 +306,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
/**
|
||||
* Return the primary value for the given header as a String, if any.
|
||||
* 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.
|
||||
* Consider using {@link #getHeaderValue(String)} for raw Object access.
|
||||
* @param name the name of the header
|
||||
|
|
@ -319,7 +319,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
|
||||
/**
|
||||
* 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.
|
||||
* Consider using {@link #getHeaderValues(String)} for raw Object access.
|
||||
* @param name the name of the header
|
||||
|
|
@ -374,7 +374,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
* returning the given URL String as-is.
|
||||
* <p>Can be overridden in subclasses, appending a session id or the like
|
||||
* 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.
|
||||
*/
|
||||
public String encodeRedirectURL(String url) {
|
||||
|
|
@ -411,12 +411,13 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
throw new IllegalStateException("Cannot send redirect - response is already committed");
|
||||
}
|
||||
Assert.notNull(url, "Redirect URL must not be null");
|
||||
this.redirectedUrl = url;
|
||||
setHeader(LOCATION_HEADER, url);
|
||||
setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
|
||||
setCommitted(true);
|
||||
}
|
||||
|
||||
public String getRedirectedUrl() {
|
||||
return this.redirectedUrl;
|
||||
return getHeader(LOCATION_HEADER);
|
||||
}
|
||||
|
||||
public void setDateHeader(String name, long value) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* 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");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -16,43 +16,54 @@
|
|||
|
||||
package org.springframework.mock.web;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Set;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.web.util.WebUtils;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MockHttpServletResponse}.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Rick Evans
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Rob Winch
|
||||
* @author Sam Brannen
|
||||
* @since 19.02.2006
|
||||
*/
|
||||
public class MockHttpServletResponseTests extends TestCase {
|
||||
public class MockHttpServletResponseTests {
|
||||
|
||||
public void testSetContentType() {
|
||||
private MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
|
||||
@Test
|
||||
public void setContentType() {
|
||||
String contentType = "test/plain";
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
response.setContentType(contentType);
|
||||
assertEquals(contentType, response.getContentType());
|
||||
assertEquals(contentType, response.getHeader("Content-Type"));
|
||||
assertEquals(WebUtils.DEFAULT_CHARACTER_ENCODING, response.getCharacterEncoding());
|
||||
}
|
||||
|
||||
public void testSetContentTypeUTF8() {
|
||||
@Test
|
||||
public void setContentTypeUTF8() {
|
||||
String contentType = "test/plain;charset=UTF-8";
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
response.setContentType(contentType);
|
||||
assertEquals("UTF-8", response.getCharacterEncoding());
|
||||
assertEquals(contentType, response.getContentType());
|
||||
assertEquals(contentType, response.getHeader("Content-Type"));
|
||||
}
|
||||
|
||||
public void testContentTypeHeader() {
|
||||
|
||||
@Test
|
||||
public void contentTypeHeader() {
|
||||
String contentType = "test/plain";
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
response.addHeader("Content-Type", contentType);
|
||||
assertEquals(contentType, response.getContentType());
|
||||
assertEquals(contentType, response.getHeader("Content-Type"));
|
||||
|
|
@ -65,9 +76,9 @@ public class MockHttpServletResponseTests extends TestCase {
|
|||
assertEquals(WebUtils.DEFAULT_CHARACTER_ENCODING, response.getCharacterEncoding());
|
||||
}
|
||||
|
||||
public void testContentTypeHeaderUTF8() {
|
||||
@Test
|
||||
public void contentTypeHeaderUTF8() {
|
||||
String contentType = "test/plain;charset=UTF-8";
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
response.setHeader("Content-Type", contentType);
|
||||
assertEquals(contentType, response.getContentType());
|
||||
assertEquals(contentType, response.getHeader("Content-Type"));
|
||||
|
|
@ -80,8 +91,8 @@ public class MockHttpServletResponseTests extends TestCase {
|
|||
assertEquals("UTF-8", response.getCharacterEncoding());
|
||||
}
|
||||
|
||||
public void testSetContentTypeThenCharacterEncoding() {
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
@Test
|
||||
public void setContentTypeThenCharacterEncoding() {
|
||||
response.setContentType("test/plain");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
assertEquals("test/plain", response.getContentType());
|
||||
|
|
@ -89,8 +100,8 @@ public class MockHttpServletResponseTests extends TestCase {
|
|||
assertEquals("UTF-8", response.getCharacterEncoding());
|
||||
}
|
||||
|
||||
public void testSetCharacterEncodingThenContentType() {
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
@Test
|
||||
public void setCharacterEncodingThenContentType() {
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setContentType("test/plain");
|
||||
assertEquals("test/plain", response.getContentType());
|
||||
|
|
@ -98,24 +109,23 @@ public class MockHttpServletResponseTests extends TestCase {
|
|||
assertEquals("UTF-8", response.getCharacterEncoding());
|
||||
}
|
||||
|
||||
public void testContentLength() {
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
@Test
|
||||
public void contentLength() {
|
||||
response.setContentLength(66);
|
||||
assertEquals(66, response.getContentLength());
|
||||
assertEquals("66", response.getHeader("Content-Length"));
|
||||
}
|
||||
|
||||
public void testContentLengthHeader() {
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
@Test
|
||||
public void contentLengthHeader() {
|
||||
response.addHeader("Content-Length", "66");
|
||||
assertEquals(66,response.getContentLength());
|
||||
assertEquals(66, response.getContentLength());
|
||||
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");
|
||||
Set<String> responseHeaders = response.getHeaderNames();
|
||||
assertNotNull(responseHeaders);
|
||||
|
|
@ -123,8 +133,8 @@ public class MockHttpServletResponseTests extends TestCase {
|
|||
assertEquals("HTTP header casing not being preserved", headerName, responseHeaders.iterator().next());
|
||||
}
|
||||
|
||||
public void testServletOutputStreamCommittedWhenBufferSizeExceeded() throws IOException {
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
@Test
|
||||
public void servletOutputStreamCommittedWhenBufferSizeExceeded() throws IOException {
|
||||
assertFalse(response.isCommitted());
|
||||
response.getOutputStream().write('X');
|
||||
assertFalse(response.isCommitted());
|
||||
|
|
@ -134,8 +144,8 @@ public class MockHttpServletResponseTests extends TestCase {
|
|||
assertEquals(size + 1, response.getContentAsByteArray().length);
|
||||
}
|
||||
|
||||
public void testServletOutputStreamCommittedOnFlushBuffer() throws IOException {
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
@Test
|
||||
public void servletOutputStreamCommittedOnFlushBuffer() throws IOException {
|
||||
assertFalse(response.isCommitted());
|
||||
response.getOutputStream().write('X');
|
||||
assertFalse(response.isCommitted());
|
||||
|
|
@ -144,8 +154,8 @@ public class MockHttpServletResponseTests extends TestCase {
|
|||
assertEquals(1, response.getContentAsByteArray().length);
|
||||
}
|
||||
|
||||
public void testServletWriterCommittedWhenBufferSizeExceeded() throws IOException {
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
@Test
|
||||
public void servletWriterCommittedWhenBufferSizeExceeded() throws IOException {
|
||||
assertFalse(response.isCommitted());
|
||||
response.getWriter().write("X");
|
||||
assertFalse(response.isCommitted());
|
||||
|
|
@ -157,8 +167,8 @@ public class MockHttpServletResponseTests extends TestCase {
|
|||
assertEquals(size + 1, response.getContentAsByteArray().length);
|
||||
}
|
||||
|
||||
public void testServletOutputStreamCommittedOnOutputStreamFlush() throws IOException {
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
@Test
|
||||
public void servletOutputStreamCommittedOnOutputStreamFlush() throws IOException {
|
||||
assertFalse(response.isCommitted());
|
||||
response.getOutputStream().write('X');
|
||||
assertFalse(response.isCommitted());
|
||||
|
|
@ -167,8 +177,8 @@ public class MockHttpServletResponseTests extends TestCase {
|
|||
assertEquals(1, response.getContentAsByteArray().length);
|
||||
}
|
||||
|
||||
public void testServletWriterCommittedOnWriterFlush() throws IOException {
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
@Test
|
||||
public void servletWriterCommittedOnWriterFlush() throws IOException {
|
||||
assertFalse(response.isCommitted());
|
||||
response.getWriter().write("X");
|
||||
assertFalse(response.isCommitted());
|
||||
|
|
@ -177,22 +187,39 @@ public class MockHttpServletResponseTests extends TestCase {
|
|||
assertEquals(1, response.getContentAsByteArray().length);
|
||||
}
|
||||
|
||||
public void testServletWriterAutoFlushedForString() throws IOException {
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
@Test
|
||||
public void servletWriterAutoFlushedForString() throws IOException {
|
||||
response.getWriter().write("X");
|
||||
assertEquals("X", response.getContentAsString());
|
||||
}
|
||||
|
||||
public void testServletWriterAutoFlushedForChar() throws IOException {
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
@Test
|
||||
public void servletWriterAutoFlushedForChar() throws IOException {
|
||||
response.getWriter().write('X');
|
||||
assertEquals("X", response.getContentAsString());
|
||||
}
|
||||
|
||||
public void testServletWriterAutoFlushedForCharArray() throws IOException {
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
@Test
|
||||
public void servletWriterAutoFlushedForCharArray() throws IOException {
|
||||
response.getWriter().write("XY".toCharArray());
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue