public class MockHttpServletResponse
extends java.lang.Object
implements javax.servlet.http.HttpServletResponse
Mock implementation of an HttpServletResponse. Captures any output is written along with any headers, status information etc. and makes it available through various getter methods.
Of major note is the fact that none of the setStatus(), sendError() or sendRedirect() methods have any real effect on the request processing lifecycle. Information is recorded so it can be verified what was invoked, but that is all.
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 and Description |
---|
MockHttpServletResponse() |
Modifier and Type | Method and Description |
---|---|
void |
addCookie(javax.servlet.http.Cookie cookie)
Adds a cookie to the set of cookies in the response.
|
void |
addDateHeader(java.lang.String name,
long value)
Adds the specified value for the named header (does not remove/replace existing values).
|
void |
addHeader(java.lang.String name,
java.lang.String value)
Adds the specified value for the named header (does not remove/replace existing values).
|
void |
addIntHeader(java.lang.String name,
int value)
Adds the specified value for the named header (does not remove/replace existing values).
|
boolean |
containsHeader(java.lang.String name)
Returns true if the specified header was placed in the response.
|
java.lang.String |
encodeRedirectUrl(java.lang.String url)
Returns the URL unchanged.
|
java.lang.String |
encodeRedirectURL(java.lang.String url)
Returns the URL unchanged.
|
java.lang.String |
encodeUrl(java.lang.String url)
Returns the URL unchanged.
|
java.lang.String |
encodeURL(java.lang.String url)
Returns the URL unchanged.
|
void |
flushBuffer()
Has no effect.
|
int |
getBufferSize()
Always returns 0.
|
java.lang.String |
getCharacterEncoding()
Gets the character encoding (defaults to UTF-8).
|
int |
getContentLength()
Returns the content length if one was set on the response by calling setContentLength().
|
java.lang.String |
getContentType()
Gets the content type for the response.
|
javax.servlet.http.Cookie[] |
getCookies()
Gets the set of cookies stored in the response.
|
java.lang.String |
getErrorMessage()
Gets the error message if one was set with setStatus() or sendError().
|
java.util.Map<java.lang.String,java.util.List<java.lang.Object>> |
getHeaderMap()
Provides access to all headers that were set.
|
java.util.Locale |
getLocale()
Gets the response locale.
|
byte[] |
getOutputBytes()
Gets the output that was written to the output stream, as a byte[].
|
javax.servlet.ServletOutputStream |
getOutputStream()
Returns a reference to a ServletOutputStream to be used for output.
|
java.lang.String |
getOutputString()
Gets the output that was written to the output stream, as a character String.
|
java.lang.String |
getRedirectUrl()
If a call was made to sendRedirect() this method will return the URL that was supplied.
|
int |
getStatus()
Gets the status (or error) code if one was set.
|
java.io.PrintWriter |
getWriter()
Returns a reference to a PrintWriter to be used for character output.
|
boolean |
isCommitted()
Always returns true.
|
void |
reset()
Always throws an IllegalStateException.
|
void |
resetBuffer()
Always throws IllegalStateException.
|
void |
sendError(int status)
Sets that status code to the error code provided.
|
void |
sendError(int status,
java.lang.String errorMessage)
Sets the status code and saves the message so it can be retrieved later.
|
void |
sendRedirect(java.lang.String url)
Simply stores the URL that was supplied, so that it can be examined later with getRedirectUrl.
|
void |
setBufferSize(int i)
Has no effect.
|
void |
setCharacterEncoding(java.lang.String encoding)
Sets the character encoding on the request.
|
void |
setContentLength(int contentLength)
Sets a custom content length on the response.
|
void |
setContentType(java.lang.String contentType)
Sets the content type for the response.
|
void |
setDateHeader(java.lang.String name,
long value)
Stores the value in a Long and saves it as a header.
|
void |
setHeader(java.lang.String name,
java.lang.String value)
Sets the value of the specified header to the single value provided.
|
void |
setIntHeader(java.lang.String name,
int value)
Stores the value in an Integer and saves it as a header.
|
void |
setLocale(java.util.Locale locale)
Sets the response locale to the one specified.
|
void |
setStatus(int statusCode)
Sets the HTTP Status code of the response.
|
void |
setStatus(int status,
java.lang.String errorMessage)
Saves the HTTP status code and the message provided.
|
public void addCookie(javax.servlet.http.Cookie cookie)
addCookie
in interface javax.servlet.http.HttpServletResponse
public javax.servlet.http.Cookie[] getCookies()
public boolean containsHeader(java.lang.String name)
containsHeader
in interface javax.servlet.http.HttpServletResponse
public java.lang.String encodeURL(java.lang.String url)
encodeURL
in interface javax.servlet.http.HttpServletResponse
public java.lang.String encodeRedirectURL(java.lang.String url)
encodeRedirectURL
in interface javax.servlet.http.HttpServletResponse
public java.lang.String encodeUrl(java.lang.String url)
encodeUrl
in interface javax.servlet.http.HttpServletResponse
public java.lang.String encodeRedirectUrl(java.lang.String url)
encodeRedirectUrl
in interface javax.servlet.http.HttpServletResponse
public void sendError(int status, java.lang.String errorMessage) throws java.io.IOException
sendError
in interface javax.servlet.http.HttpServletResponse
java.io.IOException
public void sendError(int status) throws java.io.IOException
sendError
in interface javax.servlet.http.HttpServletResponse
java.io.IOException
public void sendRedirect(java.lang.String url) throws java.io.IOException
sendRedirect
in interface javax.servlet.http.HttpServletResponse
java.io.IOException
public java.lang.String getRedirectUrl()
public void setDateHeader(java.lang.String name, long value)
setDateHeader
in interface javax.servlet.http.HttpServletResponse
public void addDateHeader(java.lang.String name, long value)
addDateHeader
in interface javax.servlet.http.HttpServletResponse
public void setHeader(java.lang.String name, java.lang.String value)
setHeader
in interface javax.servlet.http.HttpServletResponse
public void addHeader(java.lang.String name, java.lang.String value)
addHeader
in interface javax.servlet.http.HttpServletResponse
public void setIntHeader(java.lang.String name, int value)
setIntHeader
in interface javax.servlet.http.HttpServletResponse
public void addIntHeader(java.lang.String name, int value)
addIntHeader
in interface javax.servlet.http.HttpServletResponse
public java.util.Map<java.lang.String,java.util.List<java.lang.Object>> getHeaderMap()
public void setStatus(int statusCode)
setStatus
in interface javax.servlet.http.HttpServletResponse
public void setStatus(int status, java.lang.String errorMessage)
setStatus
in interface javax.servlet.http.HttpServletResponse
public int getStatus()
public java.lang.String getErrorMessage()
public void setCharacterEncoding(java.lang.String encoding)
setCharacterEncoding
in interface javax.servlet.ServletResponse
public java.lang.String getCharacterEncoding()
getCharacterEncoding
in interface javax.servlet.ServletResponse
public void setContentType(java.lang.String contentType)
setContentType
in interface javax.servlet.ServletResponse
public java.lang.String getContentType()
getContentType
in interface javax.servlet.ServletResponse
public javax.servlet.ServletOutputStream getOutputStream() throws java.io.IOException
getOutputStream
in interface javax.servlet.ServletResponse
java.io.IOException
public java.io.PrintWriter getWriter() throws java.io.IOException
getWriter
in interface javax.servlet.ServletResponse
java.io.IOException
public byte[] getOutputBytes()
public java.lang.String getOutputString()
public void setContentLength(int contentLength)
setContentLength
in interface javax.servlet.ServletResponse
public int getContentLength()
public void setBufferSize(int i)
setBufferSize
in interface javax.servlet.ServletResponse
public int getBufferSize()
getBufferSize
in interface javax.servlet.ServletResponse
public void flushBuffer() throws java.io.IOException
flushBuffer
in interface javax.servlet.ServletResponse
java.io.IOException
public void resetBuffer()
resetBuffer
in interface javax.servlet.ServletResponse
public boolean isCommitted()
isCommitted
in interface javax.servlet.ServletResponse
public void reset()
reset
in interface javax.servlet.ServletResponse
public void setLocale(java.util.Locale locale)
setLocale
in interface javax.servlet.ServletResponse
public java.util.Locale getLocale()
getLocale
in interface javax.servlet.ServletResponse
? Copyright 2005-2006, Stripes Development Team.