Skip to content

Commit 59d80ec

Browse files
rwinchsbrannen
authored andcommitted
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
1 parent 67a05e4 commit 59d80ec

File tree

2 files changed

+78
-50
lines changed

2 files changed

+78
-50
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,6 +54,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
5454

5555
private static final String CONTENT_LENGTH_HEADER = "Content-Length";
5656

57+
private static final String LOCATION_HEADER = "Location";
58+
5759
//---------------------------------------------------------------------
5860
// ServletResponse properties
5961
//---------------------------------------------------------------------
@@ -95,8 +97,6 @@ public class MockHttpServletResponse implements HttpServletResponse {
9597

9698
private String errorMessage;
9799

98-
private String redirectedUrl;
99-
100100
private String forwardedUrl;
101101

102102
private final List<String> includedUrls = new ArrayList<String>();
@@ -306,7 +306,7 @@ public Set<String> getHeaderNames() {
306306
/**
307307
* Return the primary value for the given header as a String, if any.
308308
* Will return the first value in case of multiple values.
309-
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
309+
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
310310
* As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
311311
* Consider using {@link #getHeaderValue(String)} for raw Object access.
312312
* @param name the name of the header
@@ -319,7 +319,7 @@ public String getHeader(String name) {
319319

320320
/**
321321
* Return all values for the given header as a List of Strings.
322-
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
322+
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
323323
* As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
324324
* Consider using {@link #getHeaderValues(String)} for raw Object access.
325325
* @param name the name of the header
@@ -374,7 +374,7 @@ public String encodeURL(String url) {
374374
* returning the given URL String as-is.
375375
* <p>Can be overridden in subclasses, appending a session id or the like
376376
* in a redirect-specific fashion. For general URL encoding rules,
377-
* override the common {@link #encodeURL} method instead, appyling
377+
* override the common {@link #encodeURL} method instead, applying
378378
* to redirect URLs as well as to general URLs.
379379
*/
380380
public String encodeRedirectURL(String url) {
@@ -411,12 +411,13 @@ public void sendRedirect(String url) throws IOException {
411411
throw new IllegalStateException("Cannot send redirect - response is already committed");
412412
}
413413
Assert.notNull(url, "Redirect URL must not be null");
414-
this.redirectedUrl = url;
414+
setHeader(LOCATION_HEADER, url);
415+
setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
415416
setCommitted(true);
416417
}
417418

418419
public String getRedirectedUrl() {
419-
return this.redirectedUrl;
420+
return getHeader(LOCATION_HEADER);
420421
}
421422

422423
public void setDateHeader(String name, long value) {
Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,43 +16,54 @@
1616

1717
package org.springframework.mock.web;
1818

19+
import static org.junit.Assert.*;
20+
1921
import java.io.IOException;
2022
import java.util.Arrays;
2123
import java.util.Set;
2224

23-
import junit.framework.TestCase;
25+
import javax.servlet.http.HttpServletResponse;
26+
27+
import org.junit.Test;
2428

2529
import org.springframework.web.util.WebUtils;
2630

2731
/**
32+
* Unit tests for {@link MockHttpServletResponse}.
33+
*
2834
* @author Juergen Hoeller
2935
* @author Rick Evans
3036
* @author Rossen Stoyanchev
37+
* @author Rob Winch
38+
* @author Sam Brannen
3139
* @since 19.02.2006
3240
*/
33-
public class MockHttpServletResponseTests extends TestCase {
41+
public class MockHttpServletResponseTests {
42+
43+
private MockHttpServletResponse response = new MockHttpServletResponse();
3444

35-
public void testSetContentType() {
45+
46+
@Test
47+
public void setContentType() {
3648
String contentType = "test/plain";
37-
MockHttpServletResponse response = new MockHttpServletResponse();
3849
response.setContentType(contentType);
3950
assertEquals(contentType, response.getContentType());
4051
assertEquals(contentType, response.getHeader("Content-Type"));
4152
assertEquals(WebUtils.DEFAULT_CHARACTER_ENCODING, response.getCharacterEncoding());
4253
}
4354

44-
public void testSetContentTypeUTF8() {
55+
@Test
56+
public void setContentTypeUTF8() {
4557
String contentType = "test/plain;charset=UTF-8";
46-
MockHttpServletResponse response = new MockHttpServletResponse();
4758
response.setContentType(contentType);
4859
assertEquals("UTF-8", response.getCharacterEncoding());
4960
assertEquals(contentType, response.getContentType());
5061
assertEquals(contentType, response.getHeader("Content-Type"));
5162
}
52-
53-
public void testContentTypeHeader() {
63+
64+
@Test
65+
public void contentTypeHeader() {
5466
String contentType = "test/plain";
55-
MockHttpServletResponse response = new MockHttpServletResponse();
5667
response.addHeader("Content-Type", contentType);
5768
assertEquals(contentType, response.getContentType());
5869
assertEquals(contentType, response.getHeader("Content-Type"));
@@ -65,9 +76,9 @@ public void testContentTypeHeader() {
6576
assertEquals(WebUtils.DEFAULT_CHARACTER_ENCODING, response.getCharacterEncoding());
6677
}
6778

68-
public void testContentTypeHeaderUTF8() {
79+
@Test
80+
public void contentTypeHeaderUTF8() {
6981
String contentType = "test/plain;charset=UTF-8";
70-
MockHttpServletResponse response = new MockHttpServletResponse();
7182
response.setHeader("Content-Type", contentType);
7283
assertEquals(contentType, response.getContentType());
7384
assertEquals(contentType, response.getHeader("Content-Type"));
@@ -80,51 +91,50 @@ public void testContentTypeHeaderUTF8() {
8091
assertEquals("UTF-8", response.getCharacterEncoding());
8192
}
8293

83-
public void testSetContentTypeThenCharacterEncoding() {
84-
MockHttpServletResponse response = new MockHttpServletResponse();
94+
@Test
95+
public void setContentTypeThenCharacterEncoding() {
8596
response.setContentType("test/plain");
8697
response.setCharacterEncoding("UTF-8");
8798
assertEquals("test/plain", response.getContentType());
8899
assertEquals("test/plain;charset=UTF-8", response.getHeader("Content-Type"));
89100
assertEquals("UTF-8", response.getCharacterEncoding());
90101
}
91102

92-
public void testSetCharacterEncodingThenContentType() {
93-
MockHttpServletResponse response = new MockHttpServletResponse();
103+
@Test
104+
public void setCharacterEncodingThenContentType() {
94105
response.setCharacterEncoding("UTF-8");
95106
response.setContentType("test/plain");
96107
assertEquals("test/plain", response.getContentType());
97108
assertEquals("test/plain;charset=UTF-8", response.getHeader("Content-Type"));
98109
assertEquals("UTF-8", response.getCharacterEncoding());
99110
}
100111

101-
public void testContentLength() {
102-
MockHttpServletResponse response = new MockHttpServletResponse();
112+
@Test
113+
public void contentLength() {
103114
response.setContentLength(66);
104115
assertEquals(66, response.getContentLength());
105116
assertEquals("66", response.getHeader("Content-Length"));
106117
}
107-
108-
public void testContentLengthHeader() {
109-
MockHttpServletResponse response = new MockHttpServletResponse();
118+
119+
@Test
120+
public void contentLengthHeader() {
110121
response.addHeader("Content-Length", "66");
111-
assertEquals(66,response.getContentLength());
122+
assertEquals(66, response.getContentLength());
112123
assertEquals("66", response.getHeader("Content-Length"));
113124
}
114-
115-
public void testHttpHeaderNameCasingIsPreserved() throws Exception {
116-
final String headerName = "Header1";
117125

118-
MockHttpServletResponse response = new MockHttpServletResponse();
126+
@Test
127+
public void httpHeaderNameCasingIsPreserved() throws Exception {
128+
final String headerName = "Header1";
119129
response.addHeader(headerName, "value1");
120130
Set<String> responseHeaders = response.getHeaderNames();
121131
assertNotNull(responseHeaders);
122132
assertEquals(1, responseHeaders.size());
123133
assertEquals("HTTP header casing not being preserved", headerName, responseHeaders.iterator().next());
124134
}
125135

126-
public void testServletOutputStreamCommittedWhenBufferSizeExceeded() throws IOException {
127-
MockHttpServletResponse response = new MockHttpServletResponse();
136+
@Test
137+
public void servletOutputStreamCommittedWhenBufferSizeExceeded() throws IOException {
128138
assertFalse(response.isCommitted());
129139
response.getOutputStream().write('X');
130140
assertFalse(response.isCommitted());
@@ -134,8 +144,8 @@ public void testServletOutputStreamCommittedWhenBufferSizeExceeded() throws IOEx
134144
assertEquals(size + 1, response.getContentAsByteArray().length);
135145
}
136146

137-
public void testServletOutputStreamCommittedOnFlushBuffer() throws IOException {
138-
MockHttpServletResponse response = new MockHttpServletResponse();
147+
@Test
148+
public void servletOutputStreamCommittedOnFlushBuffer() throws IOException {
139149
assertFalse(response.isCommitted());
140150
response.getOutputStream().write('X');
141151
assertFalse(response.isCommitted());
@@ -144,8 +154,8 @@ public void testServletOutputStreamCommittedOnFlushBuffer() throws IOException {
144154
assertEquals(1, response.getContentAsByteArray().length);
145155
}
146156

147-
public void testServletWriterCommittedWhenBufferSizeExceeded() throws IOException {
148-
MockHttpServletResponse response = new MockHttpServletResponse();
157+
@Test
158+
public void servletWriterCommittedWhenBufferSizeExceeded() throws IOException {
149159
assertFalse(response.isCommitted());
150160
response.getWriter().write("X");
151161
assertFalse(response.isCommitted());
@@ -157,8 +167,8 @@ public void testServletWriterCommittedWhenBufferSizeExceeded() throws IOExceptio
157167
assertEquals(size + 1, response.getContentAsByteArray().length);
158168
}
159169

160-
public void testServletOutputStreamCommittedOnOutputStreamFlush() throws IOException {
161-
MockHttpServletResponse response = new MockHttpServletResponse();
170+
@Test
171+
public void servletOutputStreamCommittedOnOutputStreamFlush() throws IOException {
162172
assertFalse(response.isCommitted());
163173
response.getOutputStream().write('X');
164174
assertFalse(response.isCommitted());
@@ -167,8 +177,8 @@ public void testServletOutputStreamCommittedOnOutputStreamFlush() throws IOExcep
167177
assertEquals(1, response.getContentAsByteArray().length);
168178
}
169179

170-
public void testServletWriterCommittedOnWriterFlush() throws IOException {
171-
MockHttpServletResponse response = new MockHttpServletResponse();
180+
@Test
181+
public void servletWriterCommittedOnWriterFlush() throws IOException {
172182
assertFalse(response.isCommitted());
173183
response.getWriter().write("X");
174184
assertFalse(response.isCommitted());
@@ -177,22 +187,39 @@ public void testServletWriterCommittedOnWriterFlush() throws IOException {
177187
assertEquals(1, response.getContentAsByteArray().length);
178188
}
179189

180-
public void testServletWriterAutoFlushedForString() throws IOException {
181-
MockHttpServletResponse response = new MockHttpServletResponse();
190+
@Test
191+
public void servletWriterAutoFlushedForString() throws IOException {
182192
response.getWriter().write("X");
183193
assertEquals("X", response.getContentAsString());
184194
}
185195

186-
public void testServletWriterAutoFlushedForChar() throws IOException {
187-
MockHttpServletResponse response = new MockHttpServletResponse();
196+
@Test
197+
public void servletWriterAutoFlushedForChar() throws IOException {
188198
response.getWriter().write('X');
189199
assertEquals("X", response.getContentAsString());
190200
}
191201

192-
public void testServletWriterAutoFlushedForCharArray() throws IOException {
193-
MockHttpServletResponse response = new MockHttpServletResponse();
202+
@Test
203+
public void servletWriterAutoFlushedForCharArray() throws IOException {
194204
response.getWriter().write("XY".toCharArray());
195205
assertEquals("XY", response.getContentAsString());
196206
}
197207

208+
@Test
209+
public void sendRedirect() throws IOException {
210+
String redirectUrl = "/redirect";
211+
response.sendRedirect(redirectUrl);
212+
assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus());
213+
assertEquals(redirectUrl, response.getHeader("Location"));
214+
assertEquals(redirectUrl, response.getRedirectedUrl());
215+
assertTrue(response.isCommitted());
216+
}
217+
218+
@Test
219+
public void locationHeaderUpdatesGetRedirectedUrl() {
220+
String redirectUrl = "/redirect";
221+
response.setHeader("Location", redirectUrl);
222+
assertEquals(redirectUrl, response.getRedirectedUrl());
223+
}
224+
198225
}

0 commit comments

Comments
 (0)