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 theHttpServletResponseinterface.As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline. Beyond that,
MockHttpServletResponseis 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 voidaddCookie(Cookie cookie)voidaddDateHeader(String name, long value)voidaddHeader(String name, String value)voidaddIncludedUrl(String includedUrl)voidaddIntHeader(String name, int value)booleancontainsHeader(String name)StringencodeRedirectUrl(String url)Deprecated.StringencodeRedirectURL(String url)The default implementation delegates toencodeURL(java.lang.String), returning the given URL String as-is.StringencodeUrl(String url)Deprecated.StringencodeURL(String url)The default implementation returns the given URL String as-is.voidflushBuffer()intgetBufferSize()StringgetCharacterEncoding()byte[]getContentAsByteArray()StringgetContentAsString()intgetContentLength()longgetContentLengthLong()StringgetContentType()CookiegetCookie(String name)Cookie[]getCookies()longgetDateHeader(String name)StringgetErrorMessage()StringgetForwardedUrl()StringgetHeader(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.ObjectgetHeaderValue(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.StringgetIncludedUrl()List<String>getIncludedUrls()LocalegetLocale()ServletOutputStreamgetOutputStream()StringgetRedirectedUrl()intgetStatus()PrintWritergetWriter()booleanisCharset()Return whether the character encoding has been set.booleanisCommitted()booleanisOutputStreamAccessAllowed()Return whethergetOutputStream()access is allowed.booleanisWriterAccessAllowed()Return whethergetOutputStream()access is allowed.voidreset()voidresetBuffer()voidsendError(int status)voidsendError(int status, String errorMessage)voidsendRedirect(String url)voidsetBufferSize(int bufferSize)voidsetCharacterEncoding(String characterEncoding)voidsetCommitted(boolean committed)voidsetContentLength(int contentLength)voidsetContentLengthLong(long contentLength)voidsetContentType(String contentType)voidsetDateHeader(String name, long value)voidsetForwardedUrl(String forwardedUrl)voidsetHeader(String name, String value)voidsetIncludedUrl(String includedUrl)voidsetIntHeader(String name, int value)voidsetLocale(Locale locale)voidsetOutputStreamAccessAllowed(boolean outputStreamAccessAllowed)Set whethergetOutputStream()access is allowed.voidsetStatus(int status)voidsetStatus(int status, String errorMessage)Deprecated.voidsetWriterAccessAllowed(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:
setCharacterEncodingin interfaceServletResponse
getCharacterEncoding
public String getCharacterEncoding()
- Specified by:
getCharacterEncodingin interfaceServletResponse
getOutputStream
public ServletOutputStream getOutputStream()
- Specified by:
getOutputStreamin interfaceServletResponse
getWriter
public PrintWriter getWriter() throws UnsupportedEncodingException
- Specified by:
getWriterin interfaceServletResponse- Throws:
UnsupportedEncodingException
getContentAsByteArray
public byte[] getContentAsByteArray()
getContentAsString
public String getContentAsString() throws UnsupportedEncodingException
- Throws:
UnsupportedEncodingException
setContentLength
public void setContentLength(int contentLength)
- Specified by:
setContentLengthin interfaceServletResponse
getContentLength
public int getContentLength()
setContentLengthLong
public void setContentLengthLong(long contentLength)
- Specified by:
setContentLengthLongin interfaceServletResponse
getContentLengthLong
public long getContentLengthLong()
setContentType
public void setContentType(String contentType)
- Specified by:
setContentTypein interfaceServletResponse
getContentType
public String getContentType()
- Specified by:
getContentTypein interfaceServletResponse
setBufferSize
public void setBufferSize(int bufferSize)
- Specified by:
setBufferSizein interfaceServletResponse
getBufferSize
public int getBufferSize()
- Specified by:
getBufferSizein interfaceServletResponse
flushBuffer
public void flushBuffer()
- Specified by:
flushBufferin interfaceServletResponse
resetBuffer
public void resetBuffer()
- Specified by:
resetBufferin interfaceServletResponse
setCommitted
public void setCommitted(boolean committed)
isCommitted
public boolean isCommitted()
- Specified by:
isCommittedin interfaceServletResponse
reset
public void reset()
- Specified by:
resetin interfaceServletResponse
setLocale
public void setLocale(Locale locale)
- Specified by:
setLocalein interfaceServletResponse
getLocale
public Locale getLocale()
- Specified by:
getLocalein interfaceServletResponse
addCookie
public void addCookie(Cookie cookie)
- Specified by:
addCookiein interfaceHttpServletResponse
getCookies
public Cookie[] getCookies()
containsHeader
public boolean containsHeader(String name)
- Specified by:
containsHeaderin 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:
getHeaderNamesin interfaceHttpServletResponse- Returns:
- the
Setof header nameStrings, or an emptySetif 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:
getHeaderin interfaceHttpServletResponse- Parameters:
name- the name of the header- Returns:
- the associated header value, or
nullif 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:
getHeadersin 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
nullif 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:
encodeURLin 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:
encodeRedirectURLin interfaceHttpServletResponse
encodeUrl
@Deprecated public String encodeUrl(String url)
Deprecated.- Specified by:
encodeUrlin interfaceHttpServletResponse
encodeRedirectUrl
@Deprecated public String encodeRedirectUrl(String url)
Deprecated.- Specified by:
encodeRedirectUrlin interfaceHttpServletResponse
sendError
public void sendError(int status, String errorMessage) throws IOException
- Specified by:
sendErrorin interfaceHttpServletResponse- Throws:
IOException
sendError
public void sendError(int status) throws IOException
- Specified by:
sendErrorin interfaceHttpServletResponse- Throws:
IOException
sendRedirect
public void sendRedirect(String url) throws IOException
- Specified by:
sendRedirectin interfaceHttpServletResponse- Throws:
IOException
getRedirectedUrl
public String getRedirectedUrl()
setDateHeader
public void setDateHeader(String name, long value)
- Specified by:
setDateHeaderin interfaceHttpServletResponse
addDateHeader
public void addDateHeader(String name, long value)
- Specified by:
addDateHeaderin interfaceHttpServletResponse
getDateHeader
public long getDateHeader(String name)
setHeader
public void setHeader(String name, String value)
- Specified by:
setHeaderin interfaceHttpServletResponse
addHeader
public void addHeader(String name, String value)
- Specified by:
addHeaderin interfaceHttpServletResponse
setIntHeader
public void setIntHeader(String name, int value)
- Specified by:
setIntHeaderin interfaceHttpServletResponse
addIntHeader
public void addIntHeader(String name, int value)
- Specified by:
addIntHeaderin interfaceHttpServletResponse
setStatus
public void setStatus(int status)
- Specified by:
setStatusin interfaceHttpServletResponse
setStatus
@Deprecated public void setStatus(int status, String errorMessage)
Deprecated.- Specified by:
setStatusin interfaceHttpServletResponse
getStatus
public int getStatus()
- Specified by:
getStatusin 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()