Class MockHttpServletRequest
- All Implemented Interfaces:
jakarta.servlet.http.HttpServletRequest, jakarta.servlet.ServletRequest
- Direct Known Subclasses:
MockMultipartHttpServletRequest
HttpServletRequest
interface.
The default, preferred Locale
for the server mocked by this request
is Locale.ENGLISH
. This value can be changed via addPreferredLocale(Locale)
or setPreferredLocales(List)
.
As of Spring 6.0, this set of mocks is designed on a Servlet 6.0 baseline.
- Since:
- 1.0.2
- Author:
- Juergen Hoeller, Rod Johnson, Rick Evans, Mark Fisher, Chris Beams, Sam Brannen, Brian Clozel
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The default protocol: 'HTTP/1.1'.static final String
The default remote address: '127.0.0.1'.static final String
The default remote host: 'localhost'.static final String
The default scheme: 'http'.static final String
The default server address: '127.0.0.1'.static final String
The default server name: 'localhost'.static final int
The default server port: '80'.Fields inherited from interface jakarta.servlet.http.HttpServletRequest
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a newMockHttpServletRequest
with a defaultMockServletContext
.MockHttpServletRequest
(@Nullable jakarta.servlet.ServletContext servletContext) Create a newMockHttpServletRequest
with the suppliedServletContext
.MockHttpServletRequest
(@Nullable jakarta.servlet.ServletContext servletContext, @Nullable String method, @Nullable String requestURI) Create a newMockHttpServletRequest
with the suppliedServletContext
,method
, andrequestURI
.MockHttpServletRequest
(@Nullable String method, @Nullable String requestURI) Create a newMockHttpServletRequest
with a defaultMockServletContext
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Add an HTTP header entry for the given name.void
addParameter
(String name, @Nullable String value) Add a single value for the specified HTTP parameter.void
addParameter
(String name, String... values) Add an array of values for the specified HTTP parameter.void
addParameters
(Map<String, ?> params) Add all provided parameters without replacing any existing values.void
addPart
(jakarta.servlet.http.Part part) void
addPreferredLocale
(Locale locale) Add a new preferred locale, before any existing locales.void
addUserRole
(String role) boolean
authenticate
(jakarta.servlet.http.HttpServletResponse response) The implementation of this (Servlet 3.1+) method callsMockHttpSession.changeSessionId()
if the session is a mock session.protected void
Check whether this request is still active (that is, not completed yet), throwing an IllegalStateException if not active anymore.void
Clear all of this request's attributes.void
close()
Mark this request as completed, keeping its state.@Nullable jakarta.servlet.AsyncContext
getAttribute
(String name) byte @Nullable []
Get the content of the request body as a byte array.Get the content of the request body as aString
, using the configured character encoding.int
long
jakarta.servlet.http.Cookie @Nullable []
long
getDateHeader
(String name) Return the long timestamp for the date header with the givenname
.jakarta.servlet.DispatcherType
getHeaders
(String name) jakarta.servlet.http.HttpServletMapping
jakarta.servlet.ServletInputStream
int
getIntHeader
(String name) Return the first preferred locale configured in this mock request.Return an enumeration of the preferred locales configured in this mock request.int
getParameter
(String name) getParameterValues
(String name) @Nullable jakarta.servlet.http.Part
Collection
<jakarta.servlet.http.Part> getParts()
int
jakarta.servlet.RequestDispatcher
getRequestDispatcher
(String path) int
jakarta.servlet.ServletConnection
jakarta.servlet.ServletContext
Return the ServletContext that this request is associated with.@Nullable jakarta.servlet.http.HttpSession
@Nullable jakarta.servlet.http.HttpSession
getSession
(boolean create) Return the original URI template used to prepare the request, if any.void
Invalidate this request, clearing its state.boolean
isActive()
Return whether this request is still active (that is, not completed yet).boolean
boolean
boolean
boolean
boolean
boolean
isSecure()
boolean
isUserInRole
(String role) void
void
logout()
void
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
(@Nullable MockAsyncContext asyncContext) void
setAsyncStarted
(boolean asyncStarted) void
setAsyncSupported
(boolean asyncSupported) void
setAttribute
(String name, @Nullable Object value) void
setAuthType
(@Nullable String authType) void
setCharacterEncoding
(@Nullable String characterEncoding) void
setContent
(byte @Nullable [] content) Set the content of the request body as a byte array.void
setContentType
(@Nullable String contentType) void
setContextPath
(String contextPath) void
setCookies
(jakarta.servlet.http.Cookie @Nullable ... cookies) void
setDispatcherType
(jakarta.servlet.DispatcherType dispatcherType) void
setHttpServletMapping
(@Nullable jakarta.servlet.http.HttpServletMapping httpServletMapping) void
setLocalAddr
(String localAddr) void
setLocalName
(String localName) void
setLocalPort
(int localPort) void
void
setParameter
(String name, String value) Set a single value for the specified HTTP parameter.void
setParameter
(String name, String... values) Set an array of values 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
(@Nullable 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
(@Nullable String queryString) void
setRemoteAddr
(String remoteAddr) void
setRemoteHost
(String remoteHost) void
setRemotePort
(int remotePort) void
setRemoteUser
(@Nullable String remoteUser) void
setRequestedSessionId
(@Nullable String requestedSessionId) void
setRequestedSessionIdFromCookie
(boolean requestedSessionIdFromCookie) void
setRequestedSessionIdFromURL
(boolean requestedSessionIdFromURL) void
setRequestedSessionIdValid
(boolean requestedSessionIdValid) void
setRequestURI
(@Nullable String requestURI) void
void
setSecure
(boolean secure) Set the booleansecure
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
(jakarta.servlet.http.HttpSession session) void
setUriTemplate
(@Nullable String uriTemplate) Set the original URI template used to prepare the request, if any.void
setUserPrincipal
(@Nullable Principal userPrincipal) jakarta.servlet.AsyncContext
jakarta.servlet.AsyncContext
startAsync
(jakarta.servlet.ServletRequest request, @Nullable jakarta.servlet.ServletResponse response) <T extends jakarta.servlet.http.HttpUpgradeHandler>
TMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface jakarta.servlet.http.HttpServletRequest
getTrailerFields, isTrailerFieldsReady, newPushBuilder
Methods inherited from interface jakarta.servlet.ServletRequest
setCharacterEncoding
-
Field Details
-
DEFAULT_PROTOCOL
The default protocol: 'HTTP/1.1'.- Since:
- 4.3.7
- See Also:
-
DEFAULT_SCHEME
-
DEFAULT_SERVER_ADDR
-
DEFAULT_SERVER_NAME
-
DEFAULT_SERVER_PORT
public static final int DEFAULT_SERVER_PORTThe default server port: '80'.- See Also:
-
DEFAULT_REMOTE_ADDR
-
DEFAULT_REMOTE_HOST
-
-
Constructor Details
-
MockHttpServletRequest
public MockHttpServletRequest()Create a newMockHttpServletRequest
with a defaultMockServletContext
.- See Also:
-
MockHttpServletRequest
Create a newMockHttpServletRequest
with a defaultMockServletContext
.- Parameters:
method
- the request method (may benull
)requestURI
- the request URI (may benull
)- See Also:
-
MockHttpServletRequest
Create a newMockHttpServletRequest
with the suppliedServletContext
.- Parameters:
servletContext
- the ServletContext that the request runs in (may benull
to use a defaultMockServletContext
)- See Also:
-
MockHttpServletRequest
public MockHttpServletRequest(@Nullable jakarta.servlet.ServletContext servletContext, @Nullable String method, @Nullable String requestURI) Create a newMockHttpServletRequest
with the suppliedServletContext
,method
, andrequestURI
.The preferred locale will be set to
Locale.ENGLISH
.- Parameters:
servletContext
- the ServletContext that the request runs in (may benull
to use a defaultMockServletContext
)method
- the request method (may benull
)requestURI
- the request URI (may benull
)- See Also:
-
-
Method Details
-
getServletContext
public jakarta.servlet.ServletContext getServletContext()Return the ServletContext that this request is associated with. (Not available in the standard HttpServletRequest interface for some reason.)- Specified by:
getServletContext
in interfacejakarta.servlet.ServletRequest
-
isActive
public boolean isActive()Return whether this request is still active (that is, not completed yet). -
close
public void close()Mark this request as completed, keeping its state. -
invalidate
public void invalidate()Invalidate this request, clearing its state. -
checkActive
Check whether this request is still active (that is, not completed yet), throwing an IllegalStateException if not active anymore.- Throws:
IllegalStateException
-
getAttribute
-
getAttributeNames
- Specified by:
getAttributeNames
in interfacejakarta.servlet.ServletRequest
-
getCharacterEncoding
-
setCharacterEncoding
-
setContent
Set the content of the request body as a byte array.If the supplied byte array represents text such as XML or JSON, the
character encoding
should typically be set as well.- See Also:
-
getContentAsByteArray
Get the content of the request body as a byte array.- Returns:
- the content as a byte array (potentially
null
) - Since:
- 5.0
- See Also:
-
getContentAsString
public @Nullable String getContentAsString() throws IllegalStateException, UnsupportedEncodingExceptionGet the content of the request body as aString
, using the configured character encoding.- Returns:
- the content as a
String
, potentiallynull
- Throws:
IllegalStateException
- if the character encoding has not been setUnsupportedEncodingException
- if the character encoding is not supported- Since:
- 5.0
- See Also:
-
getContentLength
public int getContentLength()- Specified by:
getContentLength
in interfacejakarta.servlet.ServletRequest
-
getContentLengthLong
public long getContentLengthLong()- Specified by:
getContentLengthLong
in interfacejakarta.servlet.ServletRequest
-
setContentType
-
getContentType
-
getInputStream
public jakarta.servlet.ServletInputStream getInputStream()- Specified by:
getInputStream
in interfacejakarta.servlet.ServletRequest
-
setParameter
-
setParameter
-
setParameters
Set all provided parameters replacing any existing values for the provided parameter names. To add without replacing existing values, useaddParameters(java.util.Map)
. -
addParameter
-
addParameter
-
addParameters
Add all provided parameters without replacing any existing values. To replace existing values, usesetParameters(java.util.Map)
. -
removeParameter
Remove already registered values for the specified HTTP parameter, if any. -
removeAllParameters
public void removeAllParameters()Remove all existing parameters. -
getParameter
-
getParameterNames
- Specified by:
getParameterNames
in interfacejakarta.servlet.ServletRequest
-
getParameterValues
-
getParameterMap
-
setProtocol
-
getProtocol
- Specified by:
getProtocol
in interfacejakarta.servlet.ServletRequest
-
setScheme
-
getScheme
- Specified by:
getScheme
in interfacejakarta.servlet.ServletRequest
-
setServerName
-
getServerName
- Specified by:
getServerName
in interfacejakarta.servlet.ServletRequest
-
setServerPort
public void setServerPort(int serverPort) -
getServerPort
public int getServerPort()- Specified by:
getServerPort
in interfacejakarta.servlet.ServletRequest
-
getReader
- Specified by:
getReader
in interfacejakarta.servlet.ServletRequest
- Throws:
UnsupportedEncodingException
-
setRemoteAddr
-
getRemoteAddr
- Specified by:
getRemoteAddr
in interfacejakarta.servlet.ServletRequest
-
setRemoteHost
-
getRemoteHost
- Specified by:
getRemoteHost
in interfacejakarta.servlet.ServletRequest
-
setAttribute
-
removeAttribute
- Specified by:
removeAttribute
in interfacejakarta.servlet.ServletRequest
-
clearAttributes
public void clearAttributes()Clear all of this request's attributes. -
addPreferredLocale
Add a new preferred locale, before any existing locales.- See Also:
-
setPreferredLocales
-
getLocale
Return the first preferred locale configured in this mock request.If no locales have been explicitly configured, the default, preferred
Locale
for the server mocked by this request isLocale.ENGLISH
.In contrast to the Servlet specification, this mock implementation does not take into consideration any locales specified via the
Accept-Language
header.- Specified by:
getLocale
in interfacejakarta.servlet.ServletRequest
- See Also:
-
getLocales
Return an enumeration of the preferred locales configured in this mock request.If no locales have been explicitly configured, the default, preferred
Locale
for the server mocked by this request isLocale.ENGLISH
.In contrast to the Servlet specification, this mock implementation does not take into consideration any locales specified via the
Accept-Language
header.- Specified by:
getLocales
in interfacejakarta.servlet.ServletRequest
- See Also:
-
setSecure
public void setSecure(boolean secure) Set the booleansecure
flag indicating whether the mock request was made using a secure channel, such as HTTPS.- See Also:
-
isSecure
-
getRequestDispatcher
- Specified by:
getRequestDispatcher
in interfacejakarta.servlet.ServletRequest
-
setRemotePort
public void setRemotePort(int remotePort) -
getRemotePort
public int getRemotePort()- Specified by:
getRemotePort
in interfacejakarta.servlet.ServletRequest
-
setLocalName
-
getLocalName
- Specified by:
getLocalName
in interfacejakarta.servlet.ServletRequest
-
setLocalAddr
-
getLocalAddr
- Specified by:
getLocalAddr
in interfacejakarta.servlet.ServletRequest
-
setLocalPort
public void setLocalPort(int localPort) -
getLocalPort
public int getLocalPort()- Specified by:
getLocalPort
in interfacejakarta.servlet.ServletRequest
-
startAsync
public jakarta.servlet.AsyncContext startAsync()- Specified by:
startAsync
in interfacejakarta.servlet.ServletRequest
-
startAsync
public jakarta.servlet.AsyncContext startAsync(jakarta.servlet.ServletRequest request, @Nullable jakarta.servlet.ServletResponse response) - Specified by:
startAsync
in interfacejakarta.servlet.ServletRequest
-
setAsyncStarted
public void setAsyncStarted(boolean asyncStarted) -
isAsyncStarted
public boolean isAsyncStarted()- Specified by:
isAsyncStarted
in interfacejakarta.servlet.ServletRequest
-
setAsyncSupported
public void setAsyncSupported(boolean asyncSupported) -
isAsyncSupported
public boolean isAsyncSupported()- Specified by:
isAsyncSupported
in interfacejakarta.servlet.ServletRequest
-
setAsyncContext
-
getAsyncContext
- Specified by:
getAsyncContext
in interfacejakarta.servlet.ServletRequest
-
setDispatcherType
public void setDispatcherType(jakarta.servlet.DispatcherType dispatcherType) -
getDispatcherType
public jakarta.servlet.DispatcherType getDispatcherType()- Specified by:
getDispatcherType
in interfacejakarta.servlet.ServletRequest
-
getRequestId
- Specified by:
getRequestId
in interfacejakarta.servlet.ServletRequest
-
getProtocolRequestId
- Specified by:
getProtocolRequestId
in interfacejakarta.servlet.ServletRequest
-
getServletConnection
public jakarta.servlet.ServletConnection getServletConnection()- Specified by:
getServletConnection
in interfacejakarta.servlet.ServletRequest
-
setAuthType
-
getAuthType
-
setCookies
-
getCookies
- Specified by:
getCookies
in interfacejakarta.servlet.http.HttpServletRequest
-
addHeader
Add an HTTP header entry for the given name.While this method can take any
Object
as a parameter, it is recommended to use the following types:- String or any Object to be converted using
toString()
; seegetHeader(String)
. - String, Number, or Date for date headers; see
getDateHeader(String)
. - String or Number for integer headers; see
getIntHeader(String)
. String[]
orCollection<String>
for multiple values; seegetHeaders(String)
.
- See Also:
- String or any Object to be converted using
-
removeHeader
Remove already registered entries for the specified HTTP header, if any.- Since:
- 4.3.20
-
getDateHeader
Return the long timestamp for the date header with the givenname
.If the internal value representation is a String, this method will try to parse it as a date using the supported date formats:
- "EEE, dd MMM yyyy HH:mm:ss zzz"
- "EEE, dd-MMM-yy HH:mm:ss zzz"
- "EEE MMM dd HH:mm:ss yyyy"
- Specified by:
getDateHeader
in interfacejakarta.servlet.http.HttpServletRequest
- Parameters:
name
- the header name- See Also:
-
getHeader
-
getHeaders
- Specified by:
getHeaders
in interfacejakarta.servlet.http.HttpServletRequest
-
getHeaderNames
- Specified by:
getHeaderNames
in interfacejakarta.servlet.http.HttpServletRequest
-
getIntHeader
- Specified by:
getIntHeader
in interfacejakarta.servlet.http.HttpServletRequest
-
setMethod
-
getMethod
-
setPathInfo
-
getPathInfo
-
getPathTranslated
-
setContextPath
-
getContextPath
- Specified by:
getContextPath
in interfacejakarta.servlet.http.HttpServletRequest
-
setQueryString
-
getQueryString
-
setRemoteUser
-
getRemoteUser
-
addUserRole
-
isUserInRole
- Specified by:
isUserInRole
in interfacejakarta.servlet.http.HttpServletRequest
-
setUserPrincipal
-
getUserPrincipal
-
setRequestedSessionId
-
getRequestedSessionId
-
setUriTemplate
-
getUriTemplate
-
setRequestURI
-
getRequestURI
-
getRequestURL
- Specified by:
getRequestURL
in interfacejakarta.servlet.http.HttpServletRequest
-
setServletPath
-
getServletPath
- Specified by:
getServletPath
in interfacejakarta.servlet.http.HttpServletRequest
-
setSession
public void setSession(jakarta.servlet.http.HttpSession session) -
getSession
- Specified by:
getSession
in interfacejakarta.servlet.http.HttpServletRequest
-
getSession
- Specified by:
getSession
in interfacejakarta.servlet.http.HttpServletRequest
-
changeSessionId
The implementation of this (Servlet 3.1+) method callsMockHttpSession.changeSessionId()
if the session is a mock session. Otherwise it simply returns the current session id.- Specified by:
changeSessionId
in interfacejakarta.servlet.http.HttpServletRequest
- Since:
- 4.0.3
-
setRequestedSessionIdValid
public void setRequestedSessionIdValid(boolean requestedSessionIdValid) -
isRequestedSessionIdValid
public boolean isRequestedSessionIdValid()- Specified by:
isRequestedSessionIdValid
in interfacejakarta.servlet.http.HttpServletRequest
-
setRequestedSessionIdFromCookie
public void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie) -
isRequestedSessionIdFromCookie
public boolean isRequestedSessionIdFromCookie()- Specified by:
isRequestedSessionIdFromCookie
in interfacejakarta.servlet.http.HttpServletRequest
-
setRequestedSessionIdFromURL
public void setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL) -
isRequestedSessionIdFromURL
public boolean isRequestedSessionIdFromURL()- Specified by:
isRequestedSessionIdFromURL
in interfacejakarta.servlet.http.HttpServletRequest
-
authenticate
public boolean authenticate(jakarta.servlet.http.HttpServletResponse response) throws IOException, jakarta.servlet.ServletException - Specified by:
authenticate
in interfacejakarta.servlet.http.HttpServletRequest
- Throws:
IOException
jakarta.servlet.ServletException
-
login
-
logout
public void logout() throws jakarta.servlet.ServletException- Specified by:
logout
in interfacejakarta.servlet.http.HttpServletRequest
- Throws:
jakarta.servlet.ServletException
-
addPart
public void addPart(jakarta.servlet.http.Part part) -
getPart
public @Nullable jakarta.servlet.http.Part getPart(String name) throws IOException, jakarta.servlet.ServletException - Specified by:
getPart
in interfacejakarta.servlet.http.HttpServletRequest
- Throws:
IOException
jakarta.servlet.ServletException
-
getParts
public Collection<jakarta.servlet.http.Part> getParts() throws IOException, jakarta.servlet.ServletException- Specified by:
getParts
in interfacejakarta.servlet.http.HttpServletRequest
- Throws:
IOException
jakarta.servlet.ServletException
-
setHttpServletMapping
public void setHttpServletMapping(@Nullable jakarta.servlet.http.HttpServletMapping httpServletMapping) -
getHttpServletMapping
public jakarta.servlet.http.HttpServletMapping getHttpServletMapping()- Specified by:
getHttpServletMapping
in interfacejakarta.servlet.http.HttpServletRequest
-
upgrade
public <T extends jakarta.servlet.http.HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, jakarta.servlet.ServletException - Specified by:
upgrade
in interfacejakarta.servlet.http.HttpServletRequest
- Throws:
IOException
jakarta.servlet.ServletException
-