public class MockHttpServletResponse extends Object implements 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(Cookie cookie)
Adds a cookie to the set of cookies in the response.
|
void |
addDateHeader(String name,
long value)
Adds the specified value for the named header (does not remove/replace existing values).
|
void |
addHeader(String name,
String value)
Adds the specified value for the named header (does not remove/replace existing values).
|
void |
addIntHeader(String name,
int value)
Adds the specified value for the named header (does not remove/replace existing values).
|
boolean |
containsHeader(String name)
Returns true if the specified header was placed in the response.
|
String |
encodeRedirectUrl(String url)
Returns the URL unchanged.
|
String |
encodeRedirectURL(String url)
Returns the URL unchanged.
|
String |
encodeUrl(String url)
Returns the URL unchanged.
|
String |
encodeURL(String url)
Returns the URL unchanged.
|
void |
flushBuffer()
Has no effect.
|
int |
getBufferSize()
Always returns 0.
|
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().
|
String |
getContentType()
Gets the content type for the response.
|
Cookie[] |
getCookies()
Gets the set of cookies stored in the response.
|
String |
getErrorMessage()
Gets the error message if one was set with setStatus() or sendError().
|
Map<String,List<Object>> |
getHeaderMap()
Provides access to all headers that were set.
|
Locale |
getLocale()
Gets the response locale.
|
byte[] |
getOutputBytes()
Gets the output that was written to the output stream, as a byte[].
|
ServletOutputStream |
getOutputStream()
Returns a reference to a ServletOutputStream to be used for output.
|
String |
getOutputString()
Gets the output that was written to the output stream, as a character String.
|
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.
|
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,
String errorMessage)
Sets the status code and saves the message so it can be retrieved later.
|
void |
sendRedirect(String url)
Simply sets the status code and stores the URL that was supplied, so that it can be examined
later with getRedirectUrl.
|
void |
setBufferSize(int i)
Has no effect.
|
void |
setCharacterEncoding(String encoding)
Sets the character encoding on the request.
|
void |
setContentLength(int contentLength)
Sets a custom content length on the response.
|
void |
setContentType(String contentType)
Sets the content type for the response.
|
void |
setDateHeader(String name,
long value)
Stores the value in a Long and saves it as a header.
|
void |
setHeader(String name,
String value)
Sets the value of the specified header to the single value provided.
|
void |
setIntHeader(String name,
int value)
Stores the value in an Integer and saves it as a header.
|
void |
setLocale(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,
String errorMessage)
Saves the HTTP status code and the message provided.
|
public void addCookie(Cookie cookie)
addCookie in interface HttpServletResponsepublic Cookie[] getCookies()
public boolean containsHeader(String name)
containsHeader in interface HttpServletResponsepublic String encodeURL(String url)
encodeURL in interface HttpServletResponsepublic String encodeRedirectURL(String url)
encodeRedirectURL in interface HttpServletResponsepublic String encodeUrl(String url)
encodeUrl in interface HttpServletResponsepublic String encodeRedirectUrl(String url)
encodeRedirectUrl in interface HttpServletResponsepublic void sendError(int status,
String errorMessage)
throws IOException
sendError in interface HttpServletResponseIOExceptionpublic void sendError(int status)
throws IOException
sendError in interface HttpServletResponseIOExceptionpublic void sendRedirect(String url) throws IOException
sendRedirect in interface HttpServletResponseIOExceptionpublic String getRedirectUrl()
public void setDateHeader(String name, long value)
setDateHeader in interface HttpServletResponsepublic void addDateHeader(String name, long value)
addDateHeader in interface HttpServletResponsepublic void setHeader(String name, String value)
setHeader in interface HttpServletResponsepublic void addHeader(String name, String value)
addHeader in interface HttpServletResponsepublic void setIntHeader(String name, int value)
setIntHeader in interface HttpServletResponsepublic void addIntHeader(String name, int value)
addIntHeader in interface HttpServletResponsepublic Map<String,List<Object>> getHeaderMap()
public void setStatus(int statusCode)
setStatus in interface HttpServletResponsepublic void setStatus(int status,
String errorMessage)
setStatus in interface HttpServletResponsepublic int getStatus()
public String getErrorMessage()
public void setCharacterEncoding(String encoding)
setCharacterEncoding in interface ServletResponsepublic String getCharacterEncoding()
getCharacterEncoding in interface ServletResponsepublic void setContentType(String contentType)
setContentType in interface ServletResponsepublic String getContentType()
getContentType in interface ServletResponsepublic ServletOutputStream getOutputStream() throws IOException
getOutputStream in interface ServletResponseIOExceptionpublic PrintWriter getWriter() throws IOException
getWriter in interface ServletResponseIOExceptionpublic byte[] getOutputBytes()
public String getOutputString()
public void setContentLength(int contentLength)
setContentLength in interface ServletResponsepublic int getContentLength()
public void setBufferSize(int i)
setBufferSize in interface ServletResponsepublic int getBufferSize()
getBufferSize in interface ServletResponsepublic void flushBuffer()
throws IOException
flushBuffer in interface ServletResponseIOExceptionpublic void resetBuffer()
resetBuffer in interface ServletResponsepublic boolean isCommitted()
isCommitted in interface ServletResponsepublic void reset()
reset in interface ServletResponsepublic void setLocale(Locale locale)
setLocale in interface ServletResponsepublic Locale getLocale()
getLocale in interface ServletResponse© Copyright 2005-2006, Stripes Development Team.