Skip to content

Commit 7513e21

Browse files
committed
Sync up MockHttpServletResponse copies in test sources
1 parent 5c13739 commit 7513e21

File tree

4 files changed

+112
-41
lines changed

4 files changed

+112
-41
lines changed

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

Lines changed: 8 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,7 @@ 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";
5758

5859
//---------------------------------------------------------------------
5960
// ServletResponse properties
@@ -96,8 +97,6 @@ public class MockHttpServletResponse implements HttpServletResponse {
9697

9798
private String errorMessage;
9899

99-
private String redirectedUrl;
100-
101100
private String forwardedUrl;
102101

103102
private final List<String> includedUrls = new ArrayList<String>();
@@ -307,7 +306,7 @@ public Set<String> getHeaderNames() {
307306
/**
308307
* Return the primary value for the given header as a String, if any.
309308
* Will return the first value in case of multiple values.
310-
* <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.
311310
* As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
312311
* Consider using {@link #getHeaderValue(String)} for raw Object access.
313312
* @param name the name of the header
@@ -320,7 +319,7 @@ public String getHeader(String name) {
320319

321320
/**
322321
* Return all values for the given header as a List of Strings.
323-
* <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.
324323
* As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
325324
* Consider using {@link #getHeaderValues(String)} for raw Object access.
326325
* @param name the name of the header
@@ -375,7 +374,7 @@ public String encodeURL(String url) {
375374
* returning the given URL String as-is.
376375
* <p>Can be overridden in subclasses, appending a session id or the like
377376
* in a redirect-specific fashion. For general URL encoding rules,
378-
* override the common {@link #encodeURL} method instead, appyling
377+
* override the common {@link #encodeURL} method instead, applying
379378
* to redirect URLs as well as to general URLs.
380379
*/
381380
public String encodeRedirectURL(String url) {
@@ -412,12 +411,13 @@ public void sendRedirect(String url) throws IOException {
412411
throw new IllegalStateException("Cannot send redirect - response is already committed");
413412
}
414413
Assert.notNull(url, "Redirect URL must not be null");
415-
this.redirectedUrl = url;
414+
setHeader(LOCATION_HEADER, url);
415+
setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
416416
setCommitted(true);
417417
}
418418

419419
public String getRedirectedUrl() {
420-
return this.redirectedUrl;
420+
return getHeader(LOCATION_HEADER);
421421
}
422422

423423
public void setDateHeader(String name, long value) {

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

Lines changed: 51 additions & 15 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.
@@ -50,14 +50,13 @@
5050
*/
5151
public class MockHttpServletResponse implements HttpServletResponse {
5252

53-
public static final int DEFAULT_SERVER_PORT = 80;
54-
5553
private static final String CHARSET_PREFIX = "charset=";
5654

5755
private static final String CONTENT_TYPE_HEADER = "Content-Type";
58-
56+
5957
private static final String CONTENT_LENGTH_HEADER = "Content-Length";
6058

59+
private static final String LOCATION_HEADER = "Location";
6160

6261
//---------------------------------------------------------------------
6362
// ServletResponse properties
@@ -100,8 +99,6 @@ public class MockHttpServletResponse implements HttpServletResponse {
10099

101100
private String errorMessage;
102101

103-
private String redirectedUrl;
104-
105102
private String forwardedUrl;
106103

107104
private final List<String> includedUrls = new ArrayList<String>();
@@ -146,7 +143,7 @@ public void setCharacterEncoding(String characterEncoding) {
146143
this.charset = true;
147144
updateContentTypeHeader();
148145
}
149-
146+
150147
private void updateContentTypeHeader() {
151148
if (this.contentType != null) {
152149
StringBuilder sb = new StringBuilder(this.contentType);
@@ -301,31 +298,69 @@ public boolean containsHeader(String name) {
301298

302299
/**
303300
* Return the names of all specified headers as a Set of Strings.
301+
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
304302
* @return the <code>Set</code> of header name <code>Strings</code>, or an empty <code>Set</code> if none
305303
*/
306304
public Set<String> getHeaderNames() {
307305
return this.headers.keySet();
308306
}
309307

308+
/**
309+
* Return the primary value for the given header as a String, if any.
310+
* Will return the first value in case of multiple values.
311+
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
312+
* As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
313+
* Consider using {@link #getHeaderValue(String)} for raw Object access.
314+
* @param name the name of the header
315+
* @return the associated header value, or <code>null<code> if none
316+
*/
317+
public String getHeader(String name) {
318+
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
319+
return (header != null ? header.getStringValue() : null);
320+
}
321+
322+
/**
323+
* Return all values for the given header as a List of Strings.
324+
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
325+
* As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
326+
* Consider using {@link #getHeaderValues(String)} for raw Object access.
327+
* @param name the name of the header
328+
* @return the associated header values, or an empty List if none
329+
*/
330+
public List<String> getHeaders(String name) {
331+
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
332+
if (header != null) {
333+
return header.getStringValues();
334+
}
335+
else {
336+
return Collections.emptyList();
337+
}
338+
}
339+
310340
/**
311341
* Return the primary value for the given header, if any.
312342
* <p>Will return the first value in case of multiple values.
313343
* @param name the name of the header
314344
* @return the associated header value, or <code>null<code> if none
315345
*/
316-
public String getHeader(String name) {
346+
public Object getHeaderValue(String name) {
317347
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
318-
return (header != null ? header.getValue().toString() : null);
348+
return (header != null ? header.getValue() : null);
319349
}
320350

321351
/**
322352
* Return all values for the given header as a List of value objects.
323353
* @param name the name of the header
324354
* @return the associated header values, or an empty List if none
325355
*/
326-
public List<String> getHeaders(String name) {
356+
public List<Object> getHeaderValues(String name) {
327357
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
328-
return (header != null ? header.getStringValues() : Collections.<String>emptyList());
358+
if (header != null) {
359+
return header.getValues();
360+
}
361+
else {
362+
return Collections.emptyList();
363+
}
329364
}
330365

331366
/**
@@ -341,7 +376,7 @@ public String encodeURL(String url) {
341376
* returning the given URL String as-is.
342377
* <p>Can be overridden in subclasses, appending a session id or the like
343378
* in a redirect-specific fashion. For general URL encoding rules,
344-
* override the common {@link #encodeURL} method instead, appyling
379+
* override the common {@link #encodeURL} method instead, applying
345380
* to redirect URLs as well as to general URLs.
346381
*/
347382
public String encodeRedirectURL(String url) {
@@ -378,12 +413,13 @@ public void sendRedirect(String url) throws IOException {
378413
throw new IllegalStateException("Cannot send redirect - response is already committed");
379414
}
380415
Assert.notNull(url, "Redirect URL must not be null");
381-
this.redirectedUrl = url;
416+
setHeader(LOCATION_HEADER, url);
417+
setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
382418
setCommitted(true);
383419
}
384420

385421
public String getRedirectedUrl() {
386-
return this.redirectedUrl;
422+
return getHeader(LOCATION_HEADER);
387423
}
388424

389425
public void setDateHeader(String name, long value) {
@@ -423,7 +459,7 @@ private void addHeaderValue(String name, Object value) {
423459
}
424460
doAddHeaderValue(name, value, false);
425461
}
426-
462+
427463
private boolean setSpecialHeader(String name, Object value) {
428464
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) {
429465
setContentType((String) value);

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

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 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.
@@ -29,7 +29,6 @@
2929
import java.util.Locale;
3030
import java.util.Map;
3131
import java.util.Set;
32-
3332
import javax.servlet.ServletOutputStream;
3433
import javax.servlet.http.Cookie;
3534
import javax.servlet.http.HttpServletResponse;
@@ -40,7 +39,7 @@
4039

4140
/**
4241
* Mock implementation of the {@link javax.servlet.http.HttpServletResponse}
43-
* interface. Supports the Servlet 2.5 API level.
42+
* interface. Supports the Servlet 3.0 API level
4443
*
4544
* <p>Used for testing the web framework; also useful for testing
4645
* application controllers.
@@ -51,14 +50,13 @@
5150
*/
5251
public class MockHttpServletResponse implements HttpServletResponse {
5352

54-
public static final int DEFAULT_SERVER_PORT = 80;
55-
5653
private static final String CHARSET_PREFIX = "charset=";
5754

5855
private static final String CONTENT_TYPE_HEADER = "Content-Type";
59-
56+
6057
private static final String CONTENT_LENGTH_HEADER = "Content-Length";
6158

59+
private static final String LOCATION_HEADER = "Location";
6260

6361
//---------------------------------------------------------------------
6462
// ServletResponse properties
@@ -101,8 +99,6 @@ public class MockHttpServletResponse implements HttpServletResponse {
10199

102100
private String errorMessage;
103101

104-
private String redirectedUrl;
105-
106102
private String forwardedUrl;
107103

108104
private final List<String> includedUrls = new ArrayList<String>();
@@ -147,7 +143,7 @@ public void setCharacterEncoding(String characterEncoding) {
147143
this.charset = true;
148144
updateContentTypeHeader();
149145
}
150-
146+
151147
private void updateContentTypeHeader() {
152148
if (this.contentType != null) {
153149
StringBuilder sb = new StringBuilder(this.contentType);
@@ -302,31 +298,69 @@ public boolean containsHeader(String name) {
302298

303299
/**
304300
* Return the names of all specified headers as a Set of Strings.
301+
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
305302
* @return the <code>Set</code> of header name <code>Strings</code>, or an empty <code>Set</code> if none
306303
*/
307304
public Set<String> getHeaderNames() {
308305
return this.headers.keySet();
309306
}
310307

308+
/**
309+
* Return the primary value for the given header as a String, if any.
310+
* Will return the first value in case of multiple values.
311+
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
312+
* As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
313+
* Consider using {@link #getHeaderValue(String)} for raw Object access.
314+
* @param name the name of the header
315+
* @return the associated header value, or <code>null<code> if none
316+
*/
317+
public String getHeader(String name) {
318+
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
319+
return (header != null ? header.getStringValue() : null);
320+
}
321+
322+
/**
323+
* Return all values for the given header as a List of Strings.
324+
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
325+
* As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
326+
* Consider using {@link #getHeaderValues(String)} for raw Object access.
327+
* @param name the name of the header
328+
* @return the associated header values, or an empty List if none
329+
*/
330+
public List<String> getHeaders(String name) {
331+
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
332+
if (header != null) {
333+
return header.getStringValues();
334+
}
335+
else {
336+
return Collections.emptyList();
337+
}
338+
}
339+
311340
/**
312341
* Return the primary value for the given header, if any.
313342
* <p>Will return the first value in case of multiple values.
314343
* @param name the name of the header
315344
* @return the associated header value, or <code>null<code> if none
316345
*/
317-
public String getHeader(String name) {
346+
public Object getHeaderValue(String name) {
318347
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
319-
return (header != null ? header.getValue().toString() : null);
348+
return (header != null ? header.getValue() : null);
320349
}
321350

322351
/**
323352
* Return all values for the given header as a List of value objects.
324353
* @param name the name of the header
325354
* @return the associated header values, or an empty List if none
326355
*/
327-
public List<String> getHeaders(String name) {
356+
public List<Object> getHeaderValues(String name) {
328357
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
329-
return (header != null ? header.getStringValues() : Collections.<String>emptyList());
358+
if (header != null) {
359+
return header.getValues();
360+
}
361+
else {
362+
return Collections.emptyList();
363+
}
330364
}
331365

332366
/**
@@ -342,7 +376,7 @@ public String encodeURL(String url) {
342376
* returning the given URL String as-is.
343377
* <p>Can be overridden in subclasses, appending a session id or the like
344378
* in a redirect-specific fashion. For general URL encoding rules,
345-
* override the common {@link #encodeURL} method instead, appyling
379+
* override the common {@link #encodeURL} method instead, applying
346380
* to redirect URLs as well as to general URLs.
347381
*/
348382
public String encodeRedirectURL(String url) {
@@ -379,12 +413,13 @@ public void sendRedirect(String url) throws IOException {
379413
throw new IllegalStateException("Cannot send redirect - response is already committed");
380414
}
381415
Assert.notNull(url, "Redirect URL must not be null");
382-
this.redirectedUrl = url;
416+
setHeader(LOCATION_HEADER, url);
417+
setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
383418
setCommitted(true);
384419
}
385420

386421
public String getRedirectedUrl() {
387-
return this.redirectedUrl;
422+
return getHeader(LOCATION_HEADER);
388423
}
389424

390425
public void setDateHeader(String name, long value) {
@@ -424,7 +459,7 @@ private void addHeaderValue(String name, Object value) {
424459
}
425460
doAddHeaderValue(name, value, false);
426461
}
427-
462+
428463
private boolean setSpecialHeader(String name, Object value) {
429464
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) {
430465
setContentType((String) value);

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,7 @@ public void redirectAttribute() throws Exception {
15061506
response = new MockHttpServletResponse();
15071507
getServlet().service(request, response);
15081508

1509-
assertEquals(200, response.getStatus());
1509+
assertEquals(302, response.getStatus());
15101510
assertEquals("/messages/1?name=value", response.getRedirectedUrl());
15111511
assertEquals("yay!", RequestContextUtils.getOutputFlashMap(request).get("successMessage"));
15121512

0 commit comments

Comments
 (0)