Class MockServletRequest

java.lang.Object
org.apache.juneau.rest.mock.MockServletRequest
All Implemented Interfaces:
jakarta.servlet.http.HttpServletRequest, jakarta.servlet.ServletRequest

public class MockServletRequest extends Object implements jakarta.servlet.http.HttpServletRequest
A mutable implementation of HttpServletRequest for mocking purposes.
See Also:
  • Constructor Details

  • Method Details

    • create

      public static MockServletRequest create()
      Creates a new servlet request. Initialized with the following:
      • "Accept: text/json5"
      • "Content-Type: text/json"
      Returns:
      A new request.
    • create

      public static MockServletRequest create(String method, String uri, Object... pathArgs)
      Creates a new servlet request with the specified method name and request path. Initialized with the following:
      • "Accept: text/json5"
      • "Content-Type: text/json"
      Parameters:
      method - The HTTP method name.
      uri - The request path.
      pathArgs - Optional path arguments.
      Returns:
      A new request.
    • applyOverrides

      If the specified request is a MockRestRequest, applies any of the override values to this servlet request.
      Parameters:
      req - The request to copy overrides from.
      Returns:
      This object.
    • attribute

      public MockServletRequest attribute(String name, Object value)
      Fluent setter.
      Parameters:
      name - Request attribute name.
      value - Request attribute value.
      Returns:
      This object.
    • authenticate

      public boolean authenticate(jakarta.servlet.http.HttpServletResponse response) throws IOException, jakarta.servlet.ServletException
      Specified by:
      authenticate in interface jakarta.servlet.http.HttpServletRequest
      Throws:
      IOException
      jakarta.servlet.ServletException
    • authType

      Fluent setter.
      Parameters:
      value - The auth type.
      Returns:
      This object.
    • changeSessionId

      Specified by:
      changeSessionId in interface jakarta.servlet.http.HttpServletRequest
    • characterEncoding

      Fluent setter.
      Parameters:
      value - The character encoding.
      Returns:
      This object.
    • content

      Fluent setter.
      Parameters:
      value - The body of the request.
      Can be any of the following data types: Any other types are converted to a string using the toString() method.
      Returns:
      This object.
    • contextPath

      Fluent setter.
      Parameters:
      value - The context path.
      Returns:
      This object.
    • cookies

      public MockServletRequest cookies(jakarta.servlet.http.Cookie[] value)
      Fluent setter.
      Parameters:
      value - The cookies.
      Returns:
      This object.
    • dispatcherType

      public MockServletRequest dispatcherType(jakarta.servlet.DispatcherType value)
      Fluent setter.
      Parameters:
      value - The dispatcher type.
      Returns:
      This object.
    • getAsyncContext

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

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

      Specified by:
      getAttributeNames in interface jakarta.servlet.ServletRequest
    • getAuthType

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

      Specified by:
      getCharacterEncoding in interface jakarta.servlet.ServletRequest
    • getContentLength

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

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

      Specified by:
      getContentType in interface jakarta.servlet.ServletRequest
    • getContextPath

      Specified by:
      getContextPath in interface jakarta.servlet.http.HttpServletRequest
    • getCookies

      public jakarta.servlet.http.Cookie[] getCookies()
      Specified by:
      getCookies in interface jakarta.servlet.http.HttpServletRequest
    • getDateHeader

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

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

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

      Specified by:
      getHeaderNames in interface jakarta.servlet.http.HttpServletRequest
    • getHeaders

      Specified by:
      getHeaders in interface jakarta.servlet.http.HttpServletRequest
    • getInputStream

      public jakarta.servlet.ServletInputStream getInputStream() throws IOException
      Specified by:
      getInputStream in interface jakarta.servlet.ServletRequest
      Throws:
      IOException
    • getIntHeader

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

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

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

      Specified by:
      getLocales in interface jakarta.servlet.ServletRequest
    • getLocalName

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

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

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

      public String getParameter(String name)
      Specified by:
      getParameter in interface jakarta.servlet.ServletRequest
    • getParameterMap

      Specified by:
      getParameterMap in interface jakarta.servlet.ServletRequest
    • getParameterNames

      Specified by:
      getParameterNames in interface jakarta.servlet.ServletRequest
    • getParameterValues

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

      public jakarta.servlet.http.Part getPart(String name) throws IOException, jakarta.servlet.ServletException
      Specified by:
      getPart in interface jakarta.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 interface jakarta.servlet.http.HttpServletRequest
      Throws:
      IOException
      jakarta.servlet.ServletException
    • getPathInfo

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

      Specified by:
      getPathTranslated in interface jakarta.servlet.http.HttpServletRequest
    • getProtocol

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

      Specified by:
      getProtocolRequestId in interface jakarta.servlet.ServletRequest
    • getQueryString

      Specified by:
      getQueryString in interface jakarta.servlet.http.HttpServletRequest
    • getReader

      Specified by:
      getReader in interface jakarta.servlet.ServletRequest
      Throws:
      IOException
    • getRemoteAddr

      Specified by:
      getRemoteAddr in interface jakarta.servlet.ServletRequest
    • getRemoteHost

      Specified by:
      getRemoteHost in interface jakarta.servlet.ServletRequest
    • getRemotePort

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

      Specified by:
      getRemoteUser in interface jakarta.servlet.http.HttpServletRequest
    • getRequestDispatcher

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

      Specified by:
      getRequestedSessionId in interface jakarta.servlet.http.HttpServletRequest
    • getRequestId

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

      Specified by:
      getRequestURI in interface jakarta.servlet.http.HttpServletRequest
    • getRequestURL

      Specified by:
      getRequestURL in interface jakarta.servlet.http.HttpServletRequest
    • getScheme

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

      Specified by:
      getServerName in interface jakarta.servlet.ServletRequest
    • getServerPort

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

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

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

      Specified by:
      getServletPath in interface jakarta.servlet.http.HttpServletRequest
    • getSession

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

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

      Specified by:
      getUserPrincipal in interface jakarta.servlet.http.HttpServletRequest
    • header

      public MockServletRequest header(String name, Object value)
      Fluent setter.
      Parameters:
      name - Header name.
      value - Header value.
      The value is converted to a simple string using Object.toString().
      Returns:
      This object.
    • httpSession

      public MockServletRequest httpSession(jakarta.servlet.http.HttpSession value)
      Fluent setter.
      Parameters:
      value - The HTTP session.
      Returns:
      This object.
    • isAsyncStarted

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

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

      Specified by:
      isRequestedSessionIdFromCookie in interface jakarta.servlet.http.HttpServletRequest
    • isRequestedSessionIdFromURL

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

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

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

      public boolean isUserInRole(String role)
      Specified by:
      isUserInRole in interface jakarta.servlet.http.HttpServletRequest
    • localAddr

      Fluent setter.
      Parameters:
      value - The local address.
      Returns:
      This object.
    • locale

      Fluent setter.
      Parameters:
      value - The locale.
      Returns:
      This object.
    • localName

      Fluent setter.
      Parameters:
      value - The local name.
      Returns:
      This object.
    • localPort

      public MockServletRequest localPort(int value)
      Fluent setter.
      Parameters:
      value - The local port.
      Returns:
      This object.
    • login

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

      public void logout() throws jakarta.servlet.ServletException
      Specified by:
      logout in interface jakarta.servlet.http.HttpServletRequest
      Throws:
      jakarta.servlet.ServletException
    • method

      Fluent setter.
      Parameters:
      value - The method name for this request.
      Returns:
      This object.
    • noTrace

      public MockServletRequest noTrace(boolean value)
      Enabled debug mode on this request.

      Prevents errors from being logged on the server side if no-trace per-request is enabled.

      Parameters:
      value - The enable flag value.
      Returns:
      This object.
    • pathInfo

      Fluent setter.
      Parameters:
      value - The path info.
      Returns:
      This object.
    • pathTranslated

      Fluent setter.
      Parameters:
      value - The path translated.
      Returns:
      This object.
    • pathVars

      Adds the specified parent path variables to this servlet request.

      See MockRestClient.Builder.pathVars(Map) for an example.

      Parameters:
      pathVars - The parent path variables to add. Can be null (no-op).
      Returns:
      This object.
      See Also:
    • pathVars

      public MockServletRequest pathVars(String... pairs)
      Add resolved path variables to this client.

      Identical to pathVars(Map) but allows you to specify as a list of key/value pairs.

      Parameters:
      pairs - The key/value pairs. Must be an even number of parameters.
      Returns:
      This object.
    • protocol

      Fluent setter.
      Parameters:
      value - The protocol.
      Returns:
      This object.
    • queryString

      Fluent setter.
      Parameters:
      value - The query string.
      Returns:
      This object.
    • remoteAddr

      Fluent setter.
      Parameters:
      value - The remote address.
      Returns:
      This object.
    • remoteHost

      Fluent setter.
      Parameters:
      value - The remote port.
      Returns:
      This object.
    • remotePort

      public MockServletRequest remotePort(int value)
      Fluent setter.
      Parameters:
      value - The remote port.
      Returns:
      This object.
    • remoteUser

      Fluent setter.
      Parameters:
      value - The remote user.
      Returns:
      This object.
    • removeAttribute

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

      public MockServletRequest requestDispatcher(String name, jakarta.servlet.RequestDispatcher value)
      Fluent setter.
      Parameters:
      name - The path to resolve.
      value - The request dispatcher.
      Returns:
      This object.
    • requestedSessionId

      Fluent setter.
      Parameters:
      value - The requested session ID.
      Returns:
      This object.
    • requestURI

      Fluent setter.
      Parameters:
      value - The request URI.
      Returns:
      This object.
    • role

      Sets the specified role on this request.
      Parameters:
      role - The role to add to this request (e.g. "ROLE_ADMIN").
      Returns:
      This object.
    • roles

      public MockServletRequest roles(String... roles)
      Sets the specified roles on this request.
      Parameters:
      roles - The roles to add to this request (e.g. "ROLE_ADMIN").
      Returns:
      This object.
    • scheme

      Fluent setter.
      Parameters:
      value - The scheme.
      Returns:
      This object.
    • serverName

      Fluent setter.
      Parameters:
      value - The server name.
      Returns:
      This object.
    • serverPort

      public MockServletRequest serverPort(int value)
      Fluent setter.
      Parameters:
      value - The server port.
      Returns:
      This object.
    • servletContext

      public MockServletRequest servletContext(jakarta.servlet.ServletContext value)
      Fluent setter.
      Parameters:
      value - The servlet context.
      Returns:
      This object.
    • servletPath

      Fluent setter.
      Parameters:
      value - The servlet path.
      Returns:
      This object.
    • setAttribute

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

      public void setCharacterEncoding(String characterEncoding) throws UnsupportedEncodingException
      Specified by:
      setCharacterEncoding in interface jakarta.servlet.ServletRequest
      Throws:
      UnsupportedEncodingException
    • startAsync

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

      public jakarta.servlet.AsyncContext startAsync(jakarta.servlet.ServletRequest servletRequest, jakarta.servlet.ServletResponse servletResponse) throws IllegalStateException
      Specified by:
      startAsync in interface jakarta.servlet.ServletRequest
      Throws:
      IllegalStateException
    • upgrade

      public <T extends jakarta.servlet.http.HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, jakarta.servlet.ServletException
      Specified by:
      upgrade in interface jakarta.servlet.http.HttpServletRequest
      Throws:
      IOException
      jakarta.servlet.ServletException
    • uri

      Fluent setter.
      Parameters:
      uri - The URI of the request.
      Returns:
      This object.
    • userPrincipal

      Fluent setter.
      Parameters:
      value - The user principal.
      Returns:
      This object.
    • logLevel

      Sets the JUL log level to transiently apply to the target resource's logger for the duration of this request's dispatch.

      This is a pure data holder read by the mock transport at the actual dispatch call site, which is responsible for saving the resource logger's current level, applying this value, invoking RestContext.execute(...), and restoring the prior level in a finally block. The mutation is therefore transient and never leaks across requests or tests. Replaces the removed debug(boolean) flag, which set a now-meaningless Debug request header.

      Parameters:
      value - The log level to apply during dispatch, or null for no override.
      Returns:
      This object.
    • getLogLevel

      public Level getLogLevel()
      Returns the log level set via logLevel(Level).
      Returns:
      The log level to apply during dispatch, or null if not set.