Class MockRequest

java.lang.Object
com.vaadin.browserless.mocks.MockRequest
All Implemented Interfaces:
jakarta.servlet.http.HttpServletRequest, jakarta.servlet.ServletRequest

public class MockRequest extends Object implements jakarta.servlet.http.HttpServletRequest
A standalone 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.

  • 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:
      getInputStream in interface jakarta.servlet.ServletRequest
    • startAsync

      public jakarta.servlet.AsyncContext startAsync()
      Specified by:
      startAsync in interface jakarta.servlet.ServletRequest
    • startAsync

      public jakarta.servlet.AsyncContext startAsync(jakarta.servlet.ServletRequest servletRequest, jakarta.servlet.ServletResponse servletResponse)
      Specified by:
      startAsync in interface jakarta.servlet.ServletRequest
    • getProtocol

      public String getProtocol()
      Specified by:
      getProtocol in interface jakarta.servlet.ServletRequest
    • getRequestURL

      public StringBuffer getRequestURL()
      Specified by:
      getRequestURL in interface jakarta.servlet.http.HttpServletRequest
    • getCharacterEncodingInt

      public String getCharacterEncodingInt()
      Returns the encoding set through setCharacterEncoding(String), or null if none was.
      Returns:
      the character encoding, or null
    • setCharacterEncodingInt

      public void setCharacterEncodingInt(String characterEncodingInt)
      Sets the encoding getCharacterEncodingInt() reports. getCharacterEncoding() does not read it and always returns null.
      Parameters:
      characterEncodingInt - the character encoding, or null
    • setCharacterEncoding

      public void setCharacterEncoding(String env)
      Specified by:
      setCharacterEncoding in interface jakarta.servlet.ServletRequest
    • getParameters

      public Map<String,String[]> getParameters()
      Returns the request parameters, live and directly modifiable. Prefer setParameter(String, String...) for a single parameter.
      Returns:
      the parameters, keyed by name
    • getParameterValues

      public String[] getParameterValues(String name)
      Specified by:
      getParameterValues in interface jakarta.servlet.ServletRequest
    • isAsyncStarted

      public boolean isAsyncStarted()
      Specified by:
      isAsyncStarted in interface jakarta.servlet.ServletRequest
    • getContentLengthLong

      public long getContentLengthLong()
      Specified by:
      getContentLengthLong in interface jakarta.servlet.ServletRequest
    • login

      public void login(String username, String password)
      Specified by:
      login in interface jakarta.servlet.http.HttpServletRequest
    • isRequestedSessionIdValid

      public boolean isRequestedSessionIdValid()
      Specified by:
      isRequestedSessionIdValid in interface jakarta.servlet.http.HttpServletRequest
    • getServerPort

      public int getServerPort()
      Specified by:
      getServerPort in interface jakarta.servlet.ServletRequest
    • getRequestedSessionId

      public String getRequestedSessionId()
      Specified by:
      getRequestedSessionId in interface jakarta.servlet.http.HttpServletRequest
    • getServletPath

      public String getServletPath()
      Specified by:
      getServletPath in interface jakarta.servlet.http.HttpServletRequest
    • getSession

      public jakarta.servlet.http.HttpSession getSession(boolean create)
      Specified by:
      getSession in interface jakarta.servlet.http.HttpServletRequest
    • getSession

      public jakarta.servlet.http.HttpSession getSession()
      Specified by:
      getSession in interface jakarta.servlet.http.HttpServletRequest
    • getServerName

      public String getServerName()
      Specified by:
      getServerName in interface jakarta.servlet.ServletRequest
    • getLocalAddr

      public String getLocalAddr()
      Specified by:
      getLocalAddr in interface jakarta.servlet.ServletRequest
    • upgrade

      public <T extends jakarta.servlet.http.HttpUpgradeHandler> T upgrade(Class<T> handlerClass)
      Specified by:
      upgrade in interface jakarta.servlet.http.HttpServletRequest
    • isRequestedSessionIdFromCookie

      public boolean isRequestedSessionIdFromCookie()
      Specified by:
      isRequestedSessionIdFromCookie in interface jakarta.servlet.http.HttpServletRequest
    • getPartsInt

      public List<jakarta.servlet.http.Part> getPartsInt()
      Returns the multipart parts of this request, or null when no multipart configuration has been provided.
      Returns:
      the parts, or null
    • setPartsInt

      public void setPartsInt(List<jakarta.servlet.http.Part> partsInt)
      Sets the multipart parts of this request. Leave null to have getPart(String) and getParts() fail the way a servlet container does without a multipart configuration.
      Parameters:
      partsInt - the parts, or null
    • getPart

      public jakarta.servlet.http.Part getPart(String name)
      Specified by:
      getPart in interface jakarta.servlet.http.HttpServletRequest
    • isRequestedSessionIdFromURL

      public boolean isRequestedSessionIdFromURL()
      Specified by:
      isRequestedSessionIdFromURL in interface jakarta.servlet.http.HttpServletRequest
    • getLocalPort

      public int getLocalPort()
      Specified by:
      getLocalPort in interface jakarta.servlet.ServletRequest
    • getServletContext

      public jakarta.servlet.ServletContext getServletContext()
      Specified by:
      getServletContext in interface jakarta.servlet.ServletRequest
    • getQueryString

      public String getQueryString()
      Specified by:
      getQueryString in interface jakarta.servlet.http.HttpServletRequest
    • getDispatcherType

      public jakarta.servlet.DispatcherType getDispatcherType()
      Specified by:
      getDispatcherType in interface jakarta.servlet.ServletRequest
    • getRequestId

      public String getRequestId()
      Specified by:
      getRequestId in interface jakarta.servlet.ServletRequest
    • getProtocolRequestId

      public String getProtocolRequestId()
      Specified by:
      getProtocolRequestId in interface jakarta.servlet.ServletRequest
    • getServletConnection

      public jakarta.servlet.ServletConnection getServletConnection()
      Specified by:
      getServletConnection in interface jakarta.servlet.ServletRequest
    • getParts

      public Collection<jakarta.servlet.http.Part> getParts()
      Specified by:
      getParts in interface jakarta.servlet.http.HttpServletRequest
    • getScheme

      public String getScheme()
      Specified by:
      getScheme in interface jakarta.servlet.ServletRequest
    • logout

      public void logout()
      Specified by:
      logout in interface jakarta.servlet.http.HttpServletRequest
    • getLocalName

      public String getLocalName()
      Specified by:
      getLocalName in interface jakarta.servlet.ServletRequest
    • isAsyncSupported

      public boolean isAsyncSupported()
      Specified by:
      isAsyncSupported in interface jakarta.servlet.ServletRequest
    • getParameterNames

      public Enumeration<String> getParameterNames()
      Specified by:
      getParameterNames in interface jakarta.servlet.ServletRequest
    • authenticate

      public boolean authenticate(jakarta.servlet.http.HttpServletResponse response)
      Specified by:
      authenticate in interface jakarta.servlet.http.HttpServletRequest
    • getPathTranslated

      public String getPathTranslated()
      Specified by:
      getPathTranslated in interface jakarta.servlet.http.HttpServletRequest
    • getIntHeader

      public int getIntHeader(String name)
      Specified by:
      getIntHeader in interface jakarta.servlet.http.HttpServletRequest
    • changeSessionId

      public String changeSessionId()
      Specified by:
      changeSessionId in interface jakarta.servlet.http.HttpServletRequest
    • getAsyncContext

      public jakarta.servlet.AsyncContext getAsyncContext()
      Specified by:
      getAsyncContext in interface jakarta.servlet.ServletRequest
    • getRequestURI

      public String getRequestURI()
      Specified by:
      getRequestURI in interface jakarta.servlet.http.HttpServletRequest
    • getRequestDispatcher

      public jakarta.servlet.RequestDispatcher getRequestDispatcher(String path)
      Specified by:
      getRequestDispatcher in interface jakarta.servlet.ServletRequest
    • isUserInRole

      public BiPredicate<Principal,String> isUserInRole()
      Returns the user-in-role checker.
      Returns:
      the checker deciding whether a principal holds a role
    • setUserInRole

      public void setUserInRole(BiPredicate<Principal,String> checker)
      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.
      use setUserInRole(BiPredicate), which takes the same argument
      Sets the user-in-role checker. Same as setUserInRole(BiPredicate).
      Parameters:
      checker - decides whether a principal holds a role
      Since:
      1.1
    • isUserInRole

      public boolean isUserInRole(String role)
      Set setUserInRole(BiPredicate) to modify the outcome of this function.
      Specified by:
      isUserInRole in interface jakarta.servlet.http.HttpServletRequest
    • getPathInfo

      public String getPathInfo()
      Specified by:
      getPathInfo in interface jakarta.servlet.http.HttpServletRequest
    • getRemoteUser

      public String getRemoteUser()
      Specified by:
      getRemoteUser in interface jakarta.servlet.http.HttpServletRequest
    • getCookiesInt

      public jakarta.servlet.http.Cookie[] getCookiesInt()
      Returns the cookies getCookies() reports, or null if none were added.
      Returns:
      the cookies, or null
    • setCookiesInt

      public void setCookiesInt(jakarta.servlet.http.Cookie[] cookiesInt)
      Replaces the cookies getCookies() reports.
      Parameters:
      cookiesInt - the cookies, or null for 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:
      getCookies in interface jakarta.servlet.http.HttpServletRequest
    • getLocaleInt

      public Locale getLocaleInt()
      Returns the locale getLocale() reports. Defaults to Locale.US.
      Returns:
      the locale
    • setLocaleInt

      public void setLocaleInt(Locale localeInt)
      Sets the locale getLocale() reports.
      Parameters:
      localeInt - the locale
    • getLocale

      public Locale getLocale()
      Specified by:
      getLocale in interface jakarta.servlet.ServletRequest
    • getMethod

      public String getMethod()
      Specified by:
      getMethod in interface jakarta.servlet.http.HttpServletRequest
    • getParameterMap

      public Map<String,String[]> getParameterMap()
      Specified by:
      getParameterMap in interface jakarta.servlet.ServletRequest
    • getAttributeNames

      public Enumeration<String> getAttributeNames()
      Specified by:
      getAttributeNames in interface jakarta.servlet.ServletRequest
    • getRemoteAddr

      public String getRemoteAddr()
      Specified by:
      getRemoteAddr in interface jakarta.servlet.ServletRequest
    • getHeaders

      public Enumeration<String> getHeaders(String name)
      Specified by:
      getHeaders in interface jakarta.servlet.http.HttpServletRequest
    • getUserPrincipalInt

      public Principal getUserPrincipalInt()
      Returns the principal getUserPrincipal() reports when no principal provider is set, or when the provider returns null.
      Returns:
      the principal, or null
    • setUserPrincipalInt

      public void setUserPrincipalInt(Principal userPrincipalInt)
      Sets the principal getUserPrincipal() reports when no principal provider is set, or when the provider returns null.
      Parameters:
      userPrincipalInt - the principal, or null for an anonymous request
    • getUserPrincipalProvider

      public Supplier<Principal> getUserPrincipalProvider()
      Returns the principal provider, if one has been set.
      Returns:
      the provider, or null if none has been set
      Since:
      1.1
    • principalProvider

      public void principalProvider(Supplier<Principal> supplier)
      Sets the user principal provider. Accepts a Supplier so 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 returns null, getUserPrincipal() falls back to that principal.

      Parameters:
      supplier - resolves the principal at call time
      Since:
      1.1
    • getUserPrincipal

      public Principal getUserPrincipal()
      Returns the principal from userPrincipalProvider if it is set and returns one, otherwise falls back to userPrincipalInt.
      Specified by:
      getUserPrincipal in interface jakarta.servlet.http.HttpServletRequest
    • getReader

      public BufferedReader getReader()
      Specified by:
      getReader in interface jakarta.servlet.ServletRequest
    • getLocales

      public Enumeration<Locale> getLocales()
      Specified by:
      getLocales in interface jakarta.servlet.ServletRequest
    • getAuthType

      public String getAuthType()
      Specified by:
      getAuthType in interface jakarta.servlet.http.HttpServletRequest
    • getCharacterEncoding

      public String getCharacterEncoding()
      Specified by:
      getCharacterEncoding in interface jakarta.servlet.ServletRequest
    • removeAttribute

      public void removeAttribute(String name)
      Specified by:
      removeAttribute in interface jakarta.servlet.ServletRequest
    • getContentLength

      public int getContentLength()
      Specified by:
      getContentLength in interface jakarta.servlet.ServletRequest
    • getHeaders

      public ConcurrentHashMap<String,List<String>> getHeaders()
      Returns the request headers, live and directly modifiable.
      Returns:
      the headers, keyed by name
    • getHeader

      public String getHeader(String headerName)
      Specified by:
      getHeader in interface jakarta.servlet.http.HttpServletRequest
    • getContextPath

      public String getContextPath()
      Specified by:
      getContextPath in interface jakarta.servlet.http.HttpServletRequest
    • getContentType

      public String getContentType()
      Specified by:
      getContentType in interface jakarta.servlet.ServletRequest
    • getHeaderNames

      public Enumeration<String> getHeaderNames()
      Specified by:
      getHeaderNames in interface jakarta.servlet.http.HttpServletRequest
    • getAttribute

      public Object getAttribute(String name)
      Specified by:
      getAttribute in interface jakarta.servlet.ServletRequest
    • setAttribute

      public void setAttribute(String name, Object value)
      Specified by:
      setAttribute in interface jakarta.servlet.ServletRequest
    • getParameter

      public String getParameter(String parameter)
      Specified by:
      getParameter in interface jakarta.servlet.ServletRequest
    • getRemotePort

      public int getRemotePort()
      Specified by:
      getRemotePort in interface jakarta.servlet.ServletRequest
    • getDateHeader

      public long getDateHeader(String name)
      Specified by:
      getDateHeader in interface jakarta.servlet.http.HttpServletRequest
    • getRemoteHost

      public String getRemoteHost()
      Specified by:
      getRemoteHost in interface jakarta.servlet.ServletRequest
    • isSecure

      public boolean isSecure()
      Specified by:
      isSecure in interface jakarta.servlet.ServletRequest
    • setParameter

      public void setParameter(String name, String... values)
      Equivalent to parameters.put(name, Arrays.copyOf(values, values.length)).
      Parameters:
      name - the attribute name
      values - the values of the parameter