Class MockHttpServletRequest
- All Implemented Interfaces:
javax.servlet.http.HttpServletRequest,javax.servlet.ServletRequest
HttpServletRequest interface.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe default protocol: 'http'.static final StringThe default remote address: '127.0.0.1'.static final StringThe default remote host: 'localhost'.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 javax.servlet.http.HttpServletRequest
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH -
Constructor Summary
ConstructorsConstructorDescriptionCreate a newMockHttpServletRequestwith a defaultMockServletContext.MockHttpServletRequest(String method, String requestURI) Create a newMockHttpServletRequestwith a defaultMockServletContext.MockHttpServletRequest(javax.servlet.ServletContext servletContext) Create a newMockHttpServletRequestwith the suppliedServletContext.MockHttpServletRequest(javax.servlet.ServletContext servletContext, String method, String requestURI) Create a newMockHttpServletRequestwith the suppliedServletContext,method, andrequestURI. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a header entry for the given name.voidaddParameter(String name, 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<?, ?> params) Adds all provided parameters without replacing any existing values.voidaddPart(javax.servlet.http.Part part) voidaddPreferredLocale(Locale locale) Add a new preferred locale, before any existing locales.voidaddUserRole(String role) booleanauthenticate(javax.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.javax.servlet.AsyncContextgetAttribute(String name) intlongjavax.servlet.http.Cookie[]longgetDateHeader(String name) Return the long timestamp for the date header with the givenname.javax.servlet.DispatcherTypegetHeaders(String name) javax.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) String[]getParameterValues(String name) javax.servlet.http.PartCollection<javax.servlet.http.Part> getParts()getRealPath(String path) Deprecated.intjavax.servlet.RequestDispatchergetRequestDispatcher(String path) intjavax.servlet.ServletContextReturn the ServletContext that this request is associated with.javax.servlet.http.HttpSessionjavax.servlet.http.HttpSessiongetSession(boolean create) voidInvalidate this request, clearing its state.booleanisActive()Return whether this request is still active (that is, not completed yet).booleanbooleanbooleanbooleanDeprecated.booleanbooleanbooleanisSecure()booleanisUserInRole(String role) voidvoidlogout()voidRemoves all existing parameters.voidremoveAttribute(String name) voidremoveParameter(String name) Remove already registered values for the specified HTTP parameter, if any.voidsetAsyncContext(MockAsyncContext asyncContext) voidsetAsyncStarted(boolean asyncStarted) voidsetAsyncSupported(boolean asyncSupported) voidsetAttribute(String name, Object value) voidsetAuthType(String authType) voidsetCharacterEncoding(String characterEncoding) voidsetContent(byte[] content) voidsetContentType(String contentType) voidsetContextPath(String contextPath) voidsetCookies(javax.servlet.http.Cookie... cookies) voidsetDispatcherType(javax.servlet.DispatcherType dispatcherType) 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 params) Sets all provided parameters replacing any existing values for the provided parameter names.voidsetPathInfo(String pathInfo) voidsetPreferredLocales(List<Locale> locales) Set the list of preferred locales, in descending order, effectively replacing any existing locales.voidsetProtocol(String protocol) voidsetQueryString(String queryString) voidsetRemoteAddr(String remoteAddr) voidsetRemoteHost(String remoteHost) voidsetRemotePort(int remotePort) voidsetRemoteUser(String remoteUser) voidsetRequestedSessionId(String requestedSessionId) voidsetRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie) voidsetRequestedSessionIdFromURL(boolean requestedSessionIdFromURL) voidsetRequestedSessionIdValid(boolean requestedSessionIdValid) voidsetRequestURI(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(javax.servlet.http.HttpSession session) voidsetUserPrincipal(Principal userPrincipal) javax.servlet.AsyncContextjavax.servlet.AsyncContextstartAsync(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) <T extends javax.servlet.http.HttpUpgradeHandler>
TMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface javax.servlet.http.HttpServletRequest
getHttpServletMapping, getTrailerFields, isTrailerFieldsReady, newPushBuilder
-
Field Details
-
DEFAULT_PROTOCOL
The default protocol: 'http'.- See Also:
-
DEFAULT_SERVER_ADDR
The default server address: '127.0.0.1'.- See Also:
-
DEFAULT_SERVER_NAME
The default server name: 'localhost'.- See Also:
-
DEFAULT_SERVER_PORT
public static final int DEFAULT_SERVER_PORTThe default server port: '80'.- See Also:
-
DEFAULT_REMOTE_ADDR
The default remote address: '127.0.0.1'.- See Also:
-
DEFAULT_REMOTE_HOST
The default remote host: 'localhost'.- See Also:
-
-
Constructor Details
-
MockHttpServletRequest
public MockHttpServletRequest()Create a newMockHttpServletRequestwith a defaultMockServletContext.- See Also:
-
MockHttpServletRequest
Create a newMockHttpServletRequestwith a defaultMockServletContext.- Parameters:
method- the request method (may benull)requestURI- the request URI (may benull)- See Also:
-
MockHttpServletRequest
public MockHttpServletRequest(javax.servlet.ServletContext servletContext) Create a newMockHttpServletRequestwith the suppliedServletContext.- Parameters:
servletContext- the ServletContext that the request runs in (may benullto use a defaultMockServletContext)- See Also:
-
MockHttpServletRequest
public MockHttpServletRequest(javax.servlet.ServletContext servletContext, String method, 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 benullto use a defaultMockServletContext)method- the request method (may benull)requestURI- the request URI (may benull)- See Also:
-
-
Method Details
-
getServletContext
public javax.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 interfacejavax.servlet.ServletRequest- Returns:
- ServletContext
-
isActive
public boolean isActive()Return whether this request is still active (that is, not completed yet).- Returns:
- boolean
-
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
- Specified by:
getAttributein interfacejavax.servlet.ServletRequest
-
getAttributeNames
- Specified by:
getAttributeNamesin interfacejavax.servlet.ServletRequest
-
getCharacterEncoding
- Specified by:
getCharacterEncodingin interfacejavax.servlet.ServletRequest
-
setCharacterEncoding
- Specified by:
setCharacterEncodingin interfacejavax.servlet.ServletRequest
-
setContent
public void setContent(byte[] content) -
getContentLength
public int getContentLength()- Specified by:
getContentLengthin interfacejavax.servlet.ServletRequest
-
getContentLengthLong
public long getContentLengthLong()- Specified by:
getContentLengthLongin interfacejavax.servlet.ServletRequest
-
setContentType
-
getContentType
- Specified by:
getContentTypein interfacejavax.servlet.ServletRequest
-
getInputStream
public javax.servlet.ServletInputStream getInputStream()- Specified by:
getInputStreamin interfacejavax.servlet.ServletRequest
-
setParameter
Set a single value for the specified HTTP parameter.If there are already one or more values registered for the given parameter name, they will be replaced.
- Parameters:
name- namevalue- value
-
setParameter
Set an array of values for the specified HTTP parameter.If there are already one or more values registered for the given parameter name, they will be replaced.
- Parameters:
name- namevalues- values
-
setParameters
Sets all provided parameters replacing any existing values for the provided parameter names. To add without replacing existing values, useaddParameters(java.util.Map).- Parameters:
params- map of params
-
addParameter
Add a single value for the specified HTTP parameter.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.
- Parameters:
name- namevalue- value
-
addParameter
Add an array of values for the specified HTTP parameter.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.
- Parameters:
name- namevalues- value
-
addParameters
Adds all provided parameters without replacing any existing values. To replace existing values, usesetParameters(java.util.Map).- Parameters:
params- map of params
-
removeParameter
Remove already registered values for the specified HTTP parameter, if any.- Parameters:
name- name
-
removeAllParameters
public void removeAllParameters()Removes all existing parameters. -
getParameter
- Specified by:
getParameterin interfacejavax.servlet.ServletRequest
-
getParameterNames
- Specified by:
getParameterNamesin interfacejavax.servlet.ServletRequest
-
getParameterValues
- Specified by:
getParameterValuesin interfacejavax.servlet.ServletRequest
-
getParameterMap
- Specified by:
getParameterMapin interfacejavax.servlet.ServletRequest
-
setProtocol
-
getProtocol
- Specified by:
getProtocolin interfacejavax.servlet.ServletRequest
-
setScheme
-
getScheme
- Specified by:
getSchemein interfacejavax.servlet.ServletRequest
-
setServerName
-
getServerName
- Specified by:
getServerNamein interfacejavax.servlet.ServletRequest
-
setServerPort
public void setServerPort(int serverPort) -
getServerPort
public int getServerPort()- Specified by:
getServerPortin interfacejavax.servlet.ServletRequest
-
getReader
- Specified by:
getReaderin interfacejavax.servlet.ServletRequest- Throws:
UnsupportedEncodingException
-
setRemoteAddr
-
getRemoteAddr
- Specified by:
getRemoteAddrin interfacejavax.servlet.ServletRequest
-
setRemoteHost
-
getRemoteHost
- Specified by:
getRemoteHostin interfacejavax.servlet.ServletRequest
-
setAttribute
- Specified by:
setAttributein interfacejavax.servlet.ServletRequest
-
removeAttribute
- Specified by:
removeAttributein interfacejavax.servlet.ServletRequest
-
clearAttributes
public void clearAttributes()Clear all of this request's attributes. -
addPreferredLocale
Add a new preferred locale, before any existing locales.- Parameters:
locale- locale- See Also:
-
setPreferredLocales
Set the list of preferred locales, in descending order, effectively replacing any existing locales.- Since:
- 3.2
- See Also:
-
getLocale
Return 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 interfacejavax.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
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 interfacejavax.servlet.ServletRequest- See Also:
-
setSecure
public void setSecure(boolean secure) Set the booleansecureflag indicating whether the mock request was made using a secure channel, such as HTTPS.- See Also:
-
isSecure
public boolean isSecure()- Specified by:
isSecurein interfacejavax.servlet.ServletRequest- See Also:
-
getRequestDispatcher
- Specified by:
getRequestDispatcherin interfacejavax.servlet.ServletRequest
-
getRealPath
Deprecated.- Specified by:
getRealPathin interfacejavax.servlet.ServletRequest
-
setRemotePort
public void setRemotePort(int remotePort) -
getRemotePort
public int getRemotePort()- Specified by:
getRemotePortin interfacejavax.servlet.ServletRequest
-
setLocalName
-
getLocalName
- Specified by:
getLocalNamein interfacejavax.servlet.ServletRequest
-
setLocalAddr
-
getLocalAddr
- Specified by:
getLocalAddrin interfacejavax.servlet.ServletRequest
-
setLocalPort
public void setLocalPort(int localPort) -
getLocalPort
public int getLocalPort()- Specified by:
getLocalPortin interfacejavax.servlet.ServletRequest
-
startAsync
public javax.servlet.AsyncContext startAsync()- Specified by:
startAsyncin interfacejavax.servlet.ServletRequest
-
startAsync
public javax.servlet.AsyncContext startAsync(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) - Specified by:
startAsyncin interfacejavax.servlet.ServletRequest
-
setAsyncStarted
public void setAsyncStarted(boolean asyncStarted) -
isAsyncStarted
public boolean isAsyncStarted()- Specified by:
isAsyncStartedin interfacejavax.servlet.ServletRequest
-
setAsyncSupported
public void setAsyncSupported(boolean asyncSupported) -
isAsyncSupported
public boolean isAsyncSupported()- Specified by:
isAsyncSupportedin interfacejavax.servlet.ServletRequest
-
setAsyncContext
-
getAsyncContext
public javax.servlet.AsyncContext getAsyncContext()- Specified by:
getAsyncContextin interfacejavax.servlet.ServletRequest
-
setDispatcherType
public void setDispatcherType(javax.servlet.DispatcherType dispatcherType) -
getDispatcherType
public javax.servlet.DispatcherType getDispatcherType()- Specified by:
getDispatcherTypein interfacejavax.servlet.ServletRequest
-
setAuthType
-
getAuthType
- Specified by:
getAuthTypein interfacejavax.servlet.http.HttpServletRequest
-
setCookies
public void setCookies(javax.servlet.http.Cookie... cookies) -
getCookies
public javax.servlet.http.Cookie[] getCookies()- Specified by:
getCookiesin interfacejavax.servlet.http.HttpServletRequest
-
addHeader
Add a 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(java.lang.String). - String, Number, or Date for date headers; see
getDateHeader(java.lang.String). - String or Number for integer headers; see
getIntHeader(java.lang.String). String[]orCollection<String>for multiple values; seegetHeaders(java.lang.String).
- Parameters:
name- namevalue- value- See Also:
- String or any Object to be converted using
-
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:
getDateHeaderin interfacejavax.servlet.http.HttpServletRequest- Parameters:
name- the header name- See Also:
-
getHeader
- Specified by:
getHeaderin interfacejavax.servlet.http.HttpServletRequest
-
getHeaders
- Specified by:
getHeadersin interfacejavax.servlet.http.HttpServletRequest
-
getHeaderNames
- Specified by:
getHeaderNamesin interfacejavax.servlet.http.HttpServletRequest
-
getIntHeader
- Specified by:
getIntHeaderin interfacejavax.servlet.http.HttpServletRequest
-
setMethod
-
getMethod
- Specified by:
getMethodin interfacejavax.servlet.http.HttpServletRequest
-
setPathInfo
-
getPathInfo
- Specified by:
getPathInfoin interfacejavax.servlet.http.HttpServletRequest
-
getPathTranslated
- Specified by:
getPathTranslatedin interfacejavax.servlet.http.HttpServletRequest
-
setContextPath
-
getContextPath
- Specified by:
getContextPathin interfacejavax.servlet.http.HttpServletRequest
-
setQueryString
-
getQueryString
- Specified by:
getQueryStringin interfacejavax.servlet.http.HttpServletRequest
-
setRemoteUser
-
getRemoteUser
- Specified by:
getRemoteUserin interfacejavax.servlet.http.HttpServletRequest
-
addUserRole
-
isUserInRole
- Specified by:
isUserInRolein interfacejavax.servlet.http.HttpServletRequest
-
setUserPrincipal
-
getUserPrincipal
- Specified by:
getUserPrincipalin interfacejavax.servlet.http.HttpServletRequest
-
setRequestedSessionId
-
getRequestedSessionId
- Specified by:
getRequestedSessionIdin interfacejavax.servlet.http.HttpServletRequest
-
setRequestURI
-
getRequestURI
- Specified by:
getRequestURIin interfacejavax.servlet.http.HttpServletRequest
-
getRequestURL
- Specified by:
getRequestURLin interfacejavax.servlet.http.HttpServletRequest
-
setServletPath
-
getServletPath
- Specified by:
getServletPathin interfacejavax.servlet.http.HttpServletRequest
-
setSession
public void setSession(javax.servlet.http.HttpSession session) -
getSession
public javax.servlet.http.HttpSession getSession(boolean create) - Specified by:
getSessionin interfacejavax.servlet.http.HttpServletRequest
-
getSession
public javax.servlet.http.HttpSession getSession()- Specified by:
getSessionin interfacejavax.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:
changeSessionIdin interfacejavax.servlet.http.HttpServletRequest- Since:
- 4.0.3
-
setRequestedSessionIdValid
public void setRequestedSessionIdValid(boolean requestedSessionIdValid) -
isRequestedSessionIdValid
public boolean isRequestedSessionIdValid()- Specified by:
isRequestedSessionIdValidin interfacejavax.servlet.http.HttpServletRequest
-
setRequestedSessionIdFromCookie
public void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie) -
isRequestedSessionIdFromCookie
public boolean isRequestedSessionIdFromCookie()- Specified by:
isRequestedSessionIdFromCookiein interfacejavax.servlet.http.HttpServletRequest
-
setRequestedSessionIdFromURL
public void setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL) -
isRequestedSessionIdFromURL
public boolean isRequestedSessionIdFromURL()- Specified by:
isRequestedSessionIdFromURLin interfacejavax.servlet.http.HttpServletRequest
-
isRequestedSessionIdFromUrl
Deprecated.- Specified by:
isRequestedSessionIdFromUrlin interfacejavax.servlet.http.HttpServletRequest
-
authenticate
public boolean authenticate(javax.servlet.http.HttpServletResponse response) throws IOException, javax.servlet.ServletException - Specified by:
authenticatein interfacejavax.servlet.http.HttpServletRequest- Throws:
IOExceptionjavax.servlet.ServletException
-
login
- Specified by:
loginin interfacejavax.servlet.http.HttpServletRequest- Throws:
javax.servlet.ServletException
-
logout
public void logout() throws javax.servlet.ServletException- Specified by:
logoutin interfacejavax.servlet.http.HttpServletRequest- Throws:
javax.servlet.ServletException
-
addPart
public void addPart(javax.servlet.http.Part part) -
getPart
public javax.servlet.http.Part getPart(String name) throws IOException, IllegalStateException, javax.servlet.ServletException - Specified by:
getPartin interfacejavax.servlet.http.HttpServletRequest- Throws:
IOExceptionIllegalStateExceptionjavax.servlet.ServletException
-
getParts
public Collection<javax.servlet.http.Part> getParts() throws IOException, IllegalStateException, javax.servlet.ServletException- Specified by:
getPartsin interfacejavax.servlet.http.HttpServletRequest- Throws:
IOExceptionIllegalStateExceptionjavax.servlet.ServletException
-
upgrade
public <T extends javax.servlet.http.HttpUpgradeHandler> T upgrade(Class<T> arg0) throws IOException, javax.servlet.ServletException - Specified by:
upgradein interfacejavax.servlet.http.HttpServletRequest- Throws:
IOExceptionjavax.servlet.ServletException
-