Package com.helger.servlet.mock
Class MockHttpServletResponse
- java.lang.Object
-
- com.helger.servlet.mock.MockHttpServletResponse
-
- All Implemented Interfaces:
jakarta.servlet.http.HttpServletResponse,jakarta.servlet.ServletResponse
@NotThreadSafe public class MockHttpServletResponse extends Object implements jakarta.servlet.http.HttpServletResponse
Mock implementation ofHttpServletResponse.- Author:
- Philip Helger
-
-
Field Summary
Fields Modifier and Type Field Description static CharsetDEFAULT_CHARSET_OBJstatic intDEFAULT_SERVER_PORT-
Fields inherited from interface jakarta.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()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddCookie(jakarta.servlet.http.Cookie aCookie)voidaddDateHeader(String sName, long nValue)voidaddHeader(String sName, String sValue)voidaddIntHeader(String sName, int nValue)booleancontainsHeader(String sName)StringencodeRedirectUrl(String sUrl)Deprecated.StringencodeRedirectURL(String sUrl)The default implementation returns the given URL String as-is.StringencodeUrl(String sUrl)Deprecated.StringencodeURL(String sUrl)The default implementation returns the given URL String as-is.voidflushBuffer()intgetBufferSize()StringgetCharacterEncoding()CharsetgetCharacterEncodingObj()CharsetgetCharacterEncodingObjOrDefault()byte[]getContentAsByteArray()StringgetContentAsString(Charset aCharset)intgetContentLength()StringgetContentType()jakarta.servlet.http.CookiegetCookie(String sName)jakarta.servlet.http.Cookie[]getCookies()StringgetEncodeRedirectUrlSuffix()StringgetEncodeUrlSuffix()StringgetErrorMessage()StringgetForwardedUrl()StringgetHeader(String sName)Return the primary value for the given header, if any.com.helger.commons.collection.impl.ICommonsSet<String>getHeaderNames()Return the names of all specified headers as a Set of Strings.com.helger.commons.collection.impl.ICommonsList<String>getHeaders(String sName)Return all values for the given header as a List of value objects.StringgetIncludedUrl()LocalegetLocale()jakarta.servlet.ServletOutputStreamgetOutputStream()StringgetRedirectedUrl()intgetStatus()PrintWritergetWriter()booleanisCommitted()booleanisOutputStreamAccessAllowed()booleanisWriterAccessAllowed()voidreset()voidresetBuffer()voidsendError(int nStatus)voidsendError(int nStatus, String sErrorMessage)voidsendRedirect(String sUrl)voidsetBufferSize(int nBufferSize)voidsetCharacterEncoding(String sCharacterEncoding)voidsetCharacterEncoding(Charset aCharacterEncoding)voidsetCommitted(boolean bCommitted)voidsetContentLength(int nContentLength)voidsetContentLengthLong(long len)voidsetContentType(String sContentType)voidsetDateHeader(String sName, long nValue)voidsetEncodeRedirectUrlSuffix(String sEncodeRedirectUrlSuffix)voidsetEncodeUrlSuffix(String sEncodeUrlSuffix)voidsetForwardedUrl(String sForwardedUrl)voidsetHeader(String sName, String sValue)voidsetIncludedUrl(String sIncludedUrl)voidsetIntHeader(String sName, int nValue)voidsetLocale(Locale aLocale)voidsetOutputStreamAccessAllowed(boolean bOutputStreamAccessAllowed)Set whethergetOutputStream()access is allowed.voidsetStatus(int nStatus)voidsetStatus(int nStatus, String sErrorMessage)Deprecated.voidsetWriterAccessAllowed(boolean bWriterAccessAllowed)Set whethergetWriter()access is allowed.
-
-
-
Field Detail
-
DEFAULT_SERVER_PORT
public static final int DEFAULT_SERVER_PORT
-
DEFAULT_CHARSET_OBJ
public static final Charset DEFAULT_CHARSET_OBJ
-
-
Method Detail
-
setOutputStreamAccessAllowed
public void setOutputStreamAccessAllowed(boolean bOutputStreamAccessAllowed)
Set whethergetOutputStream()access is allowed.Default is
true.- Parameters:
bOutputStreamAccessAllowed-trueto enabled
-
isOutputStreamAccessAllowed
public boolean isOutputStreamAccessAllowed()
- Returns:
- whether
getOutputStream()access is allowed.
-
setWriterAccessAllowed
public void setWriterAccessAllowed(boolean bWriterAccessAllowed)
Set whethergetWriter()access is allowed.Default is
true.- Parameters:
bWriterAccessAllowed-trueto enabled
-
isWriterAccessAllowed
public boolean isWriterAccessAllowed()
- Returns:
- whether
getOutputStream()access is allowed.
-
setCharacterEncoding
public void setCharacterEncoding(@Nullable String sCharacterEncoding)
- Specified by:
setCharacterEncodingin interfacejakarta.servlet.ServletResponse
-
getCharacterEncoding
@Nullable public String getCharacterEncoding()
- Specified by:
getCharacterEncodingin interfacejakarta.servlet.ServletResponse
-
getOutputStream
@Nonnull public jakarta.servlet.ServletOutputStream getOutputStream()
- Specified by:
getOutputStreamin interfacejakarta.servlet.ServletResponse
-
getWriter
@Nonnull public PrintWriter getWriter()
- Specified by:
getWriterin interfacejakarta.servlet.ServletResponse
-
getContentAsByteArray
@Nonnull @ReturnsMutableCopy public byte[] getContentAsByteArray()
-
setContentLength
public void setContentLength(int nContentLength)
- Specified by:
setContentLengthin interfacejakarta.servlet.ServletResponse
-
getContentLength
public int getContentLength()
-
setContentType
public void setContentType(@Nullable String sContentType)
- Specified by:
setContentTypein interfacejakarta.servlet.ServletResponse
-
getContentType
@Nullable public String getContentType()
- Specified by:
getContentTypein interfacejakarta.servlet.ServletResponse
-
setBufferSize
public void setBufferSize(int nBufferSize)
- Specified by:
setBufferSizein interfacejakarta.servlet.ServletResponse
-
getBufferSize
public int getBufferSize()
- Specified by:
getBufferSizein interfacejakarta.servlet.ServletResponse
-
flushBuffer
public void flushBuffer()
- Specified by:
flushBufferin interfacejakarta.servlet.ServletResponse
-
resetBuffer
public void resetBuffer()
- Specified by:
resetBufferin interfacejakarta.servlet.ServletResponse
-
setCommitted
public void setCommitted(boolean bCommitted)
-
isCommitted
public boolean isCommitted()
- Specified by:
isCommittedin interfacejakarta.servlet.ServletResponse
-
reset
public void reset()
- Specified by:
resetin interfacejakarta.servlet.ServletResponse
-
setLocale
public void setLocale(@Nullable Locale aLocale)
- Specified by:
setLocalein interfacejakarta.servlet.ServletResponse
-
getLocale
@Nullable public Locale getLocale()
- Specified by:
getLocalein interfacejakarta.servlet.ServletResponse
-
addCookie
public void addCookie(@Nonnull jakarta.servlet.http.Cookie aCookie)
- Specified by:
addCookiein interfacejakarta.servlet.http.HttpServletResponse
-
getCookies
@Nonnull @ReturnsMutableCopy public jakarta.servlet.http.Cookie[] getCookies()
-
containsHeader
public boolean containsHeader(@Nullable String sName)
- Specified by:
containsHeaderin interfacejakarta.servlet.http.HttpServletResponse
-
getHeaderNames
@Nonnull @ReturnsMutableCopy public com.helger.commons.collection.impl.ICommonsSet<String> getHeaderNames()
Return the names of all specified headers as a Set of Strings.- Specified by:
getHeaderNamesin interfacejakarta.servlet.http.HttpServletResponse- Returns:
- the
Setof header nameStrings, or an emptySetif none
-
getHeader
@Nullable public String getHeader(@Nullable String sName)
Return the primary value for the given header, if any.Will return the first value in case of multiple values.
- Specified by:
getHeaderin interfacejakarta.servlet.http.HttpServletResponse- Parameters:
sName- the name of the header- Returns:
- the associated header value, or
nullif none
-
getHeaders
@Nonnull public com.helger.commons.collection.impl.ICommonsList<String> getHeaders(@Nullable String sName)
Return all values for the given header as a List of value objects.- Specified by:
getHeadersin interfacejakarta.servlet.http.HttpServletResponse- Parameters:
sName- the name of the header- Returns:
- the associated header values, or an empty List if none
-
encodeURL
@Nullable public String encodeURL(@Nullable String sUrl)
The default implementation returns the given URL String as-is. UsesetEncodeUrlSuffix(String)to define a suffix to be appended.- Specified by:
encodeURLin interfacejakarta.servlet.http.HttpServletResponse- Returns:
- the encoded URL
-
encodeRedirectURL
@Nullable public String encodeRedirectURL(@Nullable String sUrl)
The default implementation returns the given URL String as-is. UsesetEncodeRedirectUrlSuffix(String)to define a suffix to be appended.- Specified by:
encodeRedirectURLin interfacejakarta.servlet.http.HttpServletResponse- Returns:
- the encoded URL
-
encodeUrl
@Deprecated(forRemoval=false) public String encodeUrl(@Nullable String sUrl)
Deprecated.- Specified by:
encodeUrlin interfacejakarta.servlet.http.HttpServletResponse
-
encodeRedirectUrl
@Deprecated(forRemoval=false) public String encodeRedirectUrl(@Nullable String sUrl)
Deprecated.- Specified by:
encodeRedirectUrlin interfacejakarta.servlet.http.HttpServletResponse
-
sendError
public void sendError(int nStatus, @Nullable String sErrorMessage) throws IOException- Specified by:
sendErrorin interfacejakarta.servlet.http.HttpServletResponse- Throws:
IOException
-
sendError
public void sendError(int nStatus) throws IOException- Specified by:
sendErrorin interfacejakarta.servlet.http.HttpServletResponse- Throws:
IOException
-
sendRedirect
public void sendRedirect(@Nonnull String sUrl) throws IOException
- Specified by:
sendRedirectin interfacejakarta.servlet.http.HttpServletResponse- Throws:
IOException
-
setDateHeader
public void setDateHeader(@Nullable String sName, long nValue)
- Specified by:
setDateHeaderin interfacejakarta.servlet.http.HttpServletResponse
-
addDateHeader
public void addDateHeader(@Nullable String sName, long nValue)
- Specified by:
addDateHeaderin interfacejakarta.servlet.http.HttpServletResponse
-
setHeader
public void setHeader(@Nullable String sName, @Nullable String sValue)
- Specified by:
setHeaderin interfacejakarta.servlet.http.HttpServletResponse
-
addHeader
public void addHeader(@Nullable String sName, @Nullable String sValue)
- Specified by:
addHeaderin interfacejakarta.servlet.http.HttpServletResponse
-
setIntHeader
public void setIntHeader(@Nullable String sName, int nValue)
- Specified by:
setIntHeaderin interfacejakarta.servlet.http.HttpServletResponse
-
addIntHeader
public void addIntHeader(@Nullable String sName, int nValue)
- Specified by:
addIntHeaderin interfacejakarta.servlet.http.HttpServletResponse
-
setStatus
public void setStatus(int nStatus)
- Specified by:
setStatusin interfacejakarta.servlet.http.HttpServletResponse
-
setStatus
@Deprecated(forRemoval=false) public void setStatus(int nStatus, @Nullable String sErrorMessage)
Deprecated.- Specified by:
setStatusin interfacejakarta.servlet.http.HttpServletResponse
-
getStatus
public int getStatus()
- Specified by:
getStatusin interfacejakarta.servlet.http.HttpServletResponse
-
setEncodeRedirectUrlSuffix
public void setEncodeRedirectUrlSuffix(@Nullable String sEncodeRedirectUrlSuffix)
-
setContentLengthLong
public void setContentLengthLong(long len)
- Specified by:
setContentLengthLongin interfacejakarta.servlet.ServletResponse
-
-