Class MockHttpServletResponse
- java.lang.Object
-
- org.apache.wicket.protocol.http.mock.MockHttpServletResponse
-
- All Implemented Interfaces:
javax.servlet.http.HttpServletResponse,javax.servlet.ServletResponse,IMetaDataBufferingWebResponse
public class MockHttpServletResponse extends java.lang.Object implements javax.servlet.http.HttpServletResponse, IMetaDataBufferingWebResponse
Mock servlet response. Implements all of the methods from the standard HttpServletResponse class plus helper methods to aid viewing the generated response.- Author:
- Chris Turner
-
-
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(MockHttpServletRequest servletRequest)Create the response object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddCookie(javax.servlet.http.Cookie cookie)Add a cookie to the response.voidaddDateHeader(java.lang.String name, long l)Add a date header.voidaddHeader(java.lang.String name, java.lang.String value)Add the given header value, including an additional entry if one already exists.voidaddIntHeader(java.lang.String name, int i)Add an int header value.static voidappend2digits(java.lang.StringBuilder buf, int i)booleancontainsHeader(java.lang.String name)Check if the response contains the given header name.java.lang.StringencodeRedirectUrl(java.lang.String url)Encode the redirectLocation URL.java.lang.StringencodeRedirectURL(java.lang.String url)Encode the redirectLocation URL.java.lang.StringencodeUrl(java.lang.String url)Encode the URL.java.lang.StringencodeURL(java.lang.String url)Encode the URL.voidflushBuffer()Flush the buffer.static java.lang.StringformatDate(long l)static voidformatDate(java.lang.StringBuilder buf, java.util.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 cookiesbyte[]getBinaryContent()Get the binary content that was written to the servlet stream.java.lang.StringgetBinaryResponse()intgetBufferSize()Return the current buffer sizejava.lang.StringgetCharacterEncoding()Get the character encoding of the response.java.lang.StringgetContentType()java.util.List<javax.servlet.http.Cookie>getCookies()Get all of the cookies that have been added to the response.java.lang.StringgetDocument()Get the text document that was written as part of this response.java.lang.StringgetErrorMessage()Get the error message.java.lang.StringgetHeader(java.lang.String name)Return the value of the given named header.java.util.Set<java.lang.String>getHeaderNames()Get the names of all of the headers.java.util.Collection<java.lang.String>getHeaders(java.lang.String name)java.util.LocalegetLocale()Get the encoded localejavax.servlet.ServletOutputStreamgetOutputStream()Get the output stream for writing binary data from the servlet.java.lang.StringgetRedirectLocation()Get the location that was redirected to.intgetStatus()Get the status code.java.io.PrintWritergetWriter()Get the print writer for writing text output for this response.voidinitialize()Reset the response ready for reuse.booleanisCommitted()Always returns false.booleanisError()Return whether the servlet returned an error code or not.booleanisRedirect()Check whether the response was redirected or not.voidreset()Delegate to initialize method.voidresetBuffer()Clears the buffer.voidsendError(int code)Send an error code.voidsendError(int code, java.lang.String msg)Send an error code.voidsendRedirect(java.lang.String location)Indicate sending of a redirectLocation to a particular named resource.voidsetBufferSize(int size)Method ignored.voidsetCharacterEncoding(java.lang.String characterEncoding)Set the character encoding.voidsetContentLength(int length)Set the content length.voidsetContentLengthLong(long len)voidsetContentType(java.lang.String type)Set the content type.voidsetDateHeader(java.lang.String name, long l)Set a date header.voidsetHeader(java.lang.String name, java.lang.String value)Set the given header value.voidsetIntHeader(java.lang.String name, int i)Set an int header value.voidsetLocale(java.util.Locale locale)Set the locale in the response header.voidsetStatus(int status)Set the status for this response.voidsetStatus(int status, java.lang.String msg)Deprecated.voidwriteMetaData(org.apache.wicket.request.http.WebResponse webResponse)transfer cookies to given web response
-
-
-
Constructor Detail
-
MockHttpServletResponse
public MockHttpServletResponse(MockHttpServletRequest servletRequest)
Create the response object.- Parameters:
servletRequest-
-
-
Method Detail
-
addCookie
public void addCookie(javax.servlet.http.Cookie cookie)
Add a cookie to the response.- Specified by:
addCookiein interfacejavax.servlet.http.HttpServletResponse- Parameters:
cookie- The cookie to add
-
addDateHeader
public void addDateHeader(java.lang.String name, long l)Add a date header.- Specified by:
addDateHeaderin interfacejavax.servlet.http.HttpServletResponse- Parameters:
name- The header valuel- The long value
-
addHeader
public void addHeader(java.lang.String name, java.lang.String value)Add the given header value, including an additional entry if one already exists.- Specified by:
addHeaderin interfacejavax.servlet.http.HttpServletResponse- Parameters:
name- The name for the headervalue- The value for the header
-
addIntHeader
public void addIntHeader(java.lang.String name, int i)Add an int header value.- Specified by:
addIntHeaderin interfacejavax.servlet.http.HttpServletResponse- Parameters:
name- The header namei- The value
-
containsHeader
public boolean containsHeader(java.lang.String name)
Check if the response contains the given header name.- Specified by:
containsHeaderin interfacejavax.servlet.http.HttpServletResponse- Parameters:
name- The name to check- Returns:
- Whether header in response or not
-
encodeRedirectUrl
public java.lang.String encodeRedirectUrl(java.lang.String url)
Encode the redirectLocation URL. Does no changes as this test implementation uses cookie based url tracking.- Specified by:
encodeRedirectUrlin interfacejavax.servlet.http.HttpServletResponse- Parameters:
url- The url to encode- Returns:
- The encoded url
-
encodeRedirectURL
public java.lang.String encodeRedirectURL(java.lang.String url)
Encode the redirectLocation URL. Does no changes as this test implementation uses cookie based url tracking.- Specified by:
encodeRedirectURLin interfacejavax.servlet.http.HttpServletResponse- Parameters:
url- The url to encode- Returns:
- The encoded url
-
encodeUrl
public java.lang.String encodeUrl(java.lang.String url)
Encode the URL. Does no changes as this test implementation uses cookie based url tracking.- Specified by:
encodeUrlin interfacejavax.servlet.http.HttpServletResponse- Parameters:
url- The url to encode- Returns:
- The encoded url
-
encodeURL
public java.lang.String encodeURL(java.lang.String url)
Encode the URL. Does no changes as this test implementation uses cookie based url tracking.- Specified by:
encodeURLin interfacejavax.servlet.http.HttpServletResponse- Parameters:
url- The url to encode- Returns:
- The encoded url
-
flushBuffer
public void flushBuffer() throws java.io.IOExceptionFlush the buffer.- Specified by:
flushBufferin interfacejavax.servlet.ServletResponse- Throws:
java.io.IOException
-
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:
getBufferSizein interfacejavax.servlet.ServletResponse- Returns:
- The buffer size
-
getCharacterEncoding
public java.lang.String getCharacterEncoding()
Get the character encoding of the response.- Specified by:
getCharacterEncodingin interfacejavax.servlet.ServletResponse- Returns:
- The character encoding
-
getCookies
public java.util.List<javax.servlet.http.Cookie> getCookies()
Get all of the cookies that have been added to the response.- Returns:
- The collection of cookies
-
getDocument
public java.lang.String getDocument()
Get the text document that was written as part of this response.- Returns:
- The document
-
getErrorMessage
public java.lang.String getErrorMessage()
Get the error message.- Returns:
- The error message, or null if no message
-
getHeader
public java.lang.String getHeader(java.lang.String name)
Return the value of the given named header.- Specified by:
getHeaderin interfacejavax.servlet.http.HttpServletResponse- Parameters:
name- The header name- Returns:
- The value, or null
-
getHeaderNames
public java.util.Set<java.lang.String> getHeaderNames()
Get the names of all of the headers.- Specified by:
getHeaderNamesin interfacejavax.servlet.http.HttpServletResponse- Returns:
- The header names
-
getLocale
public java.util.Locale getLocale()
Get the encoded locale- Specified by:
getLocalein interfacejavax.servlet.ServletResponse- Returns:
- The locale
-
getOutputStream
public javax.servlet.ServletOutputStream getOutputStream()
Get the output stream for writing binary data from the servlet.- Specified by:
getOutputStreamin interfacejavax.servlet.ServletResponse- Returns:
- The binary output stream.
-
getRedirectLocation
public java.lang.String getRedirectLocation()
Get the location that was redirected to.- Returns:
- The redirect location, or null if not a redirect
-
getStatus
public int getStatus()
Get the status code.- Specified by:
getStatusin interfacejavax.servlet.http.HttpServletResponse- Returns:
- The status code
-
getWriter
public java.io.PrintWriter getWriter() throws java.io.IOExceptionGet the print writer for writing text output for this response.- Specified by:
getWriterin interfacejavax.servlet.ServletResponse- Returns:
- The writer
- Throws:
java.io.IOException- Not used
-
initialize
public void initialize()
Reset the response ready for reuse.
-
isCommitted
public boolean isCommitted()
Always returns false.- Specified by:
isCommittedin interfacejavax.servlet.ServletResponse- 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
-
reset
public void reset()
Delegate to initialize method.- Specified by:
resetin interfacejavax.servlet.ServletResponse
-
resetBuffer
public void resetBuffer()
Clears the buffer.- Specified by:
resetBufferin interfacejavax.servlet.ServletResponse
-
sendError
public void sendError(int code) throws java.io.IOExceptionSend an error code. This implementation just sets the internal error state information.- Specified by:
sendErrorin interfacejavax.servlet.http.HttpServletResponse- Parameters:
code- The code- Throws:
java.io.IOException- Not used
-
sendError
public void sendError(int code, java.lang.String msg) throws java.io.IOExceptionSend an error code. This implementation just sets the internal error state information.- Specified by:
sendErrorin interfacejavax.servlet.http.HttpServletResponse- Parameters:
code- The error codemsg- The error message- Throws:
java.io.IOException- Not used
-
sendRedirect
public void sendRedirect(java.lang.String location) throws java.io.IOExceptionIndicate sending of a redirectLocation to a particular named resource. This implementation just keeps hold of the redirectLocation info and makes it available for query.- Specified by:
sendRedirectin interfacejavax.servlet.http.HttpServletResponse- Parameters:
location- The location to redirectLocation to- Throws:
java.io.IOException- Not used
-
setBufferSize
public void setBufferSize(int size)
Method ignored.- Specified by:
setBufferSizein interfacejavax.servlet.ServletResponse- Parameters:
size- The size
-
setCharacterEncoding
public void setCharacterEncoding(java.lang.String characterEncoding)
Set the character encoding.- Specified by:
setCharacterEncodingin interfacejavax.servlet.ServletResponse- Parameters:
characterEncoding- The character encoding
-
setContentLength
public void setContentLength(int length)
Set the content length.- Specified by:
setContentLengthin interfacejavax.servlet.ServletResponse- Parameters:
length- The length
-
setContentLengthLong
public void setContentLengthLong(long len)
- Specified by:
setContentLengthLongin interfacejavax.servlet.ServletResponse
-
setContentType
public void setContentType(java.lang.String type)
Set the content type.- Specified by:
setContentTypein interfacejavax.servlet.ServletResponse- Parameters:
type- The content type
-
getContentType
public java.lang.String getContentType()
- Specified by:
getContentTypein interfacejavax.servlet.ServletResponse- Returns:
- value of content-type header
-
setDateHeader
public void setDateHeader(java.lang.String name, long l)Set a date header.- Specified by:
setDateHeaderin interfacejavax.servlet.http.HttpServletResponse- Parameters:
name- The header namel- The long value
-
formatDate
public static java.lang.String formatDate(long l)
- Parameters:
l-- Returns:
- formatted date
-
formatDate
public static void formatDate(java.lang.StringBuilder buf, java.util.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-calendar-cookie-
-
append2digits
public static void append2digits(java.lang.StringBuilder buf, int i)- Parameters:
buf-i-
-
setHeader
public void setHeader(java.lang.String name, java.lang.String value)Set the given header value.- Specified by:
setHeaderin interfacejavax.servlet.http.HttpServletResponse- Parameters:
name- The name for the headervalue- The value for the header
-
setIntHeader
public void setIntHeader(java.lang.String name, int i)Set an int header value.- Specified by:
setIntHeaderin interfacejavax.servlet.http.HttpServletResponse- Parameters:
name- The header namei- The value
-
setLocale
public void setLocale(java.util.Locale locale)
Set the locale in the response header.- Specified by:
setLocalein interfacejavax.servlet.ServletResponse- Parameters:
locale- The locale
-
setStatus
public void setStatus(int status)
Set the status for this response.- Specified by:
setStatusin interfacejavax.servlet.http.HttpServletResponse- Parameters:
status- The status
-
setStatus
@Deprecated public void setStatus(int status, java.lang.String msg)Deprecated.Set the status for this response.- Specified by:
setStatusin interfacejavax.servlet.http.HttpServletResponse- Parameters:
status- The statusmsg- The message
-
getBinaryResponse
public java.lang.String getBinaryResponse()
- Returns:
- binary response
-
getHeaders
public java.util.Collection<java.lang.String> getHeaders(java.lang.String name)
- Specified by:
getHeadersin interfacejavax.servlet.http.HttpServletResponse- Parameters:
name-- Returns:
- headers with given name
-
writeMetaData
public void writeMetaData(org.apache.wicket.request.http.WebResponse webResponse)
Description copied from interface:IMetaDataBufferingWebResponsetransfer cookies to given web response- Specified by:
writeMetaDatain interfaceIMetaDataBufferingWebResponse- Parameters:
webResponse- web response that should receive the current cookies
-
-