Package com.avoka.tm.test
Class MockResponse
- java.lang.Object
-
- com.avoka.tm.test.MockResponse
-
- All Implemented Interfaces:
HttpServletResponse
,ServletResponse
public class MockResponse extends Object implements HttpServletResponse
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 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 MockResponse()
Create the response object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addCookie(Cookie cookie)
Add a cookie to the response.void
addDateHeader(String name, long l)
Add a date header.void
addHeader(String name, String value)
Add the given header value, including an additional entry if one already exists.void
addIntHeader(String name, int i)
Add an int header value.static void
append2digits(StringBuffer buf, int i)
Append two digits if specified int is less than 100.boolean
containsHeader(String name)
Check if the response contains the given header name.String
encodeRedirectUrl(String url)
Encode the redirectUrl.String
encodeRedirectURL(String url)
Encode the redirectUrl.String
encodeUrl(String url)
Encode thr URL.String
encodeURL(String url)
Encode thr URL.void
flushBuffer()
Flush the buffer.static String
formatDate(long value)
Formats the specified long value as a date.static void
formatDate(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[]
getBinaryContent()
Get the binary content that was written to the servlet stream.int
getBufferSize()
Return the current buffer size.String
getCharacterEncoding()
Get the character encoding of the response.int
getCode()
Get the response code for this request.String
getContentType()
Return the response content type.Collection<Cookie>
getCookies()
Get all of the cookies that have been added to the response.String
getDocument()
Get the text document that was written as part of this response.String
getErrorMessage()
Get the error message.String
getHeader(String name)
Return the value of the given named header.Set<String>
getHeaderNames()
Get the names of all of the headers.Collection<String>
getHeaders(String name)
Return the request headers.Locale
getLocale()
Get the encoded locale.ServletOutputStream
getOutputStream()
Get the output stream for writing binary data from the servlet.String
getRedirectUrl()
Get the location that was redirected to.int
getStatus()
Get the status code.PrintWriter
getWriter()
Get the print writer for writing text output for this response.void
initialize()
Reset the response ready for reuse.boolean
isCommitted()
Always returns false.boolean
isError()
Return whether the servlet returned an error code or not.boolean
isRedirect()
Check whether the response was redirected or not.void
reset()
Delegate to initialise method.void
resetBuffer()
Clears the buffer.void
sendError(int code)
Send an error code.void
sendError(int code, String msg)
Send an error code.void
sendRedirect(String url)
Indicate sending of a redirectUrl to a particular named resource.void
setBufferSize(int size)
Method ignored.void
setCharacterEncoding(String characterEncoding)
Set the character encoding.void
setContentLength(int length)
Set the content length.void
setContentLengthLong(long len)
Set the content length long.void
setContentType(String type)
Set the content type.void
setDateHeader(String name, long l)
Set a date header.void
setHeader(String name, String value)
Set the given header value.void
setIntHeader(String name, int i)
Set an int header value.void
setLocale(Locale locale)
Set the locale in the response header.void
setStatus(int status)
Set the status for this response.void
setStatus(int status, String msg)
Deprecated.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface javax.servlet.http.HttpServletResponse
getTrailerFields, setTrailerFields
-
-
-
-
Method Detail
-
addCookie
public void addCookie(Cookie cookie)
Add a cookie to the response.- Specified by:
addCookie
in interfaceHttpServletResponse
- Parameters:
cookie
- The cookie to add
-
addDateHeader
public void addDateHeader(String name, long l)
Add a date header.- Specified by:
addDateHeader
in interfaceHttpServletResponse
- Parameters:
name
- The header valuel
- The long value
-
addHeader
public void addHeader(String name, String value)
Add the given header value, including an additional entry if one already exists.- Specified by:
addHeader
in interfaceHttpServletResponse
- Parameters:
name
- The name for the headervalue
- The value for the header
-
addIntHeader
public void addIntHeader(String name, int i)
Add an int header value.- Specified by:
addIntHeader
in interfaceHttpServletResponse
- Parameters:
name
- The header namei
- The value
-
containsHeader
public boolean containsHeader(String name)
Check if the response contains the given header name.- Specified by:
containsHeader
in interfaceHttpServletResponse
- Parameters:
name
- The name to check- Returns:
- Whether header in response or not
-
encodeRedirectUrl
public String encodeRedirectUrl(String url)
Encode the redirectUrl. Does no changes as this test implementation uses cookie based url tracking.- Specified by:
encodeRedirectUrl
in interfaceHttpServletResponse
- Parameters:
url
- The url to encode- Returns:
- The encoded url
-
encodeRedirectURL
public String encodeRedirectURL(String url)
Encode the redirectUrl. Does no changes as this test implementation uses cookie based url tracking.- Specified by:
encodeRedirectURL
in interfaceHttpServletResponse
- Parameters:
url
- The url to encode- Returns:
- The encoded url
-
encodeUrl
public String encodeUrl(String url)
Encode thr URL. Does no changes as this test implementation uses cookie based url tracking.- Specified by:
encodeUrl
in interfaceHttpServletResponse
- Parameters:
url
- The url to encode- Returns:
- The encoded url
-
encodeURL
public String encodeURL(String url)
Encode thr URL. Does no changes as this test implementation uses cookie based url tracking.- Specified by:
encodeURL
in interfaceHttpServletResponse
- Parameters:
url
- The url to encode- Returns:
- The encoded url
-
flushBuffer
public void flushBuffer() throws IOException
Flush the buffer.- Specified by:
flushBuffer
in interfaceServletResponse
- Throws:
IOException
- if exception occurs during flush
-
getBinaryContent
public byte[] getBinaryContent()
Get the binary content that was written to the servlet stream.- Returns:
- The binary content
-
getBufferSize
public int getBufferSize()
Return the current buffer size.- Specified by:
getBufferSize
in interfaceServletResponse
- Returns:
- The buffer size
-
getCharacterEncoding
public String getCharacterEncoding()
Get the character encoding of the response.- Specified by:
getCharacterEncoding
in interfaceServletResponse
- Returns:
- The character encoding
-
getCode
public int getCode()
Get the response code for this request.- Returns:
- The response code
-
getCookies
public Collection<Cookie> getCookies()
Get all of the cookies that have been added to the response.- Returns:
- The collection of cookies
-
getDocument
public String getDocument()
Get the text document that was written as part of this response.- Returns:
- The document
-
getErrorMessage
public String getErrorMessage()
Get the error message.- Returns:
- The error message, or null if no message
-
getHeader
public String getHeader(String name)
Return the value of the given named header.- Specified by:
getHeader
in interfaceHttpServletResponse
- Parameters:
name
- The header name- Returns:
- The value, or null
-
getHeaderNames
public Set<String> getHeaderNames()
Get the names of all of the headers.- Specified by:
getHeaderNames
in interfaceHttpServletResponse
- Returns:
- The header names
-
getLocale
public Locale getLocale()
Get the encoded locale.- Specified by:
getLocale
in interfaceServletResponse
- Returns:
- The locale
-
getOutputStream
public ServletOutputStream getOutputStream()
Get the output stream for writing binary data from the servlet.- Specified by:
getOutputStream
in interfaceServletResponse
- Returns:
- The binary output stream.
-
getRedirectUrl
public String getRedirectUrl()
Get the location that was redirected to.- Returns:
- The redirect url, or null if not a redirect
-
getStatus
public int getStatus()
Get the status code.- Specified by:
getStatus
in interfaceHttpServletResponse
- Returns:
- The status code
-
getWriter
public PrintWriter getWriter() throws IOException
Get the print writer for writing text output for this response.- Specified by:
getWriter
in interfaceServletResponse
- Returns:
- The writer
- Throws:
IOException
- - if an input or output exception occurred
-
initialize
public void initialize()
Reset the response ready for reuse.
-
reset
public void reset()
Delegate to initialise method.- Specified by:
reset
in interfaceServletResponse
-
isCommitted
public boolean isCommitted()
Always returns false.- Specified by:
isCommitted
in interfaceServletResponse
- Returns:
- Always false
-
isError
public boolean isError()
Return whether the servlet returned an error code or not.- Returns:
- Whether an error occurred or not
-
isRedirect
public boolean isRedirect()
Check whether the response was redirected or not.- Returns:
- Whether the state was redirected or not
-
resetBuffer
public void resetBuffer()
Clears the buffer.- Specified by:
resetBuffer
in interfaceServletResponse
-
sendError
public void sendError(int code) throws IOException
Send an error code. This implementation just sets the internal error state information.- Specified by:
sendError
in interfaceHttpServletResponse
- Parameters:
code
- The code- Throws:
IOException
- Not used
-
sendError
public void sendError(int code, String msg) throws IOException
Send an error code. This implementation just sets the internal error state information.- Specified by:
sendError
in interfaceHttpServletResponse
- Parameters:
code
- The error codemsg
- The error message- Throws:
IOException
- Not used
-
sendRedirect
public void sendRedirect(String url) throws IOException
Indicate 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:
sendRedirect
in interfaceHttpServletResponse
- Parameters:
url
- The url to set redirectUrl to- Throws:
IOException
- Not used
-
setBufferSize
public void setBufferSize(int size)
Method ignored.- Specified by:
setBufferSize
in interfaceServletResponse
- Parameters:
size
- The size
-
setCharacterEncoding
public void setCharacterEncoding(String characterEncoding)
Set the character encoding.- Specified by:
setCharacterEncoding
in interfaceServletResponse
- Parameters:
characterEncoding
- The character encoding
-
setContentLength
public void setContentLength(int length)
Set the content length.- Specified by:
setContentLength
in interfaceServletResponse
- Parameters:
length
- The length
-
setContentType
public void setContentType(String type)
Set the content type.- Specified by:
setContentType
in interfaceServletResponse
- Parameters:
type
- The content type
-
getContentType
public String getContentType()
Return the response content type.- Specified by:
getContentType
in interfaceServletResponse
- Returns:
- the response content type
-
setDateHeader
public void setDateHeader(String name, long l)
Set a date header.- Specified by:
setDateHeader
in interfaceHttpServletResponse
- Parameters:
name
- The header namel
- The long value
-
formatDate
public static String formatDate(long value)
Formats the specified long value as a date.- Parameters:
value
- the specified long value to format- Returns:
- the formatted date
-
formatDate
public static void formatDate(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.- Parameters:
buf
- the buffer to render to formatted date tocalendar
- the date to formatcookie
- should date be formatted for usage in a cookie
-
append2digits
public static void append2digits(StringBuffer buf, int i)
Append two digits if specified int is less than 100.- Parameters:
buf
- the buffer to add 2 digits toi
- the digits to add
-
setHeader
public void setHeader(String name, String value)
Set the given header value.- Specified by:
setHeader
in interfaceHttpServletResponse
- Parameters:
name
- The name for the headervalue
- The value for the header
-
setIntHeader
public void setIntHeader(String name, int i)
Set an int header value.- Specified by:
setIntHeader
in interfaceHttpServletResponse
- Parameters:
name
- The header namei
- The value
-
setLocale
public void setLocale(Locale locale)
Set the locale in the response header.- Specified by:
setLocale
in interfaceServletResponse
- Parameters:
locale
- The locale
-
setStatus
public void setStatus(int status)
Set the status for this response.- Specified by:
setStatus
in interfaceHttpServletResponse
- Parameters:
status
- The status
-
setStatus
public void setStatus(int status, String msg)
Deprecated.Set the status for this response.- Specified by:
setStatus
in interfaceHttpServletResponse
- Parameters:
status
- The statusmsg
- The message
-
setContentLengthLong
public void setContentLengthLong(long len)
Set the content length long.- Specified by:
setContentLengthLong
in interfaceServletResponse
- Parameters:
len
- the content length
-
getHeaders
public Collection<String> getHeaders(String name)
Return the request headers.- Specified by:
getHeaders
in interfaceHttpServletResponse
- Parameters:
name
- the header name- Returns:
- headers
-
-