Class MockRequest
- All Implemented Interfaces:
jakarta.servlet.http.HttpServletRequest,jakarta.servlet.ServletRequest
HttpServletRequest, backed by the mocked session.
Only the parts of a request a Vaadin service reads are modeled: headers,
parameters, cookies, attributes, locale, the session and the security
principal. The rest throws UnsupportedOperationException rather than
returning a plausible-looking value, so a test that depends on it fails
loudly instead of asserting on a fiction. Ports, the auth type and the secure
flag come from MockHttpEnvironment and can be changed there.
Container failure modes are reproduced where application code depends on
them: getSession(boolean) reports no session once it has been
invalidated, and changeSessionId() refuses to run without one.
For internal use only. May be renamed or removed in a future release.
-
Field Summary
Fields inherited from interface jakarta.servlet.http.HttpServletRequest
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH -
Constructor Summary
ConstructorsConstructorDescriptionMockRequest(jakarta.servlet.http.HttpSession session) Creates a request bound to the given session. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCookie(jakarta.servlet.http.Cookie cookie) Adds a cookie to the request, as the browser would send it.booleanauthenticate(jakarta.servlet.http.HttpServletResponse response) jakarta.servlet.AsyncContextgetAttribute(String name) Returns the encoding set throughsetCharacterEncoding(String), ornullif none was.intlongjakarta.servlet.http.Cookie[]jakarta.servlet.http.Cookie[]Returns the cookiesgetCookies()reports, ornullif none were added.longgetDateHeader(String name) jakarta.servlet.DispatcherTypeReturns the request headers, live and directly modifiable.getHeaders(String name) jakarta.servlet.ServletInputStreamintgetIntHeader(String name) Returns the localegetLocale()reports.intReturnsMockHttpEnvironment.getLocalPort().getParameter(String parameter) Returns the request parameters, live and directly modifiable.String[]getParameterValues(String name) jakarta.servlet.http.PartCollection<jakarta.servlet.http.Part> getParts()List<jakarta.servlet.http.Part> Returns the multipart parts of this request, ornullwhen no multipart configuration has been provided.intReturnsMockHttpEnvironment.getRemotePort().jakarta.servlet.RequestDispatchergetRequestDispatcher(String path) intReturnsMockHttpEnvironment.getServerPort().jakarta.servlet.ServletConnectionjakarta.servlet.ServletContextjakarta.servlet.http.HttpSessionjakarta.servlet.http.HttpSessiongetSession(boolean create) Returns the principal fromuserPrincipalProviderif it is set and returns one, otherwise falls back touserPrincipalInt.Returns the principalgetUserPrincipal()reports when noprincipal provideris set, or when the provider returnsnull.Returns the principal provider, if one has been set.booleanbooleanbooleanbooleanbooleanbooleanisSecure()ReturnsMockHttpEnvironment.isSecure()Returns the user-in-role checker.booleanisUserInRole(String role) SetsetUserInRole(BiPredicate)to modify the outcome of this function.voidvoidlogout()voidprincipalProvider(Supplier<Principal> supplier) Sets the user principal provider.voidremoveAttribute(String name) voidroleChecker(BiPredicate<Principal, String> checker) Deprecated, for removal: This API element is subject to removal in a future version.voidsetAttribute(String name, Object value) voidvoidsetCharacterEncodingInt(String characterEncodingInt) Sets the encodinggetCharacterEncodingInt()reports.voidsetCookiesInt(jakarta.servlet.http.Cookie[] cookiesInt) Replaces the cookiesgetCookies()reports.voidsetLocaleInt(Locale localeInt) Sets the localegetLocale()reports.voidsetParameter(String name, String... values) Equivalent toparameters.put(name, Arrays.copyOf(values, values.length)).voidsetPartsInt(List<jakarta.servlet.http.Part> partsInt) Sets the multipart parts of this request.voidsetUserInRole(BiPredicate<Principal, String> checker) Sets the user-in-role checker.voidsetUserPrincipalInt(Principal userPrincipalInt) Sets the principalgetUserPrincipal()reports when noprincipal provideris set, or when the provider returnsnull.jakarta.servlet.AsyncContextjakarta.servlet.AsyncContextstartAsync(jakarta.servlet.ServletRequest servletRequest, jakarta.servlet.ServletResponse servletResponse) <T extends jakarta.servlet.http.HttpUpgradeHandler>
TMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface jakarta.servlet.http.HttpServletRequest
getHttpServletMapping, getTrailerFields, isTrailerFieldsReady, newPushBuilderMethods inherited from interface jakarta.servlet.ServletRequest
setCharacterEncoding
-
Constructor Details
-
MockRequest
public MockRequest(jakarta.servlet.http.HttpSession session) Creates a request bound to the given session.- Parameters:
session- the session this request belongs to
-
-
Method Details
-
getInputStream
public jakarta.servlet.ServletInputStream getInputStream()- Specified by:
getInputStreamin interfacejakarta.servlet.ServletRequest
-
startAsync
public jakarta.servlet.AsyncContext startAsync()- Specified by:
startAsyncin interfacejakarta.servlet.ServletRequest
-
startAsync
public jakarta.servlet.AsyncContext startAsync(jakarta.servlet.ServletRequest servletRequest, jakarta.servlet.ServletResponse servletResponse) - Specified by:
startAsyncin interfacejakarta.servlet.ServletRequest
-
getProtocol
- Specified by:
getProtocolin interfacejakarta.servlet.ServletRequest
-
getRequestURL
- Specified by:
getRequestURLin interfacejakarta.servlet.http.HttpServletRequest
-
getCharacterEncodingInt
Returns the encoding set throughsetCharacterEncoding(String), ornullif none was.- Returns:
- the character encoding, or
null
-
setCharacterEncodingInt
Sets the encodinggetCharacterEncodingInt()reports.getCharacterEncoding()does not read it and always returnsnull.- Parameters:
characterEncodingInt- the character encoding, ornull
-
setCharacterEncoding
- Specified by:
setCharacterEncodingin interfacejakarta.servlet.ServletRequest
-
getParameters
Returns the request parameters, live and directly modifiable. PrefersetParameter(String, String...)for a single parameter.- Returns:
- the parameters, keyed by name
-
getParameterValues
- Specified by:
getParameterValuesin interfacejakarta.servlet.ServletRequest
-
isAsyncStarted
public boolean isAsyncStarted()- Specified by:
isAsyncStartedin interfacejakarta.servlet.ServletRequest
-
getContentLengthLong
public long getContentLengthLong()- Specified by:
getContentLengthLongin interfacejakarta.servlet.ServletRequest
-
login
- Specified by:
loginin interfacejakarta.servlet.http.HttpServletRequest
-
isRequestedSessionIdValid
public boolean isRequestedSessionIdValid()- Specified by:
isRequestedSessionIdValidin interfacejakarta.servlet.http.HttpServletRequest
-
getServerPort
public int getServerPort()ReturnsMockHttpEnvironment.getServerPort().- Specified by:
getServerPortin interfacejakarta.servlet.ServletRequest
-
getRequestedSessionId
- Specified by:
getRequestedSessionIdin interfacejakarta.servlet.http.HttpServletRequest
-
getServletPath
- Specified by:
getServletPathin interfacejakarta.servlet.http.HttpServletRequest
-
getSession
public jakarta.servlet.http.HttpSession getSession(boolean create) - Specified by:
getSessionin interfacejakarta.servlet.http.HttpServletRequest
-
getSession
public jakarta.servlet.http.HttpSession getSession()- Specified by:
getSessionin interfacejakarta.servlet.http.HttpServletRequest
-
getServerName
- Specified by:
getServerNamein interfacejakarta.servlet.ServletRequest
-
getLocalAddr
- Specified by:
getLocalAddrin interfacejakarta.servlet.ServletRequest
-
upgrade
- Specified by:
upgradein interfacejakarta.servlet.http.HttpServletRequest
-
isRequestedSessionIdFromCookie
public boolean isRequestedSessionIdFromCookie()- Specified by:
isRequestedSessionIdFromCookiein interfacejakarta.servlet.http.HttpServletRequest
-
getPartsInt
Returns the multipart parts of this request, ornullwhen no multipart configuration has been provided.- Returns:
- the parts, or
null
-
setPartsInt
Sets the multipart parts of this request. Leavenullto havegetPart(String)andgetParts()fail the way a servlet container does without a multipart configuration.- Parameters:
partsInt- the parts, ornull
-
getPart
- Specified by:
getPartin interfacejakarta.servlet.http.HttpServletRequest
-
isRequestedSessionIdFromURL
public boolean isRequestedSessionIdFromURL()- Specified by:
isRequestedSessionIdFromURLin interfacejakarta.servlet.http.HttpServletRequest
-
getLocalPort
public int getLocalPort()ReturnsMockHttpEnvironment.getLocalPort().- Specified by:
getLocalPortin interfacejakarta.servlet.ServletRequest
-
getServletContext
public jakarta.servlet.ServletContext getServletContext()- Specified by:
getServletContextin interfacejakarta.servlet.ServletRequest
-
getQueryString
- Specified by:
getQueryStringin interfacejakarta.servlet.http.HttpServletRequest
-
getDispatcherType
public jakarta.servlet.DispatcherType getDispatcherType()- Specified by:
getDispatcherTypein interfacejakarta.servlet.ServletRequest
-
getRequestId
- Specified by:
getRequestIdin interfacejakarta.servlet.ServletRequest
-
getProtocolRequestId
- Specified by:
getProtocolRequestIdin interfacejakarta.servlet.ServletRequest
-
getServletConnection
public jakarta.servlet.ServletConnection getServletConnection()- Specified by:
getServletConnectionin interfacejakarta.servlet.ServletRequest
-
getParts
- Specified by:
getPartsin interfacejakarta.servlet.http.HttpServletRequest
-
getScheme
- Specified by:
getSchemein interfacejakarta.servlet.ServletRequest
-
logout
public void logout()- Specified by:
logoutin interfacejakarta.servlet.http.HttpServletRequest
-
getLocalName
- Specified by:
getLocalNamein interfacejakarta.servlet.ServletRequest
-
isAsyncSupported
public boolean isAsyncSupported()- Specified by:
isAsyncSupportedin interfacejakarta.servlet.ServletRequest
-
getParameterNames
- Specified by:
getParameterNamesin interfacejakarta.servlet.ServletRequest
-
authenticate
public boolean authenticate(jakarta.servlet.http.HttpServletResponse response) - Specified by:
authenticatein interfacejakarta.servlet.http.HttpServletRequest
-
getPathTranslated
- Specified by:
getPathTranslatedin interfacejakarta.servlet.http.HttpServletRequest
-
getIntHeader
- Specified by:
getIntHeaderin interfacejakarta.servlet.http.HttpServletRequest
-
changeSessionId
- Specified by:
changeSessionIdin interfacejakarta.servlet.http.HttpServletRequest
-
getAsyncContext
public jakarta.servlet.AsyncContext getAsyncContext()- Specified by:
getAsyncContextin interfacejakarta.servlet.ServletRequest
-
getRequestURI
- Specified by:
getRequestURIin interfacejakarta.servlet.http.HttpServletRequest
-
getRequestDispatcher
- Specified by:
getRequestDispatcherin interfacejakarta.servlet.ServletRequest
-
isUserInRole
Returns the user-in-role checker.- Returns:
- the checker deciding whether a principal holds a role
-
setUserInRole
Sets the user-in-role checker.- Parameters:
checker- decides whether a principal holds a role
-
roleChecker
@Deprecated(since="25.4", forRemoval=true) public void roleChecker(BiPredicate<Principal, String> checker) Deprecated, for removal: This API element is subject to removal in a future version.usesetUserInRole(BiPredicate), which takes the same argumentSets the user-in-role checker. Same assetUserInRole(BiPredicate).- Parameters:
checker- decides whether a principal holds a role- Since:
- 1.1
-
isUserInRole
SetsetUserInRole(BiPredicate)to modify the outcome of this function.- Specified by:
isUserInRolein interfacejakarta.servlet.http.HttpServletRequest
-
getPathInfo
- Specified by:
getPathInfoin interfacejakarta.servlet.http.HttpServletRequest
-
getRemoteUser
- Specified by:
getRemoteUserin interfacejakarta.servlet.http.HttpServletRequest
-
getCookiesInt
public jakarta.servlet.http.Cookie[] getCookiesInt()Returns the cookiesgetCookies()reports, ornullif none were added.- Returns:
- the cookies, or
null
-
setCookiesInt
public void setCookiesInt(jakarta.servlet.http.Cookie[] cookiesInt) Replaces the cookiesgetCookies()reports.- Parameters:
cookiesInt- the cookies, ornullfor none
-
addCookie
public void addCookie(jakarta.servlet.http.Cookie cookie) Adds a cookie to the request, as the browser would send it.- Parameters:
cookie- the cookie to add
-
getCookies
public jakarta.servlet.http.Cookie[] getCookies()- Specified by:
getCookiesin interfacejakarta.servlet.http.HttpServletRequest
-
getLocaleInt
Returns the localegetLocale()reports. Defaults toLocale.US.- Returns:
- the locale
-
setLocaleInt
Sets the localegetLocale()reports.- Parameters:
localeInt- the locale
-
getLocale
- Specified by:
getLocalein interfacejakarta.servlet.ServletRequest
-
getMethod
- Specified by:
getMethodin interfacejakarta.servlet.http.HttpServletRequest
-
getParameterMap
- Specified by:
getParameterMapin interfacejakarta.servlet.ServletRequest
-
getAttributeNames
- Specified by:
getAttributeNamesin interfacejakarta.servlet.ServletRequest
-
getRemoteAddr
- Specified by:
getRemoteAddrin interfacejakarta.servlet.ServletRequest
-
getHeaders
- Specified by:
getHeadersin interfacejakarta.servlet.http.HttpServletRequest
-
getUserPrincipalInt
Returns the principalgetUserPrincipal()reports when noprincipal provideris set, or when the provider returnsnull.- Returns:
- the principal, or
null
-
setUserPrincipalInt
Sets the principalgetUserPrincipal()reports when noprincipal provideris set, or when the provider returnsnull.- Parameters:
userPrincipalInt- the principal, ornullfor an anonymous request
-
getUserPrincipalProvider
Returns the principal provider, if one has been set.- Returns:
- the provider, or
nullif none has been set - Since:
- 1.1
-
principalProvider
Sets the user principal provider. Accepts aSupplierso Java callers can pass a lambda directly; Kotlin callers can do the same via SAM conversion.The provider takes precedence over
setUserPrincipalInt(Principal). When it returnsnull,getUserPrincipal()falls back to that principal.- Parameters:
supplier- resolves the principal at call time- Since:
- 1.1
-
getUserPrincipal
Returns the principal fromuserPrincipalProviderif it is set and returns one, otherwise falls back touserPrincipalInt.- Specified by:
getUserPrincipalin interfacejakarta.servlet.http.HttpServletRequest
-
getReader
- Specified by:
getReaderin interfacejakarta.servlet.ServletRequest
-
getLocales
- Specified by:
getLocalesin interfacejakarta.servlet.ServletRequest
-
getAuthType
- Specified by:
getAuthTypein interfacejakarta.servlet.http.HttpServletRequest
-
getCharacterEncoding
- Specified by:
getCharacterEncodingin interfacejakarta.servlet.ServletRequest
-
removeAttribute
- Specified by:
removeAttributein interfacejakarta.servlet.ServletRequest
-
getContentLength
public int getContentLength()- Specified by:
getContentLengthin interfacejakarta.servlet.ServletRequest
-
getHeaders
Returns the request headers, live and directly modifiable.- Returns:
- the headers, keyed by name
-
getHeader
- Specified by:
getHeaderin interfacejakarta.servlet.http.HttpServletRequest
-
getContextPath
- Specified by:
getContextPathin interfacejakarta.servlet.http.HttpServletRequest
-
getContentType
- Specified by:
getContentTypein interfacejakarta.servlet.ServletRequest
-
getHeaderNames
- Specified by:
getHeaderNamesin interfacejakarta.servlet.http.HttpServletRequest
-
getAttribute
- Specified by:
getAttributein interfacejakarta.servlet.ServletRequest
-
setAttribute
- Specified by:
setAttributein interfacejakarta.servlet.ServletRequest
-
getParameter
- Specified by:
getParameterin interfacejakarta.servlet.ServletRequest
-
getRemotePort
public int getRemotePort()ReturnsMockHttpEnvironment.getRemotePort().- Specified by:
getRemotePortin interfacejakarta.servlet.ServletRequest
-
getDateHeader
- Specified by:
getDateHeaderin interfacejakarta.servlet.http.HttpServletRequest
-
getRemoteHost
- Specified by:
getRemoteHostin interfacejakarta.servlet.ServletRequest
-
isSecure
public boolean isSecure()ReturnsMockHttpEnvironment.isSecure()- Specified by:
isSecurein interfacejakarta.servlet.ServletRequest
-
setParameter
Equivalent toparameters.put(name, Arrays.copyOf(values, values.length)).- Parameters:
name- the attribute namevalues- the values of the parameter
-
setUserInRole(BiPredicate), which takes the same argument