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 7.0, this set of mocks is designed on a Servlet 6.1 baseline.
- Since:
- 1.0.2
- Author:
- Juergen Hoeller, Rod Johnson, Rick Evans, Mark Fisher, Chris Beams, Sam Brannen, Brian Clozel
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final StringThe default protocol: 'HTTP/1.1'.static final StringThe default remote address: '127.0.0.1'.static final StringThe default remote host: 'localhost'.static final StringThe default scheme: 'http'.static final StringThe default server address: '127.0.0.1'.static final StringThe default server name: 'localhost'.static final intThe default server port: '80'.Fields inherited from interface jakarta.servlet.http.HttpServletRequestBASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
- 
Constructor SummaryConstructorsConstructorDescriptionCreate a newMockHttpServletRequestwith a defaultMockServletContext.MockHttpServletRequest(@Nullable jakarta.servlet.ServletContext servletContext) Create a newMockHttpServletRequestwith the suppliedServletContext.MockHttpServletRequest(@Nullable jakarta.servlet.ServletContext servletContext, @Nullable String method, @Nullable String requestURI) Create a newMockHttpServletRequestwith the suppliedServletContext,method, andrequestURI.MockHttpServletRequest(@Nullable String method, @Nullable String requestURI) Create a newMockHttpServletRequestwith a defaultMockServletContext.
- 
Method SummaryModifier and TypeMethodDescriptionvoidAdd an HTTP header entry for the given name.voidaddParameter(String name, @Nullable String value) Add a single value for the specified HTTP parameter.voidaddParameter(String name, String... values) Add an array of values for the specified HTTP parameter.voidaddParameters(Map<String, ?> params) Add all provided parameters without replacing any existing values.voidaddPart(jakarta.servlet.http.Part part) voidaddPreferredLocale(Locale locale) Add a new preferred locale, before any existing locales.voidaddUserRole(String role) booleanauthenticate(jakarta.servlet.http.HttpServletResponse response) The implementation of this (Servlet 3.1+) method callsMockHttpSession.changeSessionId()if the session is a mock session.protected voidCheck whether this request is still active (that is, not completed yet), throwing an IllegalStateException if not active anymore.voidClear all of this request's attributes.voidclose()Mark this request as completed, keeping its state.@Nullable jakarta.servlet.AsyncContextgetAttribute(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.intlongjakarta.servlet.http.Cookie @Nullable []longgetDateHeader(String name) Return the long timestamp for the date header with the givenname.jakarta.servlet.DispatcherTypegetHeaders(String name) jakarta.servlet.http.HttpServletMappingjakarta.servlet.ServletInputStreamintgetIntHeader(String name) Return the first preferred locale configured in this mock request.Return an enumeration of the preferred locales configured in this mock request.intgetParameter(String name) getParameterValues(String name) @Nullable jakarta.servlet.http.PartCollection<jakarta.servlet.http.Part> getParts()intjakarta.servlet.RequestDispatchergetRequestDispatcher(String path) intjakarta.servlet.ServletConnectionjakarta.servlet.ServletContextReturn the ServletContext that this request is associated with.@Nullable jakarta.servlet.http.HttpSession@Nullable jakarta.servlet.http.HttpSessiongetSession(boolean create) Return the original URI template used to prepare the request, if any.voidInvalidate this request, clearing its state.booleanisActive()Return whether this request is still active (that is, not completed yet).booleanbooleanbooleanbooleanbooleanbooleanisSecure()booleanisUserInRole(String role) voidvoidlogout()voidRemove all existing parameters.voidremoveAttribute(String name) voidremoveHeader(String name) Remove already registered entries for the specified HTTP header, if any.voidremoveParameter(String name) Remove already registered values for the specified HTTP parameter, if any.voidsetAsyncContext(@Nullable MockAsyncContext asyncContext) voidsetAsyncStarted(boolean asyncStarted) voidsetAsyncSupported(boolean asyncSupported) voidsetAttribute(String name, @Nullable Object value) voidsetAuthType(@Nullable String authType) voidsetCharacterEncoding(@Nullable String characterEncoding) voidsetContent(byte @Nullable [] content) Set the content of the request body as a byte array.voidsetContentType(@Nullable String contentType) voidsetContextPath(String contextPath) voidsetCookies(jakarta.servlet.http.Cookie @Nullable ... cookies) voidsetDispatcherType(jakarta.servlet.DispatcherType dispatcherType) voidsetHttpServletMapping(@Nullable jakarta.servlet.http.HttpServletMapping httpServletMapping) voidsetLocalAddr(String localAddr) voidsetLocalName(String localName) voidsetLocalPort(int localPort) voidvoidsetParameter(String name, String value) Set a single value for the specified HTTP parameter.voidsetParameter(String name, String... values) Set an array of values for the specified HTTP parameter.voidsetParameters(Map<String, ?> params) Set all provided parameters replacing any existing values for the provided parameter names.voidsetPathInfo(@Nullable String pathInfo) voidsetPreferredLocales(List<Locale> locales) Set the list of preferred locales, in descending order, effectively replacing any existing locales.voidsetProtocol(String protocol) voidsetQueryString(@Nullable String queryString) voidsetRemoteAddr(String remoteAddr) voidsetRemoteHost(String remoteHost) voidsetRemotePort(int remotePort) voidsetRemoteUser(@Nullable String remoteUser) voidsetRequestedSessionId(@Nullable String requestedSessionId) voidsetRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie) voidsetRequestedSessionIdFromURL(boolean requestedSessionIdFromURL) voidsetRequestedSessionIdValid(boolean requestedSessionIdValid) voidsetRequestURI(@Nullable String requestURI) voidvoidsetSecure(boolean secure) Set the booleansecureflag indicating whether the mock request was made using a secure channel, such as HTTPS.voidsetServerName(String serverName) voidsetServerPort(int serverPort) voidsetServletPath(String servletPath) voidsetSession(jakarta.servlet.http.HttpSession session) voidsetUriTemplate(@Nullable String uriTemplate) Set the original URI template used to prepare the request, if any.voidsetUserPrincipal(@Nullable Principal userPrincipal) jakarta.servlet.AsyncContextjakarta.servlet.AsyncContextstartAsync(jakarta.servlet.ServletRequest request, @Nullable jakarta.servlet.ServletResponse response) <T extends jakarta.servlet.http.HttpUpgradeHandler>
 TMethods inherited from class Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface jakarta.servlet.http.HttpServletRequestgetTrailerFields, isTrailerFieldsReady, newPushBuilderMethods inherited from interface jakarta.servlet.ServletRequestsetCharacterEncoding
- 
Field Details- 
DEFAULT_PROTOCOLThe default protocol: 'HTTP/1.1'.- Since:
- 4.3.7
- See Also:
 
- 
DEFAULT_SCHEME
- 
DEFAULT_SERVER_ADDR
- 
DEFAULT_SERVER_NAME
- 
DEFAULT_SERVER_PORTpublic static final int DEFAULT_SERVER_PORTThe default server port: '80'.- See Also:
 
- 
DEFAULT_REMOTE_ADDR
- 
DEFAULT_REMOTE_HOST
 
- 
- 
Constructor Details- 
MockHttpServletRequestpublic MockHttpServletRequest()Create a newMockHttpServletRequestwith a defaultMockServletContext.- See Also:
 
- 
MockHttpServletRequestCreate a newMockHttpServletRequestwith a defaultMockServletContext.- Parameters:
- method- the request method (may be- null)
- requestURI- the request URI (may be- null)
- See Also:
 
- 
MockHttpServletRequestCreate a newMockHttpServletRequestwith the suppliedServletContext.- Parameters:
- servletContext- the ServletContext that the request runs in (may be- nullto use a default- MockServletContext)
- See Also:
 
- 
MockHttpServletRequestpublic MockHttpServletRequest(@Nullable jakarta.servlet.ServletContext servletContext, @Nullable String method, @Nullable String requestURI) Create a newMockHttpServletRequestwith the suppliedServletContext,method, andrequestURI.The preferred locale will be set to Locale.ENGLISH.- Parameters:
- servletContext- the ServletContext that the request runs in (may be- nullto use a default- MockServletContext)
- method- the request method (may be- null)
- requestURI- the request URI (may be- null)
- See Also:
 
 
- 
- 
Method Details- 
getServletContextpublic 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:
- getServletContextin interface- jakarta.servlet.ServletRequest
 
- 
isActivepublic boolean isActive()Return whether this request is still active (that is, not completed yet).
- 
closepublic void close()Mark this request as completed, keeping its state.
- 
invalidatepublic void invalidate()Invalidate this request, clearing its state.
- 
checkActiveCheck whether this request is still active (that is, not completed yet), throwing an IllegalStateException if not active anymore.- Throws:
- IllegalStateException
 
- 
getAttribute
- 
getAttributeNames- Specified by:
- getAttributeNamesin interface- jakarta.servlet.ServletRequest
 
- 
getCharacterEncoding
- 
setCharacterEncoding
- 
setContentSet the content of the request body as a byte array.If the supplied byte array represents text such as XML or JSON, the character encodingshould typically be set as well.- See Also:
 
- 
getContentAsByteArrayGet the content of the request body as a byte array.- Returns:
- the content as a byte array (potentially null)
- Since:
- 5.0
- See Also:
 
- 
getContentAsStringpublic @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 set
- UnsupportedEncodingException- if the character encoding is not supported
- Since:
- 5.0
- See Also:
 
- 
getContentLengthpublic int getContentLength()- Specified by:
- getContentLengthin interface- jakarta.servlet.ServletRequest
 
- 
getContentLengthLongpublic long getContentLengthLong()- Specified by:
- getContentLengthLongin interface- jakarta.servlet.ServletRequest
 
- 
setContentType
- 
getContentType
- 
getInputStreampublic jakarta.servlet.ServletInputStream getInputStream()- Specified by:
- getInputStreamin interface- jakarta.servlet.ServletRequest
 
- 
setParameter
- 
setParameter
- 
setParametersSet all provided parameters replacing any existing values for the provided parameter names. To add without replacing existing values, useaddParameters(java.util.Map).
- 
addParameter
- 
addParameter
- 
addParametersAdd all provided parameters without replacing any existing values. To replace existing values, usesetParameters(java.util.Map).
- 
removeParameterRemove already registered values for the specified HTTP parameter, if any.
- 
removeAllParameterspublic void removeAllParameters()Remove all existing parameters.
- 
getParameter
- 
getParameterNames- Specified by:
- getParameterNamesin interface- jakarta.servlet.ServletRequest
 
- 
getParameterValues
- 
getParameterMap
- 
setProtocol
- 
getProtocol- Specified by:
- getProtocolin interface- jakarta.servlet.ServletRequest
 
- 
setScheme
- 
getScheme- Specified by:
- getSchemein interface- jakarta.servlet.ServletRequest
 
- 
setServerName
- 
getServerName- Specified by:
- getServerNamein interface- jakarta.servlet.ServletRequest
 
- 
setServerPortpublic void setServerPort(int serverPort) 
- 
getServerPortpublic int getServerPort()- Specified by:
- getServerPortin interface- jakarta.servlet.ServletRequest
 
- 
getReader- Specified by:
- getReaderin interface- jakarta.servlet.ServletRequest
- Throws:
- UnsupportedEncodingException
 
- 
setRemoteAddr
- 
getRemoteAddr- Specified by:
- getRemoteAddrin interface- jakarta.servlet.ServletRequest
 
- 
setRemoteHost
- 
getRemoteHost- Specified by:
- getRemoteHostin interface- jakarta.servlet.ServletRequest
 
- 
setAttribute
- 
removeAttribute- Specified by:
- removeAttributein interface- jakarta.servlet.ServletRequest
 
- 
clearAttributespublic void clearAttributes()Clear all of this request's attributes.
- 
addPreferredLocaleAdd a new preferred locale, before any existing locales.- See Also:
 
- 
setPreferredLocales
- 
getLocaleReturn the first preferred locale configured in this mock request.If no locales have been explicitly configured, the default, preferred Localefor 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-Languageheader.- Specified by:
- getLocalein interface- jakarta.servlet.ServletRequest
- See Also:
 
- 
getLocalesReturn an enumeration of the preferred locales configured in this mock request.If no locales have been explicitly configured, the default, preferred Localefor 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-Languageheader.- Specified by:
- getLocalesin interface- jakarta.servlet.ServletRequest
- See Also:
 
- 
setSecurepublic void setSecure(boolean secure) Set the booleansecureflag indicating whether the mock request was made using a secure channel, such as HTTPS.- See Also:
 
- 
isSecure
- 
getRequestDispatcher- Specified by:
- getRequestDispatcherin interface- jakarta.servlet.ServletRequest
 
- 
setRemotePortpublic void setRemotePort(int remotePort) 
- 
getRemotePortpublic int getRemotePort()- Specified by:
- getRemotePortin interface- jakarta.servlet.ServletRequest
 
- 
setLocalName
- 
getLocalName- Specified by:
- getLocalNamein interface- jakarta.servlet.ServletRequest
 
- 
setLocalAddr
- 
getLocalAddr- Specified by:
- getLocalAddrin interface- jakarta.servlet.ServletRequest
 
- 
setLocalPortpublic void setLocalPort(int localPort) 
- 
getLocalPortpublic int getLocalPort()- Specified by:
- getLocalPortin interface- jakarta.servlet.ServletRequest
 
- 
startAsyncpublic jakarta.servlet.AsyncContext startAsync()- Specified by:
- startAsyncin interface- jakarta.servlet.ServletRequest
 
- 
startAsyncpublic jakarta.servlet.AsyncContext startAsync(jakarta.servlet.ServletRequest request, @Nullable jakarta.servlet.ServletResponse response) - Specified by:
- startAsyncin interface- jakarta.servlet.ServletRequest
 
- 
setAsyncStartedpublic void setAsyncStarted(boolean asyncStarted) 
- 
isAsyncStartedpublic boolean isAsyncStarted()- Specified by:
- isAsyncStartedin interface- jakarta.servlet.ServletRequest
 
- 
setAsyncSupportedpublic void setAsyncSupported(boolean asyncSupported) 
- 
isAsyncSupportedpublic boolean isAsyncSupported()- Specified by:
- isAsyncSupportedin interface- jakarta.servlet.ServletRequest
 
- 
setAsyncContext
- 
getAsyncContext- Specified by:
- getAsyncContextin interface- jakarta.servlet.ServletRequest
 
- 
setDispatcherTypepublic void setDispatcherType(jakarta.servlet.DispatcherType dispatcherType) 
- 
getDispatcherTypepublic jakarta.servlet.DispatcherType getDispatcherType()- Specified by:
- getDispatcherTypein interface- jakarta.servlet.ServletRequest
 
- 
getRequestId- Specified by:
- getRequestIdin interface- jakarta.servlet.ServletRequest
 
- 
getProtocolRequestId- Specified by:
- getProtocolRequestIdin interface- jakarta.servlet.ServletRequest
 
- 
getServletConnectionpublic jakarta.servlet.ServletConnection getServletConnection()- Specified by:
- getServletConnectionin interface- jakarta.servlet.ServletRequest
 
- 
setAuthType
- 
getAuthType
- 
setCookies
- 
getCookies- Specified by:
- getCookiesin interface- jakarta.servlet.http.HttpServletRequest
 
- 
addHeaderAdd an HTTP header entry for the given name.While this method can take any Objectas 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[]or- Collection<String>for multiple values; see- getHeaders(String).
 - See Also:
 
- String or any Object to be converted using 
- 
removeHeaderRemove already registered entries for the specified HTTP header, if any.- Since:
- 4.3.20
 
- 
getDateHeaderReturn 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:
- getDateHeaderin interface- jakarta.servlet.http.HttpServletRequest
- Parameters:
- name- the header name
- See Also:
 
- 
getHeader
- 
getHeaders- Specified by:
- getHeadersin interface- jakarta.servlet.http.HttpServletRequest
 
- 
getHeaderNames- Specified by:
- getHeaderNamesin interface- jakarta.servlet.http.HttpServletRequest
 
- 
getIntHeader- Specified by:
- getIntHeaderin interface- jakarta.servlet.http.HttpServletRequest
 
- 
setMethod
- 
getMethod
- 
setPathInfo
- 
getPathInfo
- 
getPathTranslated
- 
setContextPath
- 
getContextPath- Specified by:
- getContextPathin interface- jakarta.servlet.http.HttpServletRequest
 
- 
setQueryString
- 
getQueryString
- 
setRemoteUser
- 
getRemoteUser
- 
addUserRole
- 
isUserInRole- Specified by:
- isUserInRolein interface- jakarta.servlet.http.HttpServletRequest
 
- 
setUserPrincipal
- 
getUserPrincipal
- 
setRequestedSessionId
- 
getRequestedSessionId
- 
setUriTemplate
- 
getUriTemplate
- 
setRequestURI
- 
getRequestURI
- 
getRequestURL- Specified by:
- getRequestURLin interface- jakarta.servlet.http.HttpServletRequest
 
- 
setServletPath
- 
getServletPath- Specified by:
- getServletPathin interface- jakarta.servlet.http.HttpServletRequest
 
- 
setSessionpublic void setSession(jakarta.servlet.http.HttpSession session) 
- 
getSession- Specified by:
- getSessionin interface- jakarta.servlet.http.HttpServletRequest
 
- 
getSession- Specified by:
- getSessionin interface- jakarta.servlet.http.HttpServletRequest
 
- 
changeSessionIdThe 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:
- changeSessionIdin interface- jakarta.servlet.http.HttpServletRequest
- Since:
- 4.0.3
 
- 
setRequestedSessionIdValidpublic void setRequestedSessionIdValid(boolean requestedSessionIdValid) 
- 
isRequestedSessionIdValidpublic boolean isRequestedSessionIdValid()- Specified by:
- isRequestedSessionIdValidin interface- jakarta.servlet.http.HttpServletRequest
 
- 
setRequestedSessionIdFromCookiepublic void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie) 
- 
isRequestedSessionIdFromCookiepublic boolean isRequestedSessionIdFromCookie()- Specified by:
- isRequestedSessionIdFromCookiein interface- jakarta.servlet.http.HttpServletRequest
 
- 
setRequestedSessionIdFromURLpublic void setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL) 
- 
isRequestedSessionIdFromURLpublic boolean isRequestedSessionIdFromURL()- Specified by:
- isRequestedSessionIdFromURLin interface- jakarta.servlet.http.HttpServletRequest
 
- 
authenticatepublic boolean authenticate(jakarta.servlet.http.HttpServletResponse response) throws IOException, jakarta.servlet.ServletException - Specified by:
- authenticatein interface- jakarta.servlet.http.HttpServletRequest
- Throws:
- IOException
- jakarta.servlet.ServletException
 
- 
login
- 
logoutpublic void logout() throws jakarta.servlet.ServletException- Specified by:
- logoutin interface- jakarta.servlet.http.HttpServletRequest
- Throws:
- jakarta.servlet.ServletException
 
- 
addPartpublic void addPart(jakarta.servlet.http.Part part) 
- 
getPartpublic @Nullable jakarta.servlet.http.Part getPart(String name) throws IOException, jakarta.servlet.ServletException - Specified by:
- getPartin interface- jakarta.servlet.http.HttpServletRequest
- Throws:
- IOException
- jakarta.servlet.ServletException
 
- 
getPartspublic Collection<jakarta.servlet.http.Part> getParts() throws IOException, jakarta.servlet.ServletException- Specified by:
- getPartsin interface- jakarta.servlet.http.HttpServletRequest
- Throws:
- IOException
- jakarta.servlet.ServletException
 
- 
setHttpServletMappingpublic void setHttpServletMapping(@Nullable jakarta.servlet.http.HttpServletMapping httpServletMapping) 
- 
getHttpServletMappingpublic jakarta.servlet.http.HttpServletMapping getHttpServletMapping()- Specified by:
- getHttpServletMappingin interface- jakarta.servlet.http.HttpServletRequest
 
- 
upgradepublic <T extends jakarta.servlet.http.HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, jakarta.servlet.ServletException - Specified by:
- upgradein interface- jakarta.servlet.http.HttpServletRequest
- Throws:
- IOException
- jakarta.servlet.ServletException
 
 
-