Class MockHttpServletResponse
- All Implemented Interfaces:
- HttpServletResponse,- ServletResponse
HttpServletResponse interface.
 As of Spring 6.0, this set of mocks is designed on a Servlet 6.0 baseline.
- Since:
- 1.0.2
- Author:
- Juergen Hoeller, Rod Johnson, Brian Clozel, Vedran Pavic, Sebastien Deleuze, Sam Brannen
- 
Field SummaryFields inherited from interface jakarta.servlet.http.HttpServletResponseSC_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 SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidvoidaddDateHeader(String name, long value) voidvoidaddIncludedUrl(String includedUrl) voidaddIntHeader(String name, int value) booleancontainsHeader(String name) encodeRedirectURL(String url) The default implementation delegates toencodeURL(java.lang.String), returning the given URL String as-is.The default implementation returns the given URL String as-is.voidintbyte[]Get the content of the response body as aString, using the charset specified for the response by the application, either throughHttpServletResponsemethods or through a charset parameter on theContent-Type.getContentAsString(Charset fallbackCharset) Get the content of the response body as aString, using the providedfallbackCharsetif no charset has been explicitly defined and otherwise using the charset specified for the response by the application, either throughHttpServletResponsemethods or through a charset parameter on theContent-Type.intGet the length of the content body from the HTTP Content-Length header.longCookie[]longgetDateHeader(String name) Return the error message used when callingHttpServletResponse.sendError(int, String).Return the primary value for the given header as a String, if any.Return the names of all specified headers as a Set of Strings.getHeaders(String name) Return all values for the given header as a List of Strings.getHeaderValue(String name) Return the primary value for the given header, if any.getHeaderValues(String name) Return all values for the given header as a List of value objects.intbooleanDetermine whether the character encoding has been explicitly set throughHttpServletResponsemethods or through acharsetparameter on theContent-Type.booleanbooleanReturn whethergetOutputStream()access is allowed.booleanReturn whethergetOutputStream()access is allowed.voidreset()voidvoidsendError(int status) voidvoidsendRedirect(String url) voidsendRedirect(String url, int sc, boolean clearBuffer) voidsetBufferSize(int bufferSize) voidsetCharacterEncoding(String characterEncoding) voidsetCommitted(boolean committed) voidsetContentLength(int contentLength) voidsetContentLengthLong(long contentLength) voidsetContentType(String contentType) voidsetDateHeader(String name, long value) voidsetDefaultCharacterEncoding(String characterEncoding) Set the default character encoding for the response.voidsetForwardedUrl(String forwardedUrl) voidvoidsetIncludedUrl(String includedUrl) voidsetIntHeader(String name, int value) voidvoidsetOutputStreamAccessAllowed(boolean outputStreamAccessAllowed) Set whethergetOutputStream()access is allowed.voidsetStatus(int status) voidsetWriterAccessAllowed(boolean writerAccessAllowed) Set whethergetWriter()access is allowed.Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface jakarta.servlet.http.HttpServletResponsegetTrailerFields, setTrailerFields
- 
Constructor Details- 
MockHttpServletResponsepublic MockHttpServletResponse()
 
- 
- 
Method Details- 
setOutputStreamAccessAllowedpublic void setOutputStreamAccessAllowed(boolean outputStreamAccessAllowed) Set whethergetOutputStream()access is allowed.Default is true.
- 
isOutputStreamAccessAllowedpublic boolean isOutputStreamAccessAllowed()Return whethergetOutputStream()access is allowed.
- 
setWriterAccessAllowedpublic void setWriterAccessAllowed(boolean writerAccessAllowed) Set whethergetWriter()access is allowed.Default is true.
- 
isWriterAccessAllowedpublic boolean isWriterAccessAllowed()Return whethergetOutputStream()access is allowed.
- 
setDefaultCharacterEncodingSet the default character encoding for the response.If this method is not invoked, ISO-8859-1will be used as the default character encoding.If the character encoding for the response has not already been explicitly set via setCharacterEncoding(String)orsetContentType(String), the character encoding for the response will be set to the supplied default character encoding.- Parameters:
- characterEncoding- the default character encoding
- Since:
- 5.3.10
- See Also:
 
- 
isCharsetpublic boolean isCharset()Determine whether the character encoding has been explicitly set throughHttpServletResponsemethods or through acharsetparameter on theContent-Type.If false,getCharacterEncoding()will return the default character encoding.
- 
setCharacterEncoding- Specified by:
- setCharacterEncodingin interface- ServletResponse
 
- 
getCharacterEncoding- Specified by:
- getCharacterEncodingin interface- ServletResponse
 
- 
getOutputStream- Specified by:
- getOutputStreamin interface- ServletResponse
 
- 
getWriter- Specified by:
- getWriterin interface- ServletResponse
- Throws:
- UnsupportedEncodingException
 
- 
getContentAsByteArraypublic byte[] getContentAsByteArray()
- 
getContentAsStringGet the content of the response body as aString, using the charset specified for the response by the application, either throughHttpServletResponsemethods or through a charset parameter on theContent-Type. If no charset has been explicitly defined, the default character encoding will be used.- Returns:
- the content as a String
- Throws:
- UnsupportedEncodingException- if the character encoding is not supported
- See Also:
 
- 
getContentAsStringGet the content of the response body as aString, using the providedfallbackCharsetif no charset has been explicitly defined and otherwise using the charset specified for the response by the application, either throughHttpServletResponsemethods or through a charset parameter on theContent-Type.- Returns:
- the content as a String
- Throws:
- UnsupportedEncodingException- if the character encoding is not supported
- Since:
- 5.2
- See Also:
 
- 
setContentLengthpublic void setContentLength(int contentLength) - Specified by:
- setContentLengthin interface- ServletResponse
 
- 
getContentLengthpublic int getContentLength()Get the length of the content body from the HTTP Content-Length header.- Returns:
- the value of the Content-Length header
- See Also:
 
- 
setContentLengthLongpublic void setContentLengthLong(long contentLength) - Specified by:
- setContentLengthLongin interface- ServletResponse
 
- 
getContentLengthLongpublic long getContentLengthLong()
- 
setContentType- Specified by:
- setContentTypein interface- ServletResponse
 
- 
getContentType- Specified by:
- getContentTypein interface- ServletResponse
 
- 
setBufferSizepublic void setBufferSize(int bufferSize) - Specified by:
- setBufferSizein interface- ServletResponse
 
- 
getBufferSizepublic int getBufferSize()- Specified by:
- getBufferSizein interface- ServletResponse
 
- 
flushBufferpublic void flushBuffer()- Specified by:
- flushBufferin interface- ServletResponse
 
- 
resetBufferpublic void resetBuffer()- Specified by:
- resetBufferin interface- ServletResponse
 
- 
setCommittedpublic void setCommitted(boolean committed) 
- 
isCommittedpublic boolean isCommitted()- Specified by:
- isCommittedin interface- ServletResponse
 
- 
resetpublic void reset()- Specified by:
- resetin interface- ServletResponse
 
- 
setLocale- Specified by:
- setLocalein interface- ServletResponse
 
- 
getLocale- Specified by:
- getLocalein interface- ServletResponse
 
- 
addCookie- Specified by:
- addCookiein interface- HttpServletResponse
 
- 
getCookies
- 
getCookie
- 
containsHeader- Specified by:
- containsHeaderin interface- HttpServletResponse
 
- 
getHeaderNamesReturn 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 interface- HttpServletResponse
- Returns:
- the Setof header nameStrings, or an emptySetif none
 
- 
getHeaderReturn the primary value for the given header as a String, if any.Will return the first value in case of multiple values. Returns a stringified value for Servlet 3.0 compatibility. Consider using getHeaderValue(String)for raw Object access.- Specified by:
- getHeaderin interface- HttpServletResponse
- Parameters:
- name- the name of the header
- Returns:
- the associated header value, or nullif none
- See Also:
 
- 
getHeadersReturn all values for the given header as a List of Strings.Returns a List of stringified values for Servlet 3.0 compatibility. Consider using getHeaderValues(String)for raw Object access.- Specified by:
- getHeadersin interface- HttpServletResponse
- Parameters:
- name- the name of the header
- Returns:
- the associated header values, or an empty List if none
- See Also:
 
- 
getHeaderValueReturn 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
 
- 
getHeaderValuesReturn 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
 
- 
encodeURLThe default implementation returns the given URL String as-is.Can be overridden in subclasses, appending a session id or the like. - Specified by:
- encodeURLin interface- HttpServletResponse
 
- 
encodeRedirectURLThe 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 interface- HttpServletResponse
 
- 
sendError- Specified by:
- sendErrorin interface- HttpServletResponse
- Throws:
- IOException
 
- 
sendError- Specified by:
- sendErrorin interface- HttpServletResponse
- Throws:
- IOException
 
- 
sendRedirect- Specified by:
- sendRedirectin interface- HttpServletResponse
- Throws:
- IOException
 
- 
sendRedirect- Throws:
- IOException
 
- 
getRedirectedUrl
- 
setDateHeader- Specified by:
- setDateHeaderin interface- HttpServletResponse
 
- 
addDateHeader- Specified by:
- addDateHeaderin interface- HttpServletResponse
 
- 
getDateHeader
- 
setHeader- Specified by:
- setHeaderin interface- HttpServletResponse
 
- 
addHeader- Specified by:
- addHeaderin interface- HttpServletResponse
 
- 
setIntHeader- Specified by:
- setIntHeaderin interface- HttpServletResponse
 
- 
addIntHeader- Specified by:
- addIntHeaderin interface- HttpServletResponse
 
- 
setStatuspublic void setStatus(int status) - Specified by:
- setStatusin interface- HttpServletResponse
 
- 
getStatuspublic int getStatus()- Specified by:
- getStatusin interface- HttpServletResponse
 
- 
getErrorMessageReturn the error message used when callingHttpServletResponse.sendError(int, String).
- 
setForwardedUrl
- 
getForwardedUrl
- 
setIncludedUrl
- 
getIncludedUrl
- 
addIncludedUrl
- 
getIncludedUrls
 
-