public class MockHttpServletRequest
extends java.lang.Object
javax.servlet.http.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 |
---|---|
private boolean |
active |
private MockAsyncContext |
asyncContext |
private boolean |
asyncStarted |
private boolean |
asyncSupported |
private java.util.Map<java.lang.String,java.lang.Object> |
attributes |
private java.lang.String |
authType |
private java.lang.String |
characterEncoding |
private static java.lang.String |
CHARSET_PREFIX |
private byte[] |
content |
private java.lang.String |
contentType |
private java.lang.String |
contextPath |
private Cookie[] |
cookies |
private static java.lang.String[] |
DATE_FORMATS
Date formats as specified in the HTTP RFC
|
static java.lang.String |
DEFAULT_PROTOCOL
The default protocol: 'HTTP/1.1'.
|
static java.lang.String |
DEFAULT_REMOTE_ADDR
The default remote address: '127.0.0.1'.
|
static java.lang.String |
DEFAULT_REMOTE_HOST
The default remote host: 'localhost'.
|
static java.lang.String |
DEFAULT_SCHEME
The default scheme: 'http'.
|
static java.lang.String |
DEFAULT_SERVER_ADDR
The default server address: '127.0.0.1'.
|
static java.lang.String |
DEFAULT_SERVER_NAME
The default server name: 'localhost'.
|
static int |
DEFAULT_SERVER_PORT
The default server port: '80'.
|
private DispatcherType |
dispatcherType |
private static java.io.BufferedReader |
EMPTY_BUFFERED_READER |
private static ServletInputStream |
EMPTY_SERVLET_INPUT_STREAM |
private static java.util.TimeZone |
GMT |
private java.util.Map<java.lang.String,HeaderValueHolder> |
headers |
private static java.lang.String |
HTTP |
private static java.lang.String |
HTTPS |
private java.lang.String |
localAddr |
private java.util.List<java.util.Locale> |
locales
List of locales in descending order
|
private java.lang.String |
localName |
private int |
localPort |
private java.lang.String |
method |
private java.util.Map<java.lang.String,java.lang.String[]> |
parameters |
private MultiValueMap<java.lang.String,Part> |
parts |
private java.lang.String |
pathInfo |
private java.lang.String |
protocol |
private java.lang.String |
queryString |
private java.lang.String |
remoteAddr |
private java.lang.String |
remoteHost |
private int |
remotePort |
private java.lang.String |
remoteUser |
private java.lang.String |
requestedSessionId |
private boolean |
requestedSessionIdFromCookie |
private boolean |
requestedSessionIdFromURL |
private boolean |
requestedSessionIdValid |
private java.lang.String |
requestURI |
private java.lang.String |
scheme |
private boolean |
secure |
private java.lang.String |
serverName |
private int |
serverPort |
private ServletContext |
servletContext |
private java.lang.String |
servletPath |
private HttpSession |
session |
private java.security.Principal |
userPrincipal |
private java.util.Set<java.lang.String> |
userRoles |
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,
java.lang.String method,
java.lang.String requestURI)
Create a new
MockHttpServletRequest with the supplied ServletContext ,
method , and requestURI . |
MockHttpServletRequest(java.lang.String method,
java.lang.String requestURI)
Create a new
MockHttpServletRequest with a default
MockServletContext . |
Modifier and Type | Method and Description |
---|---|
void |
addHeader(java.lang.String name,
java.lang.Object value)
Add a header entry for the given name.
|
void |
addParameter(java.lang.String name,
java.lang.String... values)
Add an array of values for the specified HTTP parameter.
|
void |
addParameter(java.lang.String name,
java.lang.String value)
Add a single value for the specified HTTP parameter.
|
void |
addParameters(java.util.Map<java.lang.String,?> params)
Adds all provided parameters without replacing any
existing values.
|
void |
addPart(Part part) |
void |
addPreferredLocale(java.util.Locale locale)
Add a new preferred locale, before any existing locales.
|
void |
addUserRole(java.lang.String role) |
boolean |
authenticate(HttpServletResponse response) |
java.lang.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.
|
private void |
doAddHeaderValue(java.lang.String name,
java.lang.Object value,
boolean replace) |
AsyncContext |
getAsyncContext() |
java.lang.Object |
getAttribute(java.lang.String name) |
java.util.Enumeration<java.lang.String> |
getAttributeNames() |
java.lang.String |
getAuthType() |
java.lang.String |
getCharacterEncoding() |
byte[] |
getContentAsByteArray()
Get the content of the request body as a byte array.
|
java.lang.String |
getContentAsString()
Get the content of the request body as a
String , using the configured
character encoding. |
int |
getContentLength() |
long |
getContentLengthLong() |
java.lang.String |
getContentType() |
java.lang.String |
getContextPath() |
Cookie[] |
getCookies() |
long |
getDateHeader(java.lang.String name)
Return the long timestamp for the date header with the given
name . |
DispatcherType |
getDispatcherType() |
java.lang.String |
getHeader(java.lang.String name) |
java.util.Enumeration<java.lang.String> |
getHeaderNames() |
java.util.Enumeration<java.lang.String> |
getHeaders(java.lang.String name) |
ServletInputStream |
getInputStream() |
int |
getIntHeader(java.lang.String name) |
java.lang.String |
getLocalAddr() |
java.util.Locale |
getLocale()
Return the first preferred locale configured
in this mock request.
|
java.util.Enumeration<java.util.Locale> |
getLocales()
Return an enumeration of the preferred
locales configured in this mock request.
|
java.lang.String |
getLocalName() |
int |
getLocalPort() |
java.lang.String |
getMethod() |
java.lang.String |
getParameter(java.lang.String name) |
java.util.Map<java.lang.String,java.lang.String[]> |
getParameterMap() |
java.util.Enumeration<java.lang.String> |
getParameterNames() |
java.lang.String[] |
getParameterValues(java.lang.String name) |
Part |
getPart(java.lang.String name) |
java.util.Collection<Part> |
getParts() |
java.lang.String |
getPathInfo() |
java.lang.String |
getPathTranslated() |
java.lang.String |
getProtocol() |
java.lang.String |
getQueryString() |
java.io.BufferedReader |
getReader() |
java.lang.String |
getRealPath(java.lang.String path)
Deprecated.
|
java.lang.String |
getRemoteAddr() |
java.lang.String |
getRemoteHost() |
int |
getRemotePort() |
java.lang.String |
getRemoteUser() |
RequestDispatcher |
getRequestDispatcher(java.lang.String path) |
java.lang.String |
getRequestedSessionId() |
java.lang.String |
getRequestURI() |
java.lang.StringBuffer |
getRequestURL() |
java.lang.String |
getScheme() |
java.lang.String |
getServerName() |
int |
getServerPort() |
ServletContext |
getServletContext()
Return the ServletContext that this request is associated with.
|
java.lang.String |
getServletPath() |
HttpSession |
getSession() |
HttpSession |
getSession(boolean create) |
java.security.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(java.lang.String role) |
void |
login(java.lang.String username,
java.lang.String password) |
void |
logout() |
private long |
parseDateHeader(java.lang.String name,
java.lang.String value) |
void |
removeAllParameters()
Removes all existing parameters.
|
void |
removeAttribute(java.lang.String name) |
void |
removeParameter(java.lang.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(java.lang.String name,
java.lang.Object value) |
void |
setAuthType(java.lang.String authType) |
void |
setCharacterEncoding(java.lang.String characterEncoding) |
void |
setContent(byte[] content)
Set the content of the request body as a byte array.
|
void |
setContentType(java.lang.String contentType) |
void |
setContextPath(java.lang.String contextPath) |
void |
setCookies(Cookie... cookies) |
void |
setDispatcherType(DispatcherType dispatcherType) |
void |
setLocalAddr(java.lang.String localAddr) |
void |
setLocalName(java.lang.String localName) |
void |
setLocalPort(int localPort) |
void |
setMethod(java.lang.String method) |
void |
setParameter(java.lang.String name,
java.lang.String... values)
Set an array of values for the specified HTTP parameter.
|
void |
setParameter(java.lang.String name,
java.lang.String value)
Set a single value for the specified HTTP parameter.
|
void |
setParameters(java.util.Map<java.lang.String,?> params)
Set all provided parameters replacing any existing
values for the provided parameter names.
|
void |
setPathInfo(java.lang.String pathInfo) |
void |
setPreferredLocales(java.util.List<java.util.Locale> locales)
Set the list of preferred locales, in descending order, effectively replacing
any existing locales.
|
void |
setProtocol(java.lang.String protocol) |
void |
setQueryString(java.lang.String queryString) |
void |
setRemoteAddr(java.lang.String remoteAddr) |
void |
setRemoteHost(java.lang.String remoteHost) |
void |
setRemotePort(int remotePort) |
void |
setRemoteUser(java.lang.String remoteUser) |
void |
setRequestedSessionId(java.lang.String requestedSessionId) |
void |
setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie) |
void |
setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL) |
void |
setRequestedSessionIdValid(boolean requestedSessionIdValid) |
void |
setRequestURI(java.lang.String requestURI) |
void |
setScheme(java.lang.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(java.lang.String serverName) |
void |
setServerPort(int serverPort) |
void |
setServletPath(java.lang.String servletPath) |
void |
setSession(HttpSession session) |
void |
setUserPrincipal(java.security.Principal userPrincipal) |
AsyncContext |
startAsync() |
AsyncContext |
startAsync(ServletRequest request,
ServletResponse response) |
private void |
updateAcceptLanguageHeader() |
private void |
updateContentTypeHeader() |
<T extends HttpUpgradeHandler> |
upgrade(java.lang.Class<T> handlerClass) |
private static final java.lang.String HTTP
private static final java.lang.String HTTPS
private static final java.lang.String CHARSET_PREFIX
private static final java.util.TimeZone GMT
private static final ServletInputStream EMPTY_SERVLET_INPUT_STREAM
private static final java.io.BufferedReader EMPTY_BUFFERED_READER
private static final java.lang.String[] DATE_FORMATS
public static final java.lang.String DEFAULT_PROTOCOL
public static final java.lang.String DEFAULT_SCHEME
public static final java.lang.String DEFAULT_SERVER_ADDR
public static final java.lang.String DEFAULT_SERVER_NAME
public static final int DEFAULT_SERVER_PORT
public static final java.lang.String DEFAULT_REMOTE_ADDR
public static final java.lang.String DEFAULT_REMOTE_HOST
private final ServletContext servletContext
private boolean active
private final java.util.Map<java.lang.String,java.lang.Object> attributes
@Nullable private java.lang.String characterEncoding
@Nullable private byte[] content
@Nullable private java.lang.String contentType
private final java.util.Map<java.lang.String,java.lang.String[]> parameters
private java.lang.String protocol
private java.lang.String scheme
private java.lang.String serverName
private int serverPort
private java.lang.String remoteAddr
private java.lang.String remoteHost
private final java.util.List<java.util.Locale> locales
private boolean secure
private int remotePort
private java.lang.String localName
private java.lang.String localAddr
private int localPort
private boolean asyncStarted
private boolean asyncSupported
@Nullable private MockAsyncContext asyncContext
private DispatcherType dispatcherType
@Nullable private java.lang.String authType
@Nullable private Cookie[] cookies
private final java.util.Map<java.lang.String,HeaderValueHolder> headers
@Nullable private java.lang.String method
@Nullable private java.lang.String pathInfo
private java.lang.String contextPath
@Nullable private java.lang.String queryString
@Nullable private java.lang.String remoteUser
private final java.util.Set<java.lang.String> userRoles
@Nullable private java.security.Principal userPrincipal
@Nullable private java.lang.String requestedSessionId
@Nullable private java.lang.String requestURI
private java.lang.String servletPath
@Nullable private HttpSession session
private boolean requestedSessionIdValid
private boolean requestedSessionIdFromCookie
private boolean requestedSessionIdFromURL
private final MultiValueMap<java.lang.String,Part> parts
public MockHttpServletRequest()
MockHttpServletRequest
with a default
MockServletContext
.public MockHttpServletRequest(@Nullable java.lang.String method, @Nullable java.lang.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 java.lang.String method, @Nullable java.lang.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>)
,
MockServletContext
public ServletContext getServletContext()
public boolean isActive()
public void close()
public void invalidate()
protected void checkActive() throws java.lang.IllegalStateException
java.lang.IllegalStateException
public java.lang.Object getAttribute(java.lang.String name)
public java.util.Enumeration<java.lang.String> getAttributeNames()
@Nullable public java.lang.String getCharacterEncoding()
public void setCharacterEncoding(@Nullable java.lang.String characterEncoding)
private void updateContentTypeHeader()
public 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 java.lang.String getContentAsString() throws java.lang.IllegalStateException, java.io.UnsupportedEncodingException
String
, using the configured
character encoding.String
, potentially null
java.lang.IllegalStateException
- if the character encoding has not been setjava.io.UnsupportedEncodingException
- if the character encoding is not supportedsetContent(byte[])
,
setCharacterEncoding(String)
,
getContentAsByteArray()
public int getContentLength()
public long getContentLengthLong()
public void setContentType(@Nullable java.lang.String contentType)
@Nullable public java.lang.String getContentType()
public ServletInputStream getInputStream()
public void setParameter(java.lang.String name, java.lang.String value)
If there are already one or more values registered for the given parameter name, they will be replaced.
public void setParameter(java.lang.String name, java.lang.String... values)
If there are already one or more values registered for the given parameter name, they will be replaced.
public void setParameters(java.util.Map<java.lang.String,?> params)
addParameters(java.util.Map)
.public void addParameter(java.lang.String name, @Nullable java.lang.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(java.lang.String name, java.lang.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(java.util.Map<java.lang.String,?> params)
setParameters(java.util.Map)
.public void removeParameter(java.lang.String name)
public void removeAllParameters()
@Nullable public java.lang.String getParameter(java.lang.String name)
public java.util.Enumeration<java.lang.String> getParameterNames()
public java.lang.String[] getParameterValues(java.lang.String name)
public java.util.Map<java.lang.String,java.lang.String[]> getParameterMap()
public void setProtocol(java.lang.String protocol)
public java.lang.String getProtocol()
public void setScheme(java.lang.String scheme)
public java.lang.String getScheme()
public void setServerName(java.lang.String serverName)
public java.lang.String getServerName()
public void setServerPort(int serverPort)
public int getServerPort()
public java.io.BufferedReader getReader() throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException
public void setRemoteAddr(java.lang.String remoteAddr)
public java.lang.String getRemoteAddr()
public void setRemoteHost(java.lang.String remoteHost)
public java.lang.String getRemoteHost()
public void setAttribute(java.lang.String name, @Nullable java.lang.Object value)
public void removeAttribute(java.lang.String name)
public void clearAttributes()
public void addPreferredLocale(java.util.Locale locale)
public void setPreferredLocales(java.util.List<java.util.Locale> locales)
addPreferredLocale(java.util.Locale)
private void updateAcceptLanguageHeader()
public java.util.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.
javax.servlet.ServletRequest#getLocale()
,
addPreferredLocale(Locale)
,
setPreferredLocales(List)
public java.util.Enumeration<java.util.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.
javax.servlet.ServletRequest#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()
javax.servlet.ServletRequest#isSecure()
public RequestDispatcher getRequestDispatcher(java.lang.String path)
@Deprecated public java.lang.String getRealPath(java.lang.String path)
public void setRemotePort(int remotePort)
public int getRemotePort()
public void setLocalName(java.lang.String localName)
public java.lang.String getLocalName()
public void setLocalAddr(java.lang.String localAddr)
public java.lang.String getLocalAddr()
public void setLocalPort(int localPort)
public int getLocalPort()
public AsyncContext startAsync()
public AsyncContext startAsync(ServletRequest request, @Nullable ServletResponse response)
public void setAsyncStarted(boolean asyncStarted)
public boolean isAsyncStarted()
public void setAsyncSupported(boolean asyncSupported)
public boolean isAsyncSupported()
public void setAsyncContext(@Nullable MockAsyncContext asyncContext)
@Nullable public AsyncContext getAsyncContext()
public void setDispatcherType(DispatcherType dispatcherType)
public DispatcherType getDispatcherType()
public void setAuthType(@Nullable java.lang.String authType)
@Nullable public java.lang.String getAuthType()
public void setCookies(@Nullable Cookie... cookies)
@Nullable public Cookie[] getCookies()
public void addHeader(java.lang.String name, java.lang.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)
.private void doAddHeaderValue(java.lang.String name, @Nullable java.lang.Object value, boolean replace)
public long getDateHeader(java.lang.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:
name
- the header nameprivate long parseDateHeader(java.lang.String name, java.lang.String value)
@Nullable public java.lang.String getHeader(java.lang.String name)
public java.util.Enumeration<java.lang.String> getHeaders(java.lang.String name)
public java.util.Enumeration<java.lang.String> getHeaderNames()
public int getIntHeader(java.lang.String name)
public void setMethod(@Nullable java.lang.String method)
@Nullable public java.lang.String getMethod()
public void setPathInfo(@Nullable java.lang.String pathInfo)
@Nullable public java.lang.String getPathInfo()
@Nullable public java.lang.String getPathTranslated()
public void setContextPath(java.lang.String contextPath)
public java.lang.String getContextPath()
public void setQueryString(@Nullable java.lang.String queryString)
@Nullable public java.lang.String getQueryString()
public void setRemoteUser(@Nullable java.lang.String remoteUser)
@Nullable public java.lang.String getRemoteUser()
public void addUserRole(java.lang.String role)
public boolean isUserInRole(java.lang.String role)
public void setUserPrincipal(@Nullable java.security.Principal userPrincipal)
@Nullable public java.security.Principal getUserPrincipal()
public void setRequestedSessionId(@Nullable java.lang.String requestedSessionId)
@Nullable public java.lang.String getRequestedSessionId()
public void setRequestURI(@Nullable java.lang.String requestURI)
@Nullable public java.lang.String getRequestURI()
public java.lang.StringBuffer getRequestURL()
public void setServletPath(java.lang.String servletPath)
public java.lang.String getServletPath()
public void setSession(HttpSession session)
@Nullable public HttpSession getSession(boolean create)
@Nullable public HttpSession getSession()
public java.lang.String changeSessionId()
MockHttpSession.changeSessionId()
if the session is a mock session.
Otherwise it simply returns the current session id.public void setRequestedSessionIdValid(boolean requestedSessionIdValid)
public boolean isRequestedSessionIdValid()
public void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie)
public boolean isRequestedSessionIdFromCookie()
public void setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL)
public boolean isRequestedSessionIdFromURL()
@Deprecated public boolean isRequestedSessionIdFromUrl()
public boolean authenticate(HttpServletResponse response) throws java.io.IOException, ServletException
java.io.IOException
ServletException
public void login(java.lang.String username, java.lang.String password) throws ServletException
ServletException
public void logout() throws ServletException
ServletException
public void addPart(Part part)
@Nullable public Part getPart(java.lang.String name) throws java.io.IOException, ServletException
java.io.IOException
ServletException
public java.util.Collection<Part> getParts() throws java.io.IOException, ServletException
java.io.IOException
ServletException
public <T extends HttpUpgradeHandler> T upgrade(java.lang.Class<T> handlerClass) throws java.io.IOException, ServletException
java.io.IOException
ServletException