Class MockHttpServletResponse
- java.lang.Object
- org.springframework.mock.web.MockHttpServletResponse
- All Implemented Interfaces:
HttpServletResponse
,ServletResponse
public class MockHttpServletResponse extends Object implements HttpServletResponse
Mock implementation of theHttpServletResponse
interface.As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline. Beyond that,
MockHttpServletResponse
is also compatible with Servlet 3.1'ssetContentLengthLong()
method.- Since:
- 1.0.2
- Author:
- Juergen Hoeller, Rod Johnson, Brian Clozel
Field Summary
Fields inherited from interface javax.servlet.http.HttpServletResponse
SC_ACCEPTED, SC_BAD_GATEWAY, SC_BAD_REQUEST, SC_CONFLICT, SC_CONTINUE, SC_CREATED, SC_EXPECTATION_FAILED, SC_FORBIDDEN, SC_FOUND, SC_GATEWAY_TIMEOUT, SC_GONE, SC_HTTP_VERSION_NOT_SUPPORTED, SC_INTERNAL_SERVER_ERROR, SC_LENGTH_REQUIRED, SC_METHOD_NOT_ALLOWED, SC_MOVED_PERMANENTLY, SC_MOVED_TEMPORARILY, SC_MULTIPLE_CHOICES, SC_NO_CONTENT, SC_NON_AUTHORITATIVE_INFORMATION, SC_NOT_ACCEPTABLE, SC_NOT_FOUND, SC_NOT_IMPLEMENTED, SC_NOT_MODIFIED, SC_OK, SC_PARTIAL_CONTENT, SC_PAYMENT_REQUIRED, SC_PRECONDITION_FAILED, SC_PROXY_AUTHENTICATION_REQUIRED, SC_REQUEST_ENTITY_TOO_LARGE, SC_REQUEST_TIMEOUT, SC_REQUEST_URI_TOO_LONG, SC_REQUESTED_RANGE_NOT_SATISFIABLE, SC_RESET_CONTENT, SC_SEE_OTHER, SC_SERVICE_UNAVAILABLE, SC_SWITCHING_PROTOCOLS, SC_TEMPORARY_REDIRECT, SC_UNAUTHORIZED, SC_UNSUPPORTED_MEDIA_TYPE, SC_USE_PROXY
Constructor Summary
Constructors Constructor Description MockHttpServletResponse()
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addCookie(Cookie cookie)
void
addDateHeader(String name, long value)
void
addHeader(String name, String value)
void
addIncludedUrl(String includedUrl)
void
addIntHeader(String name, int value)
boolean
containsHeader(String name)
String
encodeRedirectUrl(String url)
Deprecated.String
encodeRedirectURL(String url)
The default implementation delegates toencodeURL(java.lang.String)
, returning the given URL String as-is.String
encodeUrl(String url)
Deprecated.String
encodeURL(String url)
The default implementation returns the given URL String as-is.void
flushBuffer()
int
getBufferSize()
String
getCharacterEncoding()
byte[]
getContentAsByteArray()
String
getContentAsString()
int
getContentLength()
long
getContentLengthLong()
String
getContentType()
Cookie
getCookie(String name)
Cookie[]
getCookies()
long
getDateHeader(String name)
String
getErrorMessage()
String
getForwardedUrl()
String
getHeader(String name)
Return the primary value for the given header as a String, if any.Collection<String>
getHeaderNames()
Return the names of all specified headers as a Set of Strings.List<String>
getHeaders(String name)
Return all values for the given header as a List of Strings.Object
getHeaderValue(String name)
Return the primary value for the given header, if any.List<Object>
getHeaderValues(String name)
Return all values for the given header as a List of value objects.String
getIncludedUrl()
List<String>
getIncludedUrls()
Locale
getLocale()
ServletOutputStream
getOutputStream()
String
getRedirectedUrl()
int
getStatus()
PrintWriter
getWriter()
boolean
isCharset()
Return whether the character encoding has been set.boolean
isCommitted()
boolean
isOutputStreamAccessAllowed()
Return whethergetOutputStream()
access is allowed.boolean
isWriterAccessAllowed()
Return whethergetOutputStream()
access is allowed.void
reset()
void
resetBuffer()
void
sendError(int status)
void
sendError(int status, String errorMessage)
void
sendRedirect(String url)
void
setBufferSize(int bufferSize)
void
setCharacterEncoding(String characterEncoding)
void
setCommitted(boolean committed)
void
setContentLength(int contentLength)
void
setContentLengthLong(long contentLength)
void
setContentType(String contentType)
void
setDateHeader(String name, long value)
void
setForwardedUrl(String forwardedUrl)
void
setHeader(String name, String value)
void
setIncludedUrl(String includedUrl)
void
setIntHeader(String name, int value)
void
setLocale(Locale locale)
void
setOutputStreamAccessAllowed(boolean outputStreamAccessAllowed)
Set whethergetOutputStream()
access is allowed.void
setStatus(int status)
void
setStatus(int status, String errorMessage)
Deprecated.void
setWriterAccessAllowed(boolean writerAccessAllowed)
Set whethergetWriter()
access is allowed.
Constructor Detail
MockHttpServletResponse
public MockHttpServletResponse()
Method Detail
setOutputStreamAccessAllowed
public void setOutputStreamAccessAllowed(boolean outputStreamAccessAllowed)
Set whethergetOutputStream()
access is allowed.Default is
true
.
isOutputStreamAccessAllowed
public boolean isOutputStreamAccessAllowed()
Return whethergetOutputStream()
access is allowed.
setWriterAccessAllowed
public void setWriterAccessAllowed(boolean writerAccessAllowed)
Set whethergetWriter()
access is allowed.Default is
true
.
isWriterAccessAllowed
public boolean isWriterAccessAllowed()
Return whethergetOutputStream()
access is allowed.
isCharset
public boolean isCharset()
Return whether the character encoding has been set.If
false
,getCharacterEncoding()
will return a default encoding value.
setCharacterEncoding
public void setCharacterEncoding(String characterEncoding)
- Specified by:
setCharacterEncoding
in interfaceServletResponse
getCharacterEncoding
public String getCharacterEncoding()
- Specified by:
getCharacterEncoding
in interfaceServletResponse
getOutputStream
public ServletOutputStream getOutputStream()
- Specified by:
getOutputStream
in interfaceServletResponse
getWriter
public PrintWriter getWriter() throws UnsupportedEncodingException
- Specified by:
getWriter
in interfaceServletResponse
- Throws:
UnsupportedEncodingException
getContentAsByteArray
public byte[] getContentAsByteArray()
getContentAsString
public String getContentAsString() throws UnsupportedEncodingException
- Throws:
UnsupportedEncodingException
setContentLength
public void setContentLength(int contentLength)
- Specified by:
setContentLength
in interfaceServletResponse
getContentLength
public int getContentLength()
setContentLengthLong
public void setContentLengthLong(long contentLength)
- Specified by:
setContentLengthLong
in interfaceServletResponse
getContentLengthLong
public long getContentLengthLong()
setContentType
public void setContentType(String contentType)
- Specified by:
setContentType
in interfaceServletResponse
getContentType
public String getContentType()
- Specified by:
getContentType
in interfaceServletResponse
setBufferSize
public void setBufferSize(int bufferSize)
- Specified by:
setBufferSize
in interfaceServletResponse
getBufferSize
public int getBufferSize()
- Specified by:
getBufferSize
in interfaceServletResponse
flushBuffer
public void flushBuffer()
- Specified by:
flushBuffer
in interfaceServletResponse
resetBuffer
public void resetBuffer()
- Specified by:
resetBuffer
in interfaceServletResponse
setCommitted
public void setCommitted(boolean committed)
isCommitted
public boolean isCommitted()
- Specified by:
isCommitted
in interfaceServletResponse
reset
public void reset()
- Specified by:
reset
in interfaceServletResponse
setLocale
public void setLocale(Locale locale)
- Specified by:
setLocale
in interfaceServletResponse
getLocale
public Locale getLocale()
- Specified by:
getLocale
in interfaceServletResponse
addCookie
public void addCookie(Cookie cookie)
- Specified by:
addCookie
in interfaceHttpServletResponse
getCookies
public Cookie[] getCookies()
containsHeader
public boolean containsHeader(String name)
- Specified by:
containsHeader
in interfaceHttpServletResponse
getHeaderNames
public Collection<String> getHeaderNames()
Return the names of all specified headers as a Set of Strings.As of Servlet 3.0, this method is also defined in
HttpServletResponse
.- Specified by:
getHeaderNames
in interfaceHttpServletResponse
- Returns:
- the
Set
of header nameStrings
, or an emptySet
if none
getHeader
public String getHeader(String name)
Return the primary value for the given header as a String, if any. Will return the first value in case of multiple values.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 usinggetHeaderValue(String)
for raw Object access.- Specified by:
getHeader
in interfaceHttpServletResponse
- Parameters:
name
- the name of the header- Returns:
- the associated header value, or
null
if none
getHeaders
public List<String> getHeaders(String name)
Return all values for the given header as a List of Strings.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 usinggetHeaderValues(String)
for raw Object access.- Specified by:
getHeaders
in interfaceHttpServletResponse
- Parameters:
name
- the name of the header- Returns:
- the associated header values, or an empty List if none
getHeaderValue
public Object getHeaderValue(String name)
Return the primary value for the given header, if any.Will return the first value in case of multiple values.
- Parameters:
name
- the name of the header- Returns:
- the associated header value, or
null
if none
getHeaderValues
public List<Object> getHeaderValues(String name)
Return all values for the given header as a List of value objects.- Parameters:
name
- the name of the header- Returns:
- the associated header values, or an empty List if none
encodeURL
public String encodeURL(String url)
The default implementation returns the given URL String as-is.Can be overridden in subclasses, appending a session id or the like.
- Specified by:
encodeURL
in interfaceHttpServletResponse
encodeRedirectURL
public String encodeRedirectURL(String url)
The default implementation delegates toencodeURL(java.lang.String)
, returning the given URL String as-is.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
encodeURL(java.lang.String)
method instead, applying to redirect URLs as well as to general URLs.- Specified by:
encodeRedirectURL
in interfaceHttpServletResponse
encodeUrl
@Deprecated public String encodeUrl(String url)
Deprecated.- Specified by:
encodeUrl
in interfaceHttpServletResponse
encodeRedirectUrl
@Deprecated public String encodeRedirectUrl(String url)
Deprecated.- Specified by:
encodeRedirectUrl
in interfaceHttpServletResponse
sendError
public void sendError(int status, String errorMessage) throws IOException
- Specified by:
sendError
in interfaceHttpServletResponse
- Throws:
IOException
sendError
public void sendError(int status) throws IOException
- Specified by:
sendError
in interfaceHttpServletResponse
- Throws:
IOException
sendRedirect
public void sendRedirect(String url) throws IOException
- Specified by:
sendRedirect
in interfaceHttpServletResponse
- Throws:
IOException
getRedirectedUrl
public String getRedirectedUrl()
setDateHeader
public void setDateHeader(String name, long value)
- Specified by:
setDateHeader
in interfaceHttpServletResponse
addDateHeader
public void addDateHeader(String name, long value)
- Specified by:
addDateHeader
in interfaceHttpServletResponse
getDateHeader
public long getDateHeader(String name)
setHeader
public void setHeader(String name, String value)
- Specified by:
setHeader
in interfaceHttpServletResponse
addHeader
public void addHeader(String name, String value)
- Specified by:
addHeader
in interfaceHttpServletResponse
setIntHeader
public void setIntHeader(String name, int value)
- Specified by:
setIntHeader
in interfaceHttpServletResponse
addIntHeader
public void addIntHeader(String name, int value)
- Specified by:
addIntHeader
in interfaceHttpServletResponse
setStatus
public void setStatus(int status)
- Specified by:
setStatus
in interfaceHttpServletResponse
setStatus
@Deprecated public void setStatus(int status, String errorMessage)
Deprecated.- Specified by:
setStatus
in interfaceHttpServletResponse
getStatus
public int getStatus()
- Specified by:
getStatus
in interfaceHttpServletResponse
getErrorMessage
public String getErrorMessage()
setForwardedUrl
public void setForwardedUrl(String forwardedUrl)
getForwardedUrl
public String getForwardedUrl()
setIncludedUrl
public void setIncludedUrl(String includedUrl)
getIncludedUrl
public String getIncludedUrl()
addIncludedUrl
public void addIncludedUrl(String includedUrl)
getIncludedUrls
public List<String> getIncludedUrls()