Package com.avoka.tm.test
Class MockResponse
java.lang.Object
com.avoka.tm.test.MockResponse
- All Implemented Interfaces:
- jakarta.servlet.http.HttpServletResponse,- jakarta.servlet.ServletResponse
 Mock implementation of HttpServletResponse.
 
Implements all of the methods from the standard HttpServletResponse class plus helper methods to aid viewing the generated response.
This class was adapted from Apache Wicket.
- Since:
- 17.10.0
- 
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 TypeMethodDescriptionvoidaddCookie(jakarta.servlet.http.Cookie cookie) Add a cookie to the response.voidaddDateHeader(String name, long l) Add a date header.voidAdd the given header value, including an additional entry if one already exists.voidaddIntHeader(String name, int i) Add an int header value.static voidappend2digits(StringBuffer buf, int i) Append two digits if specified int is less than 100.booleancontainsHeader(String name) Check if the response contains the given header name.encodeRedirectUrl(String url) Encode the redirectUrl.encodeRedirectURL(String url) Encode the redirectUrl.Encode thr URL.Encode thr URL.voidFlush the buffer.static StringformatDate(long value) Formats the specified long value as a date.static voidformatDate(StringBuffer buf, Calendar calendar, boolean cookie) Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'" or "EEE, dd-MMM-yy HH:mm:ss 'GMT'" for cookies.byte[]Get the binary content that was written to the servlet stream.intReturn the current buffer size.Get the character encoding of the response.intgetCode()Get the response code for this request.Return the response content type.Collection<jakarta.servlet.http.Cookie>Get all of the cookies that have been added to the response.Get the text document that was written as part of this response.Get the error message.Return the value of the given named header.Get the names of all of the headers.getHeaders(String name) Return the request headers.Get the encoded locale.jakarta.servlet.ServletOutputStreamGet the output stream for writing binary data from the servlet.Get the location that was redirected to.intGet the status code.Get the print writer for writing text output for this response.voidReset the response ready for reuse.booleanAlways returns false.booleanisError()Return whether the servlet returned an error code or not.booleanCheck whether the response was redirected or not.voidreset()Delegate to initialise method.voidClears the buffer.voidsendError(int code) Send an error code.voidSend an error code.voidsendRedirect(String url) Indicate sending of a redirectUrl to a particular named resource.voidsetBufferSize(int size) Method ignored.voidsetCharacterEncoding(String characterEncoding) Set the character encoding.voidsetContentLength(int length) Set the content length.voidsetContentLengthLong(long len) Set the content length long.voidsetContentType(String type) Set the content type.voidsetDateHeader(String name, long l) Set a date header.voidSet the given header value.voidsetIntHeader(String name, int i) Set an int header value.voidSet the locale in the response header.voidsetStatus(int status) Set the status for this response.voidDeprecated.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- 
MockResponsepublic MockResponse()Create the response object.
 
- 
- 
Method Details- 
addCookiepublic void addCookie(jakarta.servlet.http.Cookie cookie) Add a cookie to the response.- Specified by:
- addCookiein interface- jakarta.servlet.http.HttpServletResponse
- Parameters:
- cookie- The cookie to add
 
- 
addDateHeaderAdd a date header.- Specified by:
- addDateHeaderin interface- jakarta.servlet.http.HttpServletResponse
- Parameters:
- name- The header value
- l- The long value
 
- 
addHeaderAdd the given header value, including an additional entry if one already exists.- Specified by:
- addHeaderin interface- jakarta.servlet.http.HttpServletResponse
- Parameters:
- name- The name for the header
- value- The value for the header
 
- 
addIntHeaderAdd an int header value.- Specified by:
- addIntHeaderin interface- jakarta.servlet.http.HttpServletResponse
- Parameters:
- name- The header name
- i- The value
 
- 
containsHeaderCheck if the response contains the given header name.- Specified by:
- containsHeaderin interface- jakarta.servlet.http.HttpServletResponse
- Parameters:
- name- The name to check
- Returns:
- Whether header in response or not
 
- 
encodeRedirectUrlEncode the redirectUrl. Does no changes as this test implementation uses cookie based url tracking.- Parameters:
- url- The url to encode
- Returns:
- The encoded url
 
- 
encodeRedirectURLEncode the redirectUrl. Does no changes as this test implementation uses cookie based url tracking.- Specified by:
- encodeRedirectURLin interface- jakarta.servlet.http.HttpServletResponse
- Parameters:
- url- The url to encode
- Returns:
- The encoded url
 
- 
encodeUrlEncode thr URL. Does no changes as this test implementation uses cookie based url tracking.- Parameters:
- url- The url to encode
- Returns:
- The encoded url
 
- 
encodeURLEncode thr URL. Does no changes as this test implementation uses cookie based url tracking.- Specified by:
- encodeURLin interface- jakarta.servlet.http.HttpServletResponse
- Parameters:
- url- The url to encode
- Returns:
- The encoded url
 
- 
flushBufferFlush the buffer.- Specified by:
- flushBufferin interface- jakarta.servlet.ServletResponse
- Throws:
- IOException- if exception occurs during flush
 
- 
getBinaryContentpublic byte[] getBinaryContent()Get the binary content that was written to the servlet stream.- Returns:
- The binary content
 
- 
getBufferSizepublic int getBufferSize()Return the current buffer size.- Specified by:
- getBufferSizein interface- jakarta.servlet.ServletResponse
- Returns:
- The buffer size
 
- 
getCharacterEncodingGet the character encoding of the response.- Specified by:
- getCharacterEncodingin interface- jakarta.servlet.ServletResponse
- Returns:
- The character encoding
 
- 
getCodepublic int getCode()Get the response code for this request.- Returns:
- The response code
 
- 
getCookiesGet all of the cookies that have been added to the response.- Returns:
- The collection of cookies
 
- 
getDocumentGet the text document that was written as part of this response.- Returns:
- The document
 
- 
getErrorMessageGet the error message.- Returns:
- The error message, or null if no message
 
- 
getHeaderReturn the value of the given named header.- Specified by:
- getHeaderin interface- jakarta.servlet.http.HttpServletResponse
- Parameters:
- name- The header name
- Returns:
- The value, or null
 
- 
getHeaderNamesGet the names of all of the headers.- Specified by:
- getHeaderNamesin interface- jakarta.servlet.http.HttpServletResponse
- Returns:
- The header names
 
- 
getLocaleGet the encoded locale.- Specified by:
- getLocalein interface- jakarta.servlet.ServletResponse
- Returns:
- The locale
 
- 
getOutputStreampublic jakarta.servlet.ServletOutputStream getOutputStream()Get the output stream for writing binary data from the servlet.- Specified by:
- getOutputStreamin interface- jakarta.servlet.ServletResponse
- Returns:
- The binary output stream.
 
- 
getRedirectUrlGet the location that was redirected to.- Returns:
- The redirect url, or null if not a redirect
 
- 
getStatuspublic int getStatus()Get the status code.- Specified by:
- getStatusin interface- jakarta.servlet.http.HttpServletResponse
- Returns:
- The status code
 
- 
getWriterGet the print writer for writing text output for this response.- Specified by:
- getWriterin interface- jakarta.servlet.ServletResponse
- Returns:
- The writer
- Throws:
- IOException- - if an input or output exception occurred
 
- 
initializepublic void initialize()Reset the response ready for reuse.
- 
resetpublic void reset()Delegate to initialise method.- Specified by:
- resetin interface- jakarta.servlet.ServletResponse
 
- 
isCommittedpublic boolean isCommitted()Always returns false.- Specified by:
- isCommittedin interface- jakarta.servlet.ServletResponse
- Returns:
- Always false
 
- 
isErrorpublic boolean isError()Return whether the servlet returned an error code or not.- Returns:
- Whether an error occurred or not
 
- 
isRedirectpublic boolean isRedirect()Check whether the response was redirected or not.- Returns:
- Whether the state was redirected or not
 
- 
resetBufferpublic void resetBuffer()Clears the buffer.- Specified by:
- resetBufferin interface- jakarta.servlet.ServletResponse
 
- 
sendErrorSend an error code. This implementation just sets the internal error state information.- Specified by:
- sendErrorin interface- jakarta.servlet.http.HttpServletResponse
- Parameters:
- code- The code
- Throws:
- IOException- Not used
 
- 
sendErrorSend an error code. This implementation just sets the internal error state information.- Specified by:
- sendErrorin interface- jakarta.servlet.http.HttpServletResponse
- Parameters:
- code- The error code
- msg- The error message
- Throws:
- IOException- Not used
 
- 
sendRedirectIndicate sending of a redirectUrl to a particular named resource. This implementation just keeps hold of the redirectUrl info and makes it available for querying. - Specified by:
- sendRedirectin interface- jakarta.servlet.http.HttpServletResponse
- Parameters:
- url- The url to set redirectUrl to
- Throws:
- IOException- Not used
 
- 
setBufferSizepublic void setBufferSize(int size) Method ignored.- Specified by:
- setBufferSizein interface- jakarta.servlet.ServletResponse
- Parameters:
- size- The size
 
- 
setCharacterEncodingSet the character encoding.- Specified by:
- setCharacterEncodingin interface- jakarta.servlet.ServletResponse
- Parameters:
- characterEncoding- The character encoding
 
- 
setContentLengthpublic void setContentLength(int length) Set the content length.- Specified by:
- setContentLengthin interface- jakarta.servlet.ServletResponse
- Parameters:
- length- The length
 
- 
setContentTypeSet the content type.- Specified by:
- setContentTypein interface- jakarta.servlet.ServletResponse
- Parameters:
- type- The content type
 
- 
getContentTypeReturn the response content type.- Specified by:
- getContentTypein interface- jakarta.servlet.ServletResponse
- Returns:
- the response content type
 
- 
setDateHeaderSet a date header.- Specified by:
- setDateHeaderin interface- jakarta.servlet.http.HttpServletResponse
- Parameters:
- name- The header name
- l- The long value
 
- 
formatDateFormats the specified long value as a date.- Parameters:
- value- the specified long value to format
- Returns:
- the formatted date
 
- 
formatDateFormat HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'" or "EEE, dd-MMM-yy HH:mm:ss 'GMT'" for cookies.- Parameters:
- buf- the buffer to render to formatted date to
- calendar- the date to format
- cookie- should date be formatted for usage in a cookie
 
- 
append2digitsAppend two digits if specified int is less than 100.- Parameters:
- buf- the buffer to add 2 digits to
- i- the digits to add
 
- 
setHeaderSet the given header value.- Specified by:
- setHeaderin interface- jakarta.servlet.http.HttpServletResponse
- Parameters:
- name- The name for the header
- value- The value for the header
 
- 
setIntHeaderSet an int header value.- Specified by:
- setIntHeaderin interface- jakarta.servlet.http.HttpServletResponse
- Parameters:
- name- The header name
- i- The value
 
- 
setLocaleSet the locale in the response header.- Specified by:
- setLocalein interface- jakarta.servlet.ServletResponse
- Parameters:
- locale- The locale
 
- 
setStatuspublic void setStatus(int status) Set the status for this response.- Specified by:
- setStatusin interface- jakarta.servlet.http.HttpServletResponse
- Parameters:
- status- The status
 
- 
setStatusDeprecated.Set the status for this response.- Parameters:
- status- The status
- msg- The message
 
- 
setContentLengthLongpublic void setContentLengthLong(long len) Set the content length long.- Specified by:
- setContentLengthLongin interface- jakarta.servlet.ServletResponse
- Parameters:
- len- the content length
 
- 
getHeadersReturn the request headers.- Specified by:
- getHeadersin interface- jakarta.servlet.http.HttpServletResponse
- Parameters:
- name- the header name
- Returns:
- headers
 
 
-