public class MockHttpServletRequest extends Object implements HttpServletRequest
HttpServletRequest interface.
The default, preferred Locale for the server mocked by this request
is Locale.ENGLISH. This value can be changed via addPreferredLocale(java.util.Locale)
or setPreferredLocales(java.util.List<java.util.Locale>).
As of Spring Framework 5.0, this set of mocks is designed on a Servlet 4.0 baseline.
| Modifier and Type | Field and Description |
|---|---|
static String |
DEFAULT_PROTOCOL
The default protocol: 'HTTP/1.1'.
|
static String |
DEFAULT_REMOTE_ADDR
The default remote address: '127.0.0.1'.
|
static String |
DEFAULT_REMOTE_HOST
The default remote host: 'localhost'.
|
static String |
DEFAULT_SCHEME
The default scheme: 'http'.
|
static String |
DEFAULT_SERVER_ADDR
The default server address: '127.0.0.1'.
|
static String |
DEFAULT_SERVER_NAME
The default server name: 'localhost'.
|
static int |
DEFAULT_SERVER_PORT
The default server port: '80'.
|
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH| Constructor and Description |
|---|
MockHttpServletRequest()
Create a new
MockHttpServletRequest with a default
MockServletContext. |
MockHttpServletRequest(ServletContext servletContext)
Create a new
MockHttpServletRequest with the supplied ServletContext. |
MockHttpServletRequest(ServletContext servletContext,
String method,
String requestURI)
|
MockHttpServletRequest(String method,
String requestURI)
Create a new
MockHttpServletRequest with a default
MockServletContext. |
| Modifier and Type | Method and Description |
|---|---|
void |
addHeader(String name,
Object value)
Add an HTTP header entry for the given name.
|
void |
addParameter(String name,
String... values)
Add an array of values for the specified HTTP parameter.
|
void |
addParameter(String name,
String value)
Add a single value for the specified HTTP parameter.
|
void |
addParameters(Map<String,?> params)
Add all provided parameters without replacing any
existing values.
|
void |
addPart(Part part) |
void |
addPreferredLocale(Locale locale)
Add a new preferred locale, before any existing locales.
|
void |
addUserRole(String role) |
boolean |
authenticate(HttpServletResponse response) |
String |
changeSessionId()
The implementation of this (Servlet 3.1+) method calls
MockHttpSession.changeSessionId() if the session is a mock session. |
protected void |
checkActive()
Check whether this request is still active (that is, not completed yet),
throwing an IllegalStateException if not active anymore.
|
void |
clearAttributes()
Clear all of this request's attributes.
|
void |
close()
Mark this request as completed, keeping its state.
|
AsyncContext |
getAsyncContext() |
Object |
getAttribute(String name) |
Enumeration<String> |
getAttributeNames() |
String |
getAuthType() |
String |
getCharacterEncoding() |
byte[] |
getContentAsByteArray()
Get the content of the request body as a byte array.
|
String |
getContentAsString()
Get the content of the request body as a
String, using the configured
character encoding. |
int |
getContentLength() |
long |
getContentLengthLong() |
String |
getContentType() |
String |
getContextPath() |
Cookie[] |
getCookies() |
long |
getDateHeader(String name)
Return the long timestamp for the date header with the given
name. |
DispatcherType |
getDispatcherType() |
String |
getHeader(String name) |
Enumeration<String> |
getHeaderNames() |
Enumeration<String> |
getHeaders(String name) |
ServletInputStream |
getInputStream() |
int |
getIntHeader(String name) |
String |
getLocalAddr() |
Locale |
getLocale()
Return the first preferred locale configured
in this mock request.
|
Enumeration<Locale> |
getLocales()
Return an enumeration of the preferred
locales configured in this mock request.
|
String |
getLocalName() |
int |
getLocalPort() |
String |
getMethod() |
String |
getParameter(String name) |
Map<String,String[]> |
getParameterMap() |
Enumeration<String> |
getParameterNames() |
String[] |
getParameterValues(String name) |
Part |
getPart(String name) |
Collection<Part> |
getParts() |
String |
getPathInfo() |
String |
getPathTranslated() |
String |
getProtocol() |
String |
getQueryString() |
BufferedReader |
getReader() |
String |
getRealPath(String path)
Deprecated.
|
String |
getRemoteAddr() |
String |
getRemoteHost() |
int |
getRemotePort() |
String |
getRemoteUser() |
RequestDispatcher |
getRequestDispatcher(String path) |
String |
getRequestedSessionId() |
String |
getRequestURI() |
StringBuffer |
getRequestURL() |
String |
getScheme() |
String |
getServerName() |
int |
getServerPort() |
ServletContext |
getServletContext()
Return the ServletContext that this request is associated with.
|
String |
getServletPath() |
HttpSession |
getSession() |
HttpSession |
getSession(boolean create) |
Principal |
getUserPrincipal() |
void |
invalidate()
Invalidate this request, clearing its state.
|
boolean |
isActive()
Return whether this request is still active (that is, not completed yet).
|
boolean |
isAsyncStarted() |
boolean |
isAsyncSupported() |
boolean |
isRequestedSessionIdFromCookie() |
boolean |
isRequestedSessionIdFromUrl()
Deprecated.
|
boolean |
isRequestedSessionIdFromURL() |
boolean |
isRequestedSessionIdValid() |
boolean |
isSecure()
|
boolean |
isUserInRole(String role) |
void |
login(String username,
String password) |
void |
logout() |
void |
removeAllParameters()
Remove all existing parameters.
|
void |
removeAttribute(String name) |
void |
removeHeader(String name)
Remove already registered entries for the specified HTTP header, if any.
|
void |
removeParameter(String name)
Remove already registered values for the specified HTTP parameter, if any.
|
void |
setAsyncContext(MockAsyncContext asyncContext) |
void |
setAsyncStarted(boolean asyncStarted) |
void |
setAsyncSupported(boolean asyncSupported) |
void |
setAttribute(String name,
Object value) |
void |
setAuthType(String authType) |
void |
setCharacterEncoding(String characterEncoding) |
void |
setContent(byte[] content)
Set the content of the request body as a byte array.
|
void |
setContentType(String contentType) |
void |
setContextPath(String contextPath) |
void |
setCookies(Cookie... cookies) |
void |
setDispatcherType(DispatcherType dispatcherType) |
void |
setLocalAddr(String localAddr) |
void |
setLocalName(String localName) |
void |
setLocalPort(int localPort) |
void |
setMethod(String method) |
void |
setParameter(String name,
String... values)
Set an array of values for the specified HTTP parameter.
|
void |
setParameter(String name,
String value)
Set a single value for the specified HTTP parameter.
|
void |
setParameters(Map<String,?> params)
Set all provided parameters replacing any existing
values for the provided parameter names.
|
void |
setPathInfo(String pathInfo) |
void |
setPreferredLocales(List<Locale> locales)
Set the list of preferred locales, in descending order, effectively replacing
any existing locales.
|
void |
setProtocol(String protocol) |
void |
setQueryString(String queryString) |
void |
setRemoteAddr(String remoteAddr) |
void |
setRemoteHost(String remoteHost) |
void |
setRemotePort(int remotePort) |
void |
setRemoteUser(String remoteUser) |
void |
setRequestedSessionId(String requestedSessionId) |
void |
setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie) |
void |
setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL) |
void |
setRequestedSessionIdValid(boolean requestedSessionIdValid) |
void |
setRequestURI(String requestURI) |
void |
setScheme(String scheme) |
void |
setSecure(boolean secure)
Set the boolean
secure flag indicating whether the mock request
was made using a secure channel, such as HTTPS. |
void |
setServerName(String serverName) |
void |
setServerPort(int serverPort) |
void |
setServletPath(String servletPath) |
void |
setSession(HttpSession session) |
void |
setUserPrincipal(Principal userPrincipal) |
AsyncContext |
startAsync() |
AsyncContext |
startAsync(ServletRequest request,
ServletResponse response) |
<T extends HttpUpgradeHandler> |
upgrade(Class<T> handlerClass) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetHttpServletMapping, getTrailerFields, isTrailerFieldsReady, newPushBuilderpublic static final String DEFAULT_PROTOCOL
public static final String DEFAULT_SCHEME
public static final String DEFAULT_SERVER_ADDR
public static final String DEFAULT_SERVER_NAME
public static final int DEFAULT_SERVER_PORT
public static final String DEFAULT_REMOTE_ADDR
public static final String DEFAULT_REMOTE_HOST
public MockHttpServletRequest()
MockHttpServletRequest with a default
MockServletContext.public MockHttpServletRequest(@Nullable String method, @Nullable String requestURI)
MockHttpServletRequest with a default
MockServletContext.method - the request method (may be null)requestURI - the request URI (may be null)setMethod(java.lang.String),
setRequestURI(java.lang.String),
MockHttpServletRequest(ServletContext, String, String)public MockHttpServletRequest(@Nullable ServletContext servletContext)
MockHttpServletRequest with the supplied ServletContext.servletContext - the ServletContext that the request runs in
(may be null to use a default MockServletContext)MockHttpServletRequest(ServletContext, String, String)public MockHttpServletRequest(@Nullable ServletContext servletContext, @Nullable String method, @Nullable String requestURI)
MockHttpServletRequest with the supplied ServletContext,
method, and requestURI.
The preferred locale will be set to Locale.ENGLISH.
servletContext - the ServletContext that the request runs in (may be
null to use a default MockServletContext)method - the request method (may be null)requestURI - the request URI (may be null)setMethod(java.lang.String),
setRequestURI(java.lang.String),
setPreferredLocales(java.util.List<java.util.Locale>),
MockServletContextpublic ServletContext getServletContext()
getServletContext in interface ServletRequestpublic boolean isActive()
public void close()
public void invalidate()
protected void checkActive()
throws IllegalStateException
IllegalStateExceptionpublic Object getAttribute(String name)
getAttribute in interface ServletRequestpublic Enumeration<String> getAttributeNames()
getAttributeNames in interface ServletRequest@Nullable public String getCharacterEncoding()
getCharacterEncoding in interface ServletRequestpublic void setCharacterEncoding(@Nullable String characterEncoding)
setCharacterEncoding in interface ServletRequestpublic void setContent(@Nullable byte[] content)
If the supplied byte array represents text such as XML or JSON, the
character encoding should typically be
set as well.
@Nullable public byte[] getContentAsByteArray()
null)setContent(byte[]),
getContentAsString()@Nullable public String getContentAsString() throws IllegalStateException, UnsupportedEncodingException
String, using the configured
character encoding.String, potentially nullIllegalStateException - if the character encoding has not been setUnsupportedEncodingException - if the character encoding is not supportedsetContent(byte[]),
setCharacterEncoding(String),
getContentAsByteArray()public int getContentLength()
getContentLength in interface ServletRequestpublic long getContentLengthLong()
getContentLengthLong in interface ServletRequest@Nullable public String getContentType()
getContentType in interface ServletRequestpublic ServletInputStream getInputStream()
getInputStream in interface ServletRequestpublic void setParameter(String name, String value)
If there are already one or more values registered for the given parameter name, they will be replaced.
public void setParameter(String name, String... values)
If there are already one or more values registered for the given parameter name, they will be replaced.
public void setParameters(Map<String,?> params)
addParameters(java.util.Map).public void addParameter(String name, @Nullable String value)
If there are already one or more values registered for the given parameter name, the given value will be added to the end of the list.
public void addParameter(String name, String... values)
If there are already one or more values registered for the given parameter name, the given values will be added to the end of the list.
public void addParameters(Map<String,?> params)
setParameters(java.util.Map).public void removeParameter(String name)
public void removeAllParameters()
@Nullable public String getParameter(String name)
getParameter in interface ServletRequestpublic Enumeration<String> getParameterNames()
getParameterNames in interface ServletRequestpublic String[] getParameterValues(String name)
getParameterValues in interface ServletRequestpublic Map<String,String[]> getParameterMap()
getParameterMap in interface ServletRequestpublic void setProtocol(String protocol)
public String getProtocol()
getProtocol in interface ServletRequestpublic void setScheme(String scheme)
public String getScheme()
getScheme in interface ServletRequestpublic void setServerName(String serverName)
public String getServerName()
getServerName in interface ServletRequestpublic void setServerPort(int serverPort)
public int getServerPort()
getServerPort in interface ServletRequestpublic BufferedReader getReader() throws UnsupportedEncodingException
getReader in interface ServletRequestUnsupportedEncodingExceptionpublic void setRemoteAddr(String remoteAddr)
public String getRemoteAddr()
getRemoteAddr in interface ServletRequestpublic void setRemoteHost(String remoteHost)
public String getRemoteHost()
getRemoteHost in interface ServletRequestpublic void setAttribute(String name, @Nullable Object value)
setAttribute in interface ServletRequestpublic void removeAttribute(String name)
removeAttribute in interface ServletRequestpublic void clearAttributes()
public void addPreferredLocale(Locale locale)
public void setPreferredLocales(List<Locale> locales)
addPreferredLocale(java.util.Locale)public Locale getLocale()
If no locales have been explicitly configured, the default,
preferred Locale for the server mocked by this
request is Locale.ENGLISH.
In contrast to the Servlet specification, this mock implementation
does not take into consideration any locales
specified via the Accept-Language header.
getLocale in interface ServletRequestServletRequest.getLocale(),
addPreferredLocale(Locale),
setPreferredLocales(List)public Enumeration<Locale> getLocales()
If no locales have been explicitly configured, the default,
preferred Locale for the server mocked by this
request is Locale.ENGLISH.
In contrast to the Servlet specification, this mock implementation
does not take into consideration any locales
specified via the Accept-Language header.
getLocales in interface ServletRequestServletRequest.getLocales(),
addPreferredLocale(Locale),
setPreferredLocales(List)public void setSecure(boolean secure)
secure flag indicating whether the mock request
was made using a secure channel, such as HTTPS.isSecure(),
getScheme(),
setScheme(String)public boolean isSecure()
isSecure in interface ServletRequestServletRequest.isSecure()public RequestDispatcher getRequestDispatcher(String path)
getRequestDispatcher in interface ServletRequest@Deprecated public String getRealPath(String path)
getRealPath in interface ServletRequestpublic void setRemotePort(int remotePort)
public int getRemotePort()
getRemotePort in interface ServletRequestpublic void setLocalName(String localName)
public String getLocalName()
getLocalName in interface ServletRequestpublic void setLocalAddr(String localAddr)
public String getLocalAddr()
getLocalAddr in interface ServletRequestpublic void setLocalPort(int localPort)
public int getLocalPort()
getLocalPort in interface ServletRequestpublic AsyncContext startAsync()
startAsync in interface ServletRequestpublic AsyncContext startAsync(ServletRequest request, @Nullable ServletResponse response)
startAsync in interface ServletRequestpublic void setAsyncStarted(boolean asyncStarted)
public boolean isAsyncStarted()
isAsyncStarted in interface ServletRequestpublic void setAsyncSupported(boolean asyncSupported)
public boolean isAsyncSupported()
isAsyncSupported in interface ServletRequestpublic void setAsyncContext(@Nullable MockAsyncContext asyncContext)
@Nullable public AsyncContext getAsyncContext()
getAsyncContext in interface ServletRequestpublic void setDispatcherType(DispatcherType dispatcherType)
public DispatcherType getDispatcherType()
getDispatcherType in interface ServletRequest@Nullable public String getAuthType()
getAuthType in interface HttpServletRequest@Nullable public Cookie[] getCookies()
getCookies in interface HttpServletRequestpublic void addHeader(String name, Object value)
While this method can take any Object as a parameter,
it is recommended to use the following types:
toString(); see getHeader(java.lang.String).getDateHeader(java.lang.String).getIntHeader(java.lang.String).String[] or Collection<String> for multiple values; see getHeaders(java.lang.String).public void removeHeader(String name)
public long getDateHeader(String name)
name.
If the internal value representation is a String, this method will try to parse it as a date using the supported date formats:
getDateHeader in interface HttpServletRequestname - the header name@Nullable public String getHeader(String name)
getHeader in interface HttpServletRequestpublic Enumeration<String> getHeaders(String name)
getHeaders in interface HttpServletRequestpublic Enumeration<String> getHeaderNames()
getHeaderNames in interface HttpServletRequestpublic int getIntHeader(String name)
getIntHeader in interface HttpServletRequest@Nullable public String getMethod()
getMethod in interface HttpServletRequest@Nullable public String getPathInfo()
getPathInfo in interface HttpServletRequest@Nullable public String getPathTranslated()
getPathTranslated in interface HttpServletRequestpublic void setContextPath(String contextPath)
public String getContextPath()
getContextPath in interface HttpServletRequest@Nullable public String getQueryString()
getQueryString in interface HttpServletRequest@Nullable public String getRemoteUser()
getRemoteUser in interface HttpServletRequestpublic void addUserRole(String role)
public boolean isUserInRole(String role)
isUserInRole in interface HttpServletRequest@Nullable public Principal getUserPrincipal()
getUserPrincipal in interface HttpServletRequest@Nullable public String getRequestedSessionId()
getRequestedSessionId in interface HttpServletRequest@Nullable public String getRequestURI()
getRequestURI in interface HttpServletRequestpublic StringBuffer getRequestURL()
getRequestURL in interface HttpServletRequestpublic void setServletPath(String servletPath)
public String getServletPath()
getServletPath in interface HttpServletRequestpublic void setSession(HttpSession session)
@Nullable public HttpSession getSession(boolean create)
getSession in interface HttpServletRequest@Nullable public HttpSession getSession()
getSession in interface HttpServletRequestpublic String changeSessionId()
MockHttpSession.changeSessionId() if the session is a mock session.
Otherwise it simply returns the current session id.changeSessionId in interface HttpServletRequestpublic void setRequestedSessionIdValid(boolean requestedSessionIdValid)
public boolean isRequestedSessionIdValid()
isRequestedSessionIdValid in interface HttpServletRequestpublic void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie)
public boolean isRequestedSessionIdFromCookie()
isRequestedSessionIdFromCookie in interface HttpServletRequestpublic void setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL)
public boolean isRequestedSessionIdFromURL()
isRequestedSessionIdFromURL in interface HttpServletRequest@Deprecated public boolean isRequestedSessionIdFromUrl()
isRequestedSessionIdFromUrl in interface HttpServletRequestpublic boolean authenticate(HttpServletResponse response) throws IOException, ServletException
authenticate in interface HttpServletRequestIOExceptionServletExceptionpublic void login(String username, String password) throws ServletException
login in interface HttpServletRequestServletExceptionpublic void logout()
throws ServletException
logout in interface HttpServletRequestServletExceptionpublic void addPart(Part part)
@Nullable public Part getPart(String name) throws IOException, ServletException
getPart in interface HttpServletRequestIOExceptionServletExceptionpublic Collection<Part> getParts() throws IOException, ServletException
getParts in interface HttpServletRequestIOExceptionServletExceptionpublic <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException
upgrade in interface HttpServletRequestIOExceptionServletException