Sync up MockHttpServletResponse copies in test sources
This commit is contained in:
parent
5c13739afa
commit
7513e2124a
|
|
@ -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,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
|
||||
private static final String CONTENT_LENGTH_HEADER = "Content-Length";
|
||||
|
||||
private static final String LOCATION_HEADER = "Location";
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// ServletResponse properties
|
||||
|
|
@ -96,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>();
|
||||
|
|
@ -307,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
|
||||
|
|
@ -320,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
|
||||
|
|
@ -375,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) {
|
||||
|
|
@ -412,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.
|
||||
|
|
@ -50,14 +50,13 @@ import org.springframework.web.util.WebUtils;
|
|||
*/
|
||||
public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
public static final int DEFAULT_SERVER_PORT = 80;
|
||||
|
||||
private static final String CHARSET_PREFIX = "charset=";
|
||||
|
||||
private static final String CONTENT_TYPE_HEADER = "Content-Type";
|
||||
|
||||
|
||||
private static final String CONTENT_LENGTH_HEADER = "Content-Length";
|
||||
|
||||
private static final String LOCATION_HEADER = "Location";
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// ServletResponse properties
|
||||
|
|
@ -100,8 +99,6 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
|
||||
private String errorMessage;
|
||||
|
||||
private String redirectedUrl;
|
||||
|
||||
private String forwardedUrl;
|
||||
|
||||
private final List<String> includedUrls = new ArrayList<String>();
|
||||
|
|
@ -146,7 +143,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
this.charset = true;
|
||||
updateContentTypeHeader();
|
||||
}
|
||||
|
||||
|
||||
private void updateContentTypeHeader() {
|
||||
if (this.contentType != null) {
|
||||
StringBuilder sb = new StringBuilder(this.contentType);
|
||||
|
|
@ -301,21 +298,54 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
|
||||
/**
|
||||
* Return the names of all specified headers as a Set of Strings.
|
||||
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
|
||||
* @return the <code>Set</code> of header name <code>Strings</code>, or an empty <code>Set</code> if none
|
||||
*/
|
||||
public Set<String> getHeaderNames() {
|
||||
return this.headers.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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
|
||||
* @return the associated header value, or <code>null<code> if none
|
||||
*/
|
||||
public String getHeader(String name) {
|
||||
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
|
||||
return (header != null ? header.getStringValue() : null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all values for the given header as a List of Strings.
|
||||
* <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
|
||||
* @return the associated header values, or an empty List if none
|
||||
*/
|
||||
public List<String> getHeaders(String name) {
|
||||
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
|
||||
if (header != null) {
|
||||
return header.getStringValues();
|
||||
}
|
||||
else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the primary value for the given header, if any.
|
||||
* <p>Will return the first value in case of multiple values.
|
||||
* @param name the name of the header
|
||||
* @return the associated header value, or <code>null<code> if none
|
||||
*/
|
||||
public String getHeader(String name) {
|
||||
public Object getHeaderValue(String name) {
|
||||
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
|
||||
return (header != null ? header.getValue().toString() : null);
|
||||
return (header != null ? header.getValue() : null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -323,9 +353,14 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
* @param name the name of the header
|
||||
* @return the associated header values, or an empty List if none
|
||||
*/
|
||||
public List<String> getHeaders(String name) {
|
||||
public List<Object> getHeaderValues(String name) {
|
||||
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
|
||||
return (header != null ? header.getStringValues() : Collections.<String>emptyList());
|
||||
if (header != null) {
|
||||
return header.getValues();
|
||||
}
|
||||
else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -341,7 +376,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) {
|
||||
|
|
@ -378,12 +413,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) {
|
||||
|
|
@ -423,7 +459,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
}
|
||||
doAddHeaderValue(name, value, false);
|
||||
}
|
||||
|
||||
|
||||
private boolean setSpecialHeader(String name, Object value) {
|
||||
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) {
|
||||
setContentType((String) value);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
|
|
@ -29,7 +29,6 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
|
@ -40,7 +39,7 @@ import org.springframework.web.util.WebUtils;
|
|||
|
||||
/**
|
||||
* Mock implementation of the {@link javax.servlet.http.HttpServletResponse}
|
||||
* interface. Supports the Servlet 2.5 API level.
|
||||
* interface. Supports the Servlet 3.0 API level
|
||||
*
|
||||
* <p>Used for testing the web framework; also useful for testing
|
||||
* application controllers.
|
||||
|
|
@ -51,14 +50,13 @@ import org.springframework.web.util.WebUtils;
|
|||
*/
|
||||
public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
public static final int DEFAULT_SERVER_PORT = 80;
|
||||
|
||||
private static final String CHARSET_PREFIX = "charset=";
|
||||
|
||||
private static final String CONTENT_TYPE_HEADER = "Content-Type";
|
||||
|
||||
|
||||
private static final String CONTENT_LENGTH_HEADER = "Content-Length";
|
||||
|
||||
private static final String LOCATION_HEADER = "Location";
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// ServletResponse properties
|
||||
|
|
@ -101,8 +99,6 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
|
||||
private String errorMessage;
|
||||
|
||||
private String redirectedUrl;
|
||||
|
||||
private String forwardedUrl;
|
||||
|
||||
private final List<String> includedUrls = new ArrayList<String>();
|
||||
|
|
@ -147,7 +143,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
this.charset = true;
|
||||
updateContentTypeHeader();
|
||||
}
|
||||
|
||||
|
||||
private void updateContentTypeHeader() {
|
||||
if (this.contentType != null) {
|
||||
StringBuilder sb = new StringBuilder(this.contentType);
|
||||
|
|
@ -302,21 +298,54 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
|
||||
/**
|
||||
* Return the names of all specified headers as a Set of Strings.
|
||||
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
|
||||
* @return the <code>Set</code> of header name <code>Strings</code>, or an empty <code>Set</code> if none
|
||||
*/
|
||||
public Set<String> getHeaderNames() {
|
||||
return this.headers.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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
|
||||
* @return the associated header value, or <code>null<code> if none
|
||||
*/
|
||||
public String getHeader(String name) {
|
||||
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
|
||||
return (header != null ? header.getStringValue() : null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all values for the given header as a List of Strings.
|
||||
* <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
|
||||
* @return the associated header values, or an empty List if none
|
||||
*/
|
||||
public List<String> getHeaders(String name) {
|
||||
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
|
||||
if (header != null) {
|
||||
return header.getStringValues();
|
||||
}
|
||||
else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the primary value for the given header, if any.
|
||||
* <p>Will return the first value in case of multiple values.
|
||||
* @param name the name of the header
|
||||
* @return the associated header value, or <code>null<code> if none
|
||||
*/
|
||||
public String getHeader(String name) {
|
||||
public Object getHeaderValue(String name) {
|
||||
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
|
||||
return (header != null ? header.getValue().toString() : null);
|
||||
return (header != null ? header.getValue() : null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -324,9 +353,14 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
* @param name the name of the header
|
||||
* @return the associated header values, or an empty List if none
|
||||
*/
|
||||
public List<String> getHeaders(String name) {
|
||||
public List<Object> getHeaderValues(String name) {
|
||||
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
|
||||
return (header != null ? header.getStringValues() : Collections.<String>emptyList());
|
||||
if (header != null) {
|
||||
return header.getValues();
|
||||
}
|
||||
else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -342,7 +376,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) {
|
||||
|
|
@ -379,12 +413,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) {
|
||||
|
|
@ -424,7 +459,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
}
|
||||
doAddHeaderValue(name, value, false);
|
||||
}
|
||||
|
||||
|
||||
private boolean setSpecialHeader(String name, Object value) {
|
||||
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) {
|
||||
setContentType((String) value);
|
||||
|
|
|
|||
|
|
@ -1506,7 +1506,7 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
|
|||
response = new MockHttpServletResponse();
|
||||
getServlet().service(request, response);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
assertEquals(302, response.getStatus());
|
||||
assertEquals("/messages/1?name=value", response.getRedirectedUrl());
|
||||
assertEquals("yay!", RequestContextUtils.getOutputFlashMap(request).get("successMessage"));
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue