public class MockHttpServletResponse
extends java.lang.Object
javax.servlet.http.HttpServletResponse
interface.
As of Spring Framework 5.0, this set of mocks is designed on a Servlet 4.0 baseline.
Modifier and Type | Class and Description |
---|---|
private class |
MockHttpServletResponse.ResponsePrintWriter
Inner class that adapts the PrintWriter to mark the
response as committed once the buffer size is exceeded.
|
private class |
MockHttpServletResponse.ResponseServletOutputStream
Inner class that adapts the ServletOutputStream to mark the
response as committed once the buffer size is exceeded.
|
Modifier and Type | Field and Description |
---|---|
private int |
bufferSize |
private java.lang.String |
characterEncoding |
private boolean |
charset |
private static java.lang.String |
CHARSET_PREFIX |
private boolean |
committed |
private java.io.ByteArrayOutputStream |
content |
private long |
contentLength |
private java.lang.String |
contentType |
private java.util.List<Cookie> |
cookies |
private static java.lang.String |
DATE_FORMAT |
private java.lang.String |
errorMessage |
private java.lang.String |
forwardedUrl |
private static java.util.TimeZone |
GMT |
private java.util.Map<java.lang.String,HeaderValueHolder> |
headers |
private java.util.List<java.lang.String> |
includedUrls |
private java.util.Locale |
locale |
private ServletOutputStream |
outputStream |
private boolean |
outputStreamAccessAllowed |
private int |
status |
private java.io.PrintWriter |
writer |
private boolean |
writerAccessAllowed |
Constructor and Description |
---|
MockHttpServletResponse() |
Modifier and Type | Method and Description |
---|---|
void |
addCookie(Cookie cookie) |
void |
addDateHeader(java.lang.String name,
long value) |
void |
addHeader(java.lang.String name,
java.lang.String value) |
private void |
addHeaderValue(java.lang.String name,
java.lang.Object value) |
void |
addIncludedUrl(java.lang.String includedUrl) |
void |
addIntHeader(java.lang.String name,
int value) |
boolean |
containsHeader(java.lang.String name) |
private void |
doAddHeaderValue(java.lang.String name,
java.lang.Object value,
boolean replace) |
java.lang.String |
encodeRedirectUrl(java.lang.String url)
Deprecated.
|
java.lang.String |
encodeRedirectURL(java.lang.String url)
The default implementation delegates to
encodeURL(java.lang.String) ,
returning the given URL String as-is. |
java.lang.String |
encodeUrl(java.lang.String url)
Deprecated.
|
java.lang.String |
encodeURL(java.lang.String url)
The default implementation returns the given URL String as-is.
|
void |
flushBuffer() |
private java.lang.String |
formatDate(long date) |
int |
getBufferSize() |
java.lang.String |
getCharacterEncoding() |
byte[] |
getContentAsByteArray() |
java.lang.String |
getContentAsString() |
int |
getContentLength() |
long |
getContentLengthLong() |
java.lang.String |
getContentType() |
Cookie |
getCookie(java.lang.String name) |
private java.lang.String |
getCookieHeader(Cookie cookie) |
Cookie[] |
getCookies() |
long |
getDateHeader(java.lang.String name) |
java.lang.String |
getErrorMessage() |
java.lang.String |
getForwardedUrl() |
java.lang.String |
getHeader(java.lang.String name)
Return the primary value for the given header as a String, if any.
|
java.util.Collection<java.lang.String> |
getHeaderNames()
Return the names of all specified headers as a Set of Strings.
|
java.util.List<java.lang.String> |
getHeaders(java.lang.String name)
Return all values for the given header as a List of Strings.
|
java.lang.Object |
getHeaderValue(java.lang.String name)
Return the primary value for the given header, if any.
|
java.util.List<java.lang.Object> |
getHeaderValues(java.lang.String name)
Return all values for the given header as a List of value objects.
|
java.lang.String |
getIncludedUrl() |
java.util.List<java.lang.String> |
getIncludedUrls() |
java.util.Locale |
getLocale() |
ServletOutputStream |
getOutputStream() |
java.lang.String |
getRedirectedUrl() |
int |
getStatus() |
java.io.PrintWriter |
getWriter() |
boolean |
isCharset()
Return whether the character encoding has been set.
|
boolean |
isCommitted() |
boolean |
isOutputStreamAccessAllowed()
Return whether
getOutputStream() access is allowed. |
boolean |
isWriterAccessAllowed()
Return whether
getOutputStream() access is allowed. |
private java.text.DateFormat |
newDateFormat() |
void |
reset() |
void |
resetBuffer() |
void |
sendError(int status) |
void |
sendError(int status,
java.lang.String errorMessage) |
void |
sendRedirect(java.lang.String url) |
void |
setBufferSize(int bufferSize) |
void |
setCharacterEncoding(java.lang.String characterEncoding) |
void |
setCommitted(boolean committed) |
private void |
setCommittedIfBufferSizeExceeded() |
void |
setContentLength(int contentLength) |
void |
setContentLengthLong(long contentLength) |
void |
setContentType(java.lang.String contentType) |
private void |
setCookie(Cookie cookie)
Set the
Set-Cookie header to the supplied Cookie ,
overwriting any previous cookies. |
void |
setDateHeader(java.lang.String name,
long value) |
void |
setForwardedUrl(java.lang.String forwardedUrl) |
void |
setHeader(java.lang.String name,
java.lang.String value) |
private void |
setHeaderValue(java.lang.String name,
java.lang.Object value) |
void |
setIncludedUrl(java.lang.String includedUrl) |
void |
setIntHeader(java.lang.String name,
int value) |
void |
setLocale(java.util.Locale locale) |
void |
setOutputStreamAccessAllowed(boolean outputStreamAccessAllowed)
Set whether
getOutputStream() access is allowed. |
private boolean |
setSpecialHeader(java.lang.String name,
java.lang.Object value,
boolean replaceHeader) |
void |
setStatus(int status) |
void |
setStatus(int status,
java.lang.String errorMessage)
Deprecated.
|
void |
setWriterAccessAllowed(boolean writerAccessAllowed)
Set whether
getWriter() access is allowed. |
private void |
updateContentTypeHeader() |
private static final java.lang.String CHARSET_PREFIX
private static final java.lang.String DATE_FORMAT
private static final java.util.TimeZone GMT
private boolean outputStreamAccessAllowed
private boolean writerAccessAllowed
@Nullable private java.lang.String characterEncoding
private boolean charset
private final java.io.ByteArrayOutputStream content
private final ServletOutputStream outputStream
@Nullable private java.io.PrintWriter writer
private long contentLength
@Nullable private java.lang.String contentType
private int bufferSize
private boolean committed
private java.util.Locale locale
private final java.util.List<Cookie> cookies
private final java.util.Map<java.lang.String,HeaderValueHolder> headers
private int status
@Nullable private java.lang.String errorMessage
@Nullable private java.lang.String forwardedUrl
private final java.util.List<java.lang.String> includedUrls
public void setOutputStreamAccessAllowed(boolean outputStreamAccessAllowed)
getOutputStream()
access is allowed.
Default is true
.
public boolean isOutputStreamAccessAllowed()
getOutputStream()
access is allowed.public void setWriterAccessAllowed(boolean writerAccessAllowed)
getWriter()
access is allowed.
Default is true
.
public boolean isWriterAccessAllowed()
getOutputStream()
access is allowed.public boolean isCharset()
If false
, getCharacterEncoding()
will return a default encoding value.
public void setCharacterEncoding(java.lang.String characterEncoding)
private void updateContentTypeHeader()
@Nullable public java.lang.String getCharacterEncoding()
public ServletOutputStream getOutputStream()
public java.io.PrintWriter getWriter() throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException
public byte[] getContentAsByteArray()
public java.lang.String getContentAsString() throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException
public void setContentLength(int contentLength)
public int getContentLength()
public void setContentLengthLong(long contentLength)
public long getContentLengthLong()
public void setContentType(@Nullable java.lang.String contentType)
@Nullable public java.lang.String getContentType()
public void setBufferSize(int bufferSize)
public int getBufferSize()
public void flushBuffer()
public void resetBuffer()
private void setCommittedIfBufferSizeExceeded()
public void setCommitted(boolean committed)
public boolean isCommitted()
public void reset()
public void setLocale(java.util.Locale locale)
public java.util.Locale getLocale()
public void addCookie(Cookie cookie)
private java.lang.String getCookieHeader(Cookie cookie)
public Cookie[] getCookies()
@Nullable public Cookie getCookie(java.lang.String name)
public boolean containsHeader(java.lang.String name)
public java.util.Collection<java.lang.String> getHeaderNames()
As of Servlet 3.0, this method is also defined HttpServletResponse.
Set
of header name Strings
, or an empty Set
if none@Nullable public java.lang.String getHeader(java.lang.String name)
As of Servlet 3.0, this method is also defined in HttpServletResponse.
As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
Consider using getHeaderValue(String)
for raw Object access.
name
- the name of the headernull
if nonepublic java.util.List<java.lang.String> getHeaders(java.lang.String name)
As of Servlet 3.0, this method is also defined in HttpServletResponse.
As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
Consider using getHeaderValues(String)
for raw Object access.
name
- the name of the header@Nullable public java.lang.Object getHeaderValue(java.lang.String name)
Will return the first value in case of multiple values.
name
- the name of the headernull
if nonepublic java.util.List<java.lang.Object> getHeaderValues(java.lang.String name)
name
- the name of the headerpublic java.lang.String encodeURL(java.lang.String url)
Can be overridden in subclasses, appending a session id or the like.
public java.lang.String encodeRedirectURL(java.lang.String url)
encodeURL(java.lang.String)
,
returning the given URL String as-is.
Can be overridden in subclasses, appending a session id or the like
in a redirect-specific fashion. For general URL encoding rules,
override the common encodeURL(java.lang.String)
method instead, applying
to redirect URLs as well as to general URLs.
@Deprecated public java.lang.String encodeUrl(java.lang.String url)
@Deprecated public java.lang.String encodeRedirectUrl(java.lang.String url)
public void sendError(int status, java.lang.String errorMessage) throws java.io.IOException
java.io.IOException
public void sendError(int status) throws java.io.IOException
java.io.IOException
public void sendRedirect(java.lang.String url) throws java.io.IOException
java.io.IOException
@Nullable public java.lang.String getRedirectedUrl()
public void setDateHeader(java.lang.String name, long value)
public void addDateHeader(java.lang.String name, long value)
public long getDateHeader(java.lang.String name)
private java.lang.String formatDate(long date)
private java.text.DateFormat newDateFormat()
public void setHeader(java.lang.String name, java.lang.String value)
public void addHeader(java.lang.String name, java.lang.String value)
public void setIntHeader(java.lang.String name, int value)
public void addIntHeader(java.lang.String name, int value)
private void setHeaderValue(java.lang.String name, java.lang.Object value)
private void addHeaderValue(java.lang.String name, java.lang.Object value)
private boolean setSpecialHeader(java.lang.String name, java.lang.Object value, boolean replaceHeader)
private void doAddHeaderValue(java.lang.String name, java.lang.Object value, boolean replace)
private void setCookie(Cookie cookie)
Set-Cookie
header to the supplied Cookie
,
overwriting any previous cookies.cookie
- the Cookie
to setaddCookie(Cookie)
public void setStatus(int status)
@Deprecated public void setStatus(int status, java.lang.String errorMessage)
public int getStatus()
@Nullable public java.lang.String getErrorMessage()
public void setForwardedUrl(@Nullable java.lang.String forwardedUrl)
@Nullable public java.lang.String getForwardedUrl()
public void setIncludedUrl(@Nullable java.lang.String includedUrl)
@Nullable public java.lang.String getIncludedUrl()
public void addIncludedUrl(java.lang.String includedUrl)
public java.util.List<java.lang.String> getIncludedUrls()