Class MockRequest

  • All Implemented Interfaces:
    javax.servlet.http.HttpServletRequest, javax.servlet.ServletRequest

    public class MockRequest
    extends java.lang.Object
    implements javax.servlet.http.HttpServletRequest

    Mock implementation of HttpServletRequest.

    Implements all of the methods from the standard HttpServletRequest class plus helper methods to aid setting up a request.

    This class was adapted from Apache Click.

    Since:
    5.0
    See Also:
    HttpServletRequest
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String REMOTE_USER
      The REMOTE_USER header.
      • Fields inherited from interface javax.servlet.http.HttpServletRequest

        BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
    • Constructor Summary

      Constructors 
      Constructor Description
      MockRequest()
      Create new MockRequest.
      MockRequest​(java.util.Locale locale)
      Create new MockRequest for the specified local.
      MockRequest​(java.util.Locale locale, java.lang.String contextPath, java.lang.String servletPath, javax.servlet.ServletContext servletContext, javax.servlet.http.HttpSession session)
      Create a new MockRequest for the specified arguments.
      MockRequest​(java.util.Locale locale, javax.servlet.ServletContext servletContext)
      Create a new MockRequest for the specified locale and servletContext.
      MockRequest​(java.util.Locale locale, javax.servlet.ServletContext servletContext, javax.servlet.http.HttpSession session)
      Create a new MockRequest for the specified arguments.
      MockRequest​(javax.servlet.ServletContext servletContext)
      Create a new MockRequest for the specified context.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      MockRequest addCookie​(javax.servlet.http.Cookie cookie)
      Add a new cookie.
      MockRequest addHeader​(java.lang.String name, java.lang.String value)
      Add a header to the request.
      boolean authenticate​(javax.servlet.http.HttpServletResponse response)  
      java.lang.String changeSessionId()  
      javax.servlet.AsyncContext getAsyncContext()  
      java.lang.Object getAttribute​(java.lang.String name)
      Get an attribute.
      java.util.Enumeration<java.lang.String> getAttributeNames()
      Get the names of all of the values.
      java.lang.String getAuthType()
      Get the auth type.
      java.lang.String getCharacterEncoding()
      Get the current character encoding.
      int getContentLength()
      Return the length of the content.
      long getContentLengthLong()  
      java.lang.String getContentType()
      If useMultiPartContentType set as true return the correct content-type.
      java.lang.String getContextPath()
      Returns the portion of the request URI that indicates the context of the request.
      javax.servlet.http.Cookie[] getCookies()
      Get all of the cookies for this request.
      long getDateHeader​(java.lang.String name)
      Get the given header as a date.
      javax.servlet.DispatcherType getDispatcherType()  
      java.lang.String getForward()
      Returns the url that was forwarded to, otherwise return null.
      java.lang.String getHeader​(java.lang.String name)
      Get the given header value.
      java.util.Enumeration<java.lang.String> getHeaderNames()
      Get the names of all of the headers.
      java.util.Map<java.lang.String,​java.util.List<java.lang.String>> getHeaders()
      Return the map of headers for this request.
      java.util.Enumeration<java.lang.String> getHeaders​(java.lang.String name)
      Get enumeration of all header values with the given name.
      java.util.List<java.lang.String> getIncludes()
      Returns the list of server side included url's.
      javax.servlet.ServletInputStream getInputStream()
      Returns an input stream if there has been added some uploaded files.
      int getIntHeader​(java.lang.String name)
      Get the given header as an int.
      java.lang.String getLocalAddr()
      Return the local address, "127.0.0.1".
      java.util.Locale getLocale()
      Get the locale of the request.
      java.util.Enumeration<java.util.Locale> getLocales()
      Return all the accepted locales.
      java.lang.String getLocalName()
      Return the local name, "127.0.0.1".
      int getLocalPort()
      Return the local port, 80.
      java.lang.String getMethod()
      Get the method.
      java.lang.String getParameter​(java.lang.String name)
      Get the request parameter with the given name.
      java.util.Map<java.lang.String,​java.lang.String[]> getParameterMap()
      Get the map of all of the parameters.
      java.util.Enumeration<java.lang.String> getParameterNames()
      Get the names of all of the parameters.
      java.lang.String[] getParameterValues​(java.lang.String name)
      Get the values for the given parameter.
      javax.servlet.http.Part getPart​(java.lang.String name)  
      java.util.Collection<javax.servlet.http.Part> getParts()  
      java.lang.String getPathInfo()
      Get the path info.
      java.lang.String getPathTranslated()
      Always returns null.
      java.lang.String getProtocol()
      Get the protocol.
      java.lang.String getQueryString()
      Get the query string part of the request.
      java.io.BufferedReader getReader()
      This feature is not implemented at this time as we are not supporting binary servlet input.
      java.lang.String getRealPath​(java.lang.String name)
      Deprecated.
      Use ServletContext.getRealPath(String) instead.
      java.lang.String getRemoteAddr()
      Get the remote address of the client.
      java.lang.String getRemoteHost()
      Get the remote host.
      int getRemotePort()
      Return the remote port, 80.
      java.lang.String getRemoteUser()
      Return the name of the userPrincipal if set, otherwise the value of the "REMOTE_USER" header.
      javax.servlet.RequestDispatcher getRequestDispatcher​(java.lang.String path)
      Returns a RequestDispatcher for the specified path.
      java.lang.String getRequestedSessionId()
      Get the requested session id.
      java.lang.String getRequestURI()
      Returns context path and servlet path concatenated, typically /applicationClassName/applicationClassName.
      java.lang.StringBuffer getRequestURL()
      Returns (an attempt at) a reconstructed URL based on it's constituent parts.
      java.lang.String getScheme()
      Get the scheme http, https, or ftp.
      java.lang.String getServerName()
      Get the host server name to which the request was sent.
      int getServerPort()
      Returns the port number to which the request was sent.
      javax.servlet.ServletContext getServletContext()
      Return the servlet context.
      java.lang.String getServletPath()
      Return a String containing the name or path of the servlet being called.
      javax.servlet.http.HttpSession getSession()
      Returns the current HttpSession associated with this request.
      javax.servlet.http.HttpSession getSession​(boolean create)
      Returns the current HttpSession associated with this request.
      java.security.Principal getUserPrincipal()
      Get the user principal.
      void initialize()
      Reset the request back to a default state.
      boolean isAsyncStarted()  
      boolean isAsyncSupported()  
      boolean isPost()
      Return whether the request is a post or not.
      boolean isRequestedSessionIdFromCookie()
      Check whether session id is from a cookie.
      boolean isRequestedSessionIdFromUrl()
      Check whether session id is from a url rewrite.
      boolean isRequestedSessionIdFromURL()
      Check whether session id is from a url rewrite.
      boolean isRequestedSessionIdValid()
      Check whether the session id is valid.
      boolean isSecure()
      Always returns false.
      boolean isUserInRole​(java.lang.String role)
      Returns true if the authenticated user is included in the given role, false otherwise.
      void login​(java.lang.String username, java.lang.String password)  
      void logout()  
      void removeAttribute​(java.lang.String name)
      Remove the given attribute.
      MockRequest removeParameter​(java.lang.String name)
      Remove the specified parameter.
      void reset()
      Delegate to initialize method.
      void setAttribute​(java.lang.String name, java.lang.Object o)
      Set the given attribute.
      void setAuthType​(java.lang.String authType)
      Set the auth type.
      void setCharacterEncoding​(java.lang.String encoding)
      Set the character encoding.
      MockRequest setContentType​(java.lang.String contentType)
      Set the request content type.
      MockRequest setContextPath​(java.lang.String contextPath)
      Set the portion of the request URI that indicates the context of the request.
      void setCookies​(javax.servlet.http.Cookie[] theCookies)
      Set the cookies.
      MockRequest setHeader​(java.lang.String name, java.lang.String value)
      Set request header value.
      MockRequest setHeader​(java.lang.String name, java.lang.String... values)
      Set request header values.
      MockRequest setHttpSession​(javax.servlet.http.HttpSession session)
      Set the request's session instance.
      MockRequest setMethod​(java.lang.String method)
      Set the method.
      MockRequest setParameter​(java.lang.String name, java.lang.String value)
      Set a parameter.
      MockRequest setParameter​(java.lang.String name, java.lang.String[] values)
      Set the specified parameter name to the array of strings.
      MockRequest setParameters​(java.util.Map<java.lang.String,​java.lang.Object> parameters)
      Sets a map of parameters.
      MockRequest setPathInfo​(java.lang.String path)
      Set the path that this request is supposed to be serving.
      void setRequestURL​(java.lang.String requestURL)
      Provide a convenience method for setting the request URL.
      MockRequest setScheme​(java.lang.String scheme)
      Set the request's scheme, for example http, https, or ftp.
      MockRequest setServerName​(java.lang.String serverName)
      Sets the host server name to which the request was sent.
      MockRequest setServerPort​(int serverPort)
      Set the port number to which the request was sent.
      MockRequest setServletContext​(javax.servlet.ServletContext servletContext)
      Set the request's servletContext instance.
      MockRequest setServletPath​(java.lang.String servletPath)
      Set the string containing the name or path of the servlet being called.
      MockRequest setSession​(javax.servlet.http.HttpSession session)
      Set the current HttpSession associated with this request.
      MockRequest setUseMultiPartContentType​(boolean useMultiPartContentType)
      True will force Request to generate multiPart ContentType and ContentLength.
      MockRequest setUserPrincipal​(java.security.Principal userPrincipal)
      Set the user principal.
      javax.servlet.AsyncContext startAsync()  
      javax.servlet.AsyncContext startAsync​(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse)  
      java.lang.String toString()
      Returns the String representation of the mock request.
      javax.servlet.http.HttpUpgradeHandler upgrade​(java.lang.Class handlerClass)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface javax.servlet.http.HttpServletRequest

        getHttpServletMapping, getTrailerFields, isTrailerFieldsReady, newPushBuilder
    • Field Detail

      • REMOTE_USER

        public static final java.lang.String REMOTE_USER
        The REMOTE_USER header.
        See Also:
        Constant Field Values
    • Constructor Detail

      • MockRequest

        public MockRequest()
        Create new MockRequest.
      • MockRequest

        public MockRequest​(java.util.Locale locale)
        Create new MockRequest for the specified local.
        Parameters:
        locale - locale for this request
      • MockRequest

        public MockRequest​(javax.servlet.ServletContext servletContext)
        Create a new MockRequest for the specified context.
        Parameters:
        servletContext - the servletContext for this request
      • MockRequest

        public MockRequest​(java.util.Locale locale,
                           javax.servlet.ServletContext servletContext)
        Create a new MockRequest for the specified locale and servletContext.
        Parameters:
        locale - locale for this request
        servletContext - the servletContext for this request
      • MockRequest

        public MockRequest​(java.util.Locale locale,
                           javax.servlet.ServletContext servletContext,
                           javax.servlet.http.HttpSession session)
        Create a new MockRequest for the specified arguments.
        Parameters:
        locale - The request locale, or null to use the default locale
        session - The session object
        servletContext - The current servlet context
      • MockRequest

        public MockRequest​(java.util.Locale locale,
                           java.lang.String contextPath,
                           java.lang.String servletPath,
                           javax.servlet.ServletContext servletContext,
                           javax.servlet.http.HttpSession session)
        Create a new MockRequest for the specified arguments.
        Parameters:
        locale - The request locale, or null to use the default locale
        contextPath - the request context path
        servletPath - the request servlet path
        servletContext - The current servlet context
        session - the request session
    • Method Detail

      • setServletContext

        public MockRequest setServletContext​(javax.servlet.ServletContext servletContext)
        Set the request's servletContext instance.
        Parameters:
        servletContext - the new ServletContext instance
        Returns:
        the MockRequest object
      • setHttpSession

        public MockRequest setHttpSession​(javax.servlet.http.HttpSession session)
        Set the request's session instance.
        Parameters:
        session - the new HttpSession instance
        Returns:
        the MockRequest object
      • addCookie

        public MockRequest addCookie​(javax.servlet.http.Cookie cookie)
        Add a new cookie.
        Parameters:
        cookie - The cookie
        Returns:
        the MockRequest object
      • addHeader

        public MockRequest addHeader​(java.lang.String name,
                                     java.lang.String value)
        Add a header to the request.
        Parameters:
        name - the name of the header to add
        value - the value
        Returns:
        the MockRequest object
      • setHeader

        public MockRequest setHeader​(java.lang.String name,
                                     java.lang.String value)
        Set request header value. The existing header value will be replaced.
        Parameters:
        name - the name of the header to set
        value - the header value
        Returns:
        the MockRequest object
      • setHeader

        public MockRequest setHeader​(java.lang.String name,
                                     java.lang.String... values)
        Set request header values. The existing header values will be replaced.
        Parameters:
        name - the name of the header to set
        values - the header values
        Returns:
        the MockRequest object
      • getAttribute

        public java.lang.Object getAttribute​(java.lang.String name)
        Get an attribute.
        Specified by:
        getAttribute in interface javax.servlet.ServletRequest
        Parameters:
        name - The attribute name
        Returns:
        The value, or null
      • getAttributeNames

        public java.util.Enumeration<java.lang.String> getAttributeNames()
        Get the names of all of the values.
        Specified by:
        getAttributeNames in interface javax.servlet.ServletRequest
        Returns:
        The names
      • getAuthType

        public java.lang.String getAuthType()
        Get the auth type.
        Specified by:
        getAuthType in interface javax.servlet.http.HttpServletRequest
        Returns:
        The auth type
      • getCharacterEncoding

        public java.lang.String getCharacterEncoding()
        Get the current character encoding.
        Specified by:
        getCharacterEncoding in interface javax.servlet.ServletRequest
        Returns:
        The character encoding
      • setUseMultiPartContentType

        public MockRequest setUseMultiPartContentType​(boolean useMultiPartContentType)
        True will force Request to generate multiPart ContentType and ContentLength.
        Parameters:
        useMultiPartContentType - true if the request is multi-part, false otherwise
        Returns:
        the MockRequest object
      • getContentLength

        public int getContentLength()
        Return the length of the content. This is always -1 except if useMultiPartContentType set as true. Then the length will be the length of the generated request.
        Specified by:
        getContentLength in interface javax.servlet.ServletRequest
        Returns:
        -1 if useMultiPartContentType is false. Else the length of the generated request.
      • setContentType

        public MockRequest setContentType​(java.lang.String contentType)
        Set the request content type.
        Parameters:
        contentType - the request content type.
        Returns:
        the MockRequest object
      • getContentType

        public java.lang.String getContentType()
        If useMultiPartContentType set as true return the correct content-type. If the contentType property is defined then this value will be returned.
        Specified by:
        getContentType in interface javax.servlet.ServletRequest
        Returns:
        The correct multipart content-type if useMultiPartContentType is true. Else null.
      • getCookies

        public javax.servlet.http.Cookie[] getCookies()
        Get all of the cookies for this request.
        Specified by:
        getCookies in interface javax.servlet.http.HttpServletRequest
        Returns:
        The cookies
      • getDateHeader

        public long getDateHeader​(java.lang.String name)
        Get the given header as a date.
        Specified by:
        getDateHeader in interface javax.servlet.http.HttpServletRequest
        Parameters:
        name - The header name
        Returns:
        The date, or -1 if header not found
      • getHeader

        public java.lang.String getHeader​(java.lang.String name)
        Get the given header value.
        Specified by:
        getHeader in interface javax.servlet.http.HttpServletRequest
        Parameters:
        name - The header name
        Returns:
        The header value or null
      • getHeaderNames

        public java.util.Enumeration<java.lang.String> getHeaderNames()
        Get the names of all of the headers.
        Specified by:
        getHeaderNames in interface javax.servlet.http.HttpServletRequest
        Returns:
        The header names
      • getHeaders

        public java.util.Enumeration<java.lang.String> getHeaders​(java.lang.String name)
        Get enumeration of all header values with the given name.
        Specified by:
        getHeaders in interface javax.servlet.http.HttpServletRequest
        Parameters:
        name - The name
        Returns:
        The header values
      • getHeaders

        public java.util.Map<java.lang.String,​java.util.List<java.lang.String>> getHeaders()
        Return the map of headers for this request.
        Returns:
        the map of headers for this request
      • getInputStream

        public javax.servlet.ServletInputStream getInputStream()
                                                        throws java.io.IOException
        Returns an input stream if there has been added some uploaded files.
        Specified by:
        getInputStream in interface javax.servlet.ServletRequest
        Returns:
        The input stream
        Throws:
        java.io.IOException - If an I/O related problem occurs
      • getIntHeader

        public int getIntHeader​(java.lang.String name)
        Get the given header as an int.
        Specified by:
        getIntHeader in interface javax.servlet.http.HttpServletRequest
        Parameters:
        name - The header name
        Returns:
        The header value or -1 if header not found
      • getLocale

        public java.util.Locale getLocale()
        Get the locale of the request. Attempts to decode the Accept-Language header and if not found returns the default locale of the JVM.
        Specified by:
        getLocale in interface javax.servlet.ServletRequest
        Returns:
        The locale
      • getLocales

        public java.util.Enumeration<java.util.Locale> getLocales()
        Return all the accepted locales. This implementation always returns just one.
        Specified by:
        getLocales in interface javax.servlet.ServletRequest
        Returns:
        The locales
      • getMethod

        public java.lang.String getMethod()
        Get the method. The returned string will be in upper case eg. POST.
        Specified by:
        getMethod in interface javax.servlet.http.HttpServletRequest
        Returns:
        The method
      • getParameter

        public java.lang.String getParameter​(java.lang.String name)
        Get the request parameter with the given name.
        Specified by:
        getParameter in interface javax.servlet.ServletRequest
        Parameters:
        name - The parameter name
        Returns:
        The parameter value, or null
      • getParameterMap

        public java.util.Map<java.lang.String,​java.lang.String[]> getParameterMap()
        Get the map of all of the parameters.
        Specified by:
        getParameterMap in interface javax.servlet.ServletRequest
        Returns:
        The parameters
      • getParameterNames

        public java.util.Enumeration<java.lang.String> getParameterNames()
        Get the names of all of the parameters.
        Specified by:
        getParameterNames in interface javax.servlet.ServletRequest
        Returns:
        The parameter names
      • getParameterValues

        public java.lang.String[] getParameterValues​(java.lang.String name)
        Get the values for the given parameter.
        Specified by:
        getParameterValues in interface javax.servlet.ServletRequest
        Parameters:
        name - The name of the parameter
        Returns:
        The return values
      • getPathInfo

        public java.lang.String getPathInfo()
        Get the path info.
        Specified by:
        getPathInfo in interface javax.servlet.http.HttpServletRequest
        Returns:
        The path info
      • getPathTranslated

        public java.lang.String getPathTranslated()
        Always returns null.
        Specified by:
        getPathTranslated in interface javax.servlet.http.HttpServletRequest
        Returns:
        null
      • getProtocol

        public java.lang.String getProtocol()
        Get the protocol.
        Specified by:
        getProtocol in interface javax.servlet.ServletRequest
        Returns:
        Always HTTP/1.1
      • getQueryString

        public java.lang.String getQueryString()
        Get the query string part of the request.
        Specified by:
        getQueryString in interface javax.servlet.http.HttpServletRequest
        Returns:
        The query string
      • getReader

        public java.io.BufferedReader getReader()
                                         throws java.io.IOException
        This feature is not implemented at this time as we are not supporting binary servlet input. This functionality may be added in the future.
        Specified by:
        getReader in interface javax.servlet.ServletRequest
        Returns:
        The reader
        Throws:
        java.io.IOException - If an I/O related problem occurs
      • getRealPath

        public java.lang.String getRealPath​(java.lang.String name)
        Deprecated.
        Use ServletContext.getRealPath(String) instead.
        Deprecated method - should not be used.
        Specified by:
        getRealPath in interface javax.servlet.ServletRequest
        Parameters:
        name - The name
        Returns:
        The path
      • getRemoteAddr

        public java.lang.String getRemoteAddr()
        Get the remote address of the client.
        Specified by:
        getRemoteAddr in interface javax.servlet.ServletRequest
        Returns:
        Always 127.0.0.1
      • getRemoteHost

        public java.lang.String getRemoteHost()
        Get the remote host.
        Specified by:
        getRemoteHost in interface javax.servlet.ServletRequest
        Returns:
        Always localhost
      • getRemoteUser

        public java.lang.String getRemoteUser()

        Return the name of the userPrincipal if set, otherwise the value of the "REMOTE_USER" header.

        To set the remote user, create an instance of a MockPrincipal and set it on the request through the method setUserPrincipal(java.security.Principal).

        Specified by:
        getRemoteUser in interface javax.servlet.http.HttpServletRequest
        Returns:
        the name of the remote user
      • getLocalAddr

        public java.lang.String getLocalAddr()
        Return the local address, "127.0.0.1".
        Specified by:
        getLocalAddr in interface javax.servlet.ServletRequest
        Returns:
        "127.0.0.1" as the local address
      • getLocalName

        public java.lang.String getLocalName()
        Return the local name, "127.0.0.1".
        Specified by:
        getLocalName in interface javax.servlet.ServletRequest
        Returns:
        "127.0.0.1" as the local name
      • getLocalPort

        public int getLocalPort()
        Return the local port, 80.
        Specified by:
        getLocalPort in interface javax.servlet.ServletRequest
        Returns:
        80 as the local port
      • getRemotePort

        public int getRemotePort()
        Return the remote port, 80.
        Specified by:
        getRemotePort in interface javax.servlet.ServletRequest
        Returns:
        80 as the remote port
      • getRequestDispatcher

        public javax.servlet.RequestDispatcher getRequestDispatcher​(java.lang.String path)
        Returns a RequestDispatcher for the specified path. The dispatcher will not dispatch to the resource. It only records the specified path so that one can test if the correct path was dispatched to.
        Specified by:
        getRequestDispatcher in interface javax.servlet.ServletRequest
        Parameters:
        path - a String specifying the pathname to the resource
        Returns:
        a dispatcher for the specified path
      • getRequestedSessionId

        public java.lang.String getRequestedSessionId()
        Get the requested session id. Always returns the id of the current session.
        Specified by:
        getRequestedSessionId in interface javax.servlet.http.HttpServletRequest
        Returns:
        The session id
      • getRequestURI

        public java.lang.String getRequestURI()
        Returns context path and servlet path concatenated, typically /applicationClassName/applicationClassName.
        Specified by:
        getRequestURI in interface javax.servlet.http.HttpServletRequest
        Returns:
        The path value
        See Also:
        HttpServletRequest.getRequestURI()
      • setRequestURL

        public void setRequestURL​(java.lang.String requestURL)
        Provide a convenience method for setting the request URL.
        Parameters:
        requestURL - the request URL to set
      • getRequestURL

        public java.lang.StringBuffer getRequestURL()
        Returns (an attempt at) a reconstructed URL based on it's constituent parts. If the requestURL property is set this value will be returned instead.
        Specified by:
        getRequestURL in interface javax.servlet.http.HttpServletRequest
        Returns:
        a StringBuffer object containing the reconstructed URL
        See Also:
        HttpServletRequest.getRequestURL()
      • isPost

        public boolean isPost()
        Return whether the request is a post or not.
        Returns:
        true if the request is a post, false otherwise
      • getScheme

        public java.lang.String getScheme()
        Get the scheme http, https, or ftp.
        Specified by:
        getScheme in interface javax.servlet.ServletRequest
        Returns:
        the scheme used by this request
      • setScheme

        public MockRequest setScheme​(java.lang.String scheme)
        Set the request's scheme, for example http, https, or ftp.
        Parameters:
        scheme - the request's scheme
        Returns:
        the MockRequest object
      • getServerName

        public java.lang.String getServerName()
        Get the host server name to which the request was sent.
        Specified by:
        getServerName in interface javax.servlet.ServletRequest
        Returns:
        always the host server name
      • setServerName

        public MockRequest setServerName​(java.lang.String serverName)
        Sets the host server name to which the request was sent.
        Parameters:
        serverName - the server name the request was sent to
        Returns:
        the MockRequest object
      • getServerPort

        public int getServerPort()
        Returns the port number to which the request was sent.
        Specified by:
        getServerPort in interface javax.servlet.ServletRequest
        Returns:
        the server port to which the request was sent
      • setServerPort

        public MockRequest setServerPort​(int serverPort)
        Set the port number to which the request was sent.
        Parameters:
        serverPort - the port number to which the request was sent
        Returns:
        the MockRequest object
      • getContextPath

        public java.lang.String getContextPath()
        Returns the portion of the request URI that indicates the context of the request.
        Specified by:
        getContextPath in interface javax.servlet.http.HttpServletRequest
        Returns:
        the portion of the request URI that indicates the context of the request.
      • setContextPath

        public MockRequest setContextPath​(java.lang.String contextPath)
        Set the portion of the request URI that indicates the context of the request.
        Parameters:
        contextPath - the portion of the request URI that indicates the context of the request.
        Returns:
        the MockRequest object
      • getServletContext

        public javax.servlet.ServletContext getServletContext()
        Return the servlet context.
        Specified by:
        getServletContext in interface javax.servlet.ServletRequest
        Returns:
        the servlet context.
        Since:
        Servlet 3.0
      • getServletPath

        public java.lang.String getServletPath()
        Return a String containing the name or path of the servlet being called.
        Specified by:
        getServletPath in interface javax.servlet.http.HttpServletRequest
        Returns:
        The servlet path
      • setServletPath

        public MockRequest setServletPath​(java.lang.String servletPath)
        Set the string containing the name or path of the servlet being called.
        Parameters:
        servletPath - a String containing the name or path of the servlet being called
        Returns:
        the MockRequest object
      • getSession

        public javax.servlet.http.HttpSession getSession()
        Returns the current HttpSession associated with this request.
        Specified by:
        getSession in interface javax.servlet.http.HttpServletRequest
        Returns:
        the session associated with this request
      • setSession

        public MockRequest setSession​(javax.servlet.http.HttpSession session)
        Set the current HttpSession associated with this request.
        Parameters:
        session - the HttpSession to associate with this request
        Returns:
        the MockRequest object
      • getSession

        public javax.servlet.http.HttpSession getSession​(boolean create)
        Returns the current HttpSession associated with this request.
        Specified by:
        getSession in interface javax.servlet.http.HttpServletRequest
        Parameters:
        create - if true creates a new session if one does not exist
        Returns:
        the current HttpSession associated with this request.
      • getUserPrincipal

        public java.security.Principal getUserPrincipal()
        Get the user principal. If no user principal was set this method will create a user principal for the getRemoteUser().
        Specified by:
        getUserPrincipal in interface javax.servlet.http.HttpServletRequest
        Returns:
        the user principal
      • setUserPrincipal

        public MockRequest setUserPrincipal​(java.security.Principal userPrincipal)
        Set the user principal.
        Parameters:
        userPrincipal - the user principal
        Returns:
        the MockRequest object
      • initialize

        public final void initialize()
        Reset the request back to a default state.
      • reset

        public void reset()
        Delegate to initialize method.
      • isRequestedSessionIdFromCookie

        public boolean isRequestedSessionIdFromCookie()
        Check whether session id is from a cookie. Always returns true.
        Specified by:
        isRequestedSessionIdFromCookie in interface javax.servlet.http.HttpServletRequest
        Returns:
        Always true
      • isRequestedSessionIdFromUrl

        public boolean isRequestedSessionIdFromUrl()
        Check whether session id is from a url rewrite. Always returns false.
        Specified by:
        isRequestedSessionIdFromUrl in interface javax.servlet.http.HttpServletRequest
        Returns:
        Always false
      • isRequestedSessionIdFromURL

        public boolean isRequestedSessionIdFromURL()
        Check whether session id is from a url rewrite. Always returns false.
        Specified by:
        isRequestedSessionIdFromURL in interface javax.servlet.http.HttpServletRequest
        Returns:
        Always false
      • isRequestedSessionIdValid

        public boolean isRequestedSessionIdValid()
        Check whether the session id is valid.
        Specified by:
        isRequestedSessionIdValid in interface javax.servlet.http.HttpServletRequest
        Returns:
        Always true
      • isSecure

        public boolean isSecure()
        Always returns false.
        Specified by:
        isSecure in interface javax.servlet.ServletRequest
        Returns:
        Always false
      • isUserInRole

        public boolean isUserInRole​(java.lang.String role)

        Returns true if the authenticated user is included in the given role, false otherwise.

        To mock up roles for a user, create a user principal and set the necessary roles. See MockPrincipal for an example.

        Specified by:
        isUserInRole in interface javax.servlet.http.HttpServletRequest
        Parameters:
        role - the role name
        Returns:
        true if the user is included in the specified role, false otherwise
      • removeAttribute

        public void removeAttribute​(java.lang.String name)
        Remove the given attribute.
        Specified by:
        removeAttribute in interface javax.servlet.ServletRequest
        Parameters:
        name - The name of the attribute
      • setAttribute

        public void setAttribute​(java.lang.String name,
                                 java.lang.Object o)
        Set the given attribute.
        Specified by:
        setAttribute in interface javax.servlet.ServletRequest
        Parameters:
        name - The attribute name
        o - The value to set
      • setAuthType

        public void setAuthType​(java.lang.String authType)
        Set the auth type.
        Parameters:
        authType - The auth type
      • setCharacterEncoding

        public void setCharacterEncoding​(java.lang.String encoding)
                                  throws java.io.UnsupportedEncodingException
        Set the character encoding.
        Specified by:
        setCharacterEncoding in interface javax.servlet.ServletRequest
        Parameters:
        encoding - The character encoding
        Throws:
        java.io.UnsupportedEncodingException - If encoding not supported
      • setCookies

        public void setCookies​(javax.servlet.http.Cookie[] theCookies)
        Set the cookies.
        Parameters:
        theCookies - The cookies
      • setMethod

        public MockRequest setMethod​(java.lang.String method)
        Set the method.
        Parameters:
        method - The method
        Returns:
        the MockRequest object
      • setParameter

        public MockRequest setParameter​(java.lang.String name,
                                        java.lang.String value)
        Set a parameter.
        Parameters:
        name - The name
        value - The value
        Returns:
        the MockRequest object
      • setParameter

        public MockRequest setParameter​(java.lang.String name,
                                        java.lang.String[] values)
        Set the specified parameter name to the array of strings.
        Parameters:
        name - name of the parameter
        values - the parameter values
        Returns:
        the MockRequest object
      • removeParameter

        public MockRequest removeParameter​(java.lang.String name)
        Remove the specified parameter.
        Parameters:
        name - the parameter name to remove
        Returns:
        the MockRequest object
      • setParameters

        public MockRequest setParameters​(java.util.Map<java.lang.String,​java.lang.Object> parameters)
        Sets a map of parameters.
        Parameters:
        parameters - the parameters to set
        Returns:
        the MockRequest object
      • setPathInfo

        public MockRequest setPathInfo​(java.lang.String path)
        Set the path that this request is supposed to be serving. The path is relative to the web application root and should start with a / character
        Parameters:
        path - specifies the request path to serve
        Returns:
        the MockRequest object
      • getForward

        public java.lang.String getForward()
        Returns the url that was forwarded to, otherwise return null. javax.servlet.ServletResponse)
        Returns:
        url that was forwarded to
      • getIncludes

        public java.util.List<java.lang.String> getIncludes()
        Returns the list of server side included url's. javax.servlet.ServletResponse)
        Returns:
        list of urls that were included
      • toString

        public java.lang.String toString()
        Returns the String representation of the mock request.
        Overrides:
        toString in class java.lang.Object
        Returns:
        string representation of the mock request
      • getAsyncContext

        public javax.servlet.AsyncContext getAsyncContext()
        Specified by:
        getAsyncContext in interface javax.servlet.ServletRequest
        Returns:
        null
        See Also:
        ServletRequest.getAsyncContext()
      • getContentLengthLong

        public long getContentLengthLong()
        Specified by:
        getContentLengthLong in interface javax.servlet.ServletRequest
        Returns:
        0
        See Also:
        ServletRequest.getContentLengthLong()
      • getDispatcherType

        public javax.servlet.DispatcherType getDispatcherType()
        Specified by:
        getDispatcherType in interface javax.servlet.ServletRequest
        Returns:
        null
        See Also:
        ServletRequest.getDispatcherType()
      • isAsyncStarted

        public boolean isAsyncStarted()
        Specified by:
        isAsyncStarted in interface javax.servlet.ServletRequest
        Returns:
        false
        See Also:
        ServletRequest.isAsyncStarted()
      • isAsyncSupported

        public boolean isAsyncSupported()
        Specified by:
        isAsyncSupported in interface javax.servlet.ServletRequest
        Returns:
        false
        See Also:
        ServletRequest.isAsyncSupported()
      • startAsync

        public javax.servlet.AsyncContext startAsync()
        Specified by:
        startAsync in interface javax.servlet.ServletRequest
        Returns:
        the (re)initialized AsyncContext
        See Also:
        ServletRequest.startAsync()
      • startAsync

        public javax.servlet.AsyncContext startAsync​(javax.servlet.ServletRequest servletRequest,
                                                     javax.servlet.ServletResponse servletResponse)
        Specified by:
        startAsync in interface javax.servlet.ServletRequest
        Parameters:
        servletRequest - the ServletRequest used to initialize the AsyncContext
        servletResponse - the ServletResponse used to initialize the AsyncContext
        Returns:
        null
        See Also:
        ServletRequest.startAsync(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
      • authenticate

        public boolean authenticate​(javax.servlet.http.HttpServletResponse response)
                             throws java.io.IOException
        Specified by:
        authenticate in interface javax.servlet.http.HttpServletRequest
        Parameters:
        response - The HttpServletResponse associated with this HttpServletRequest
        Returns:
        false
        Throws:
        java.io.IOException - never thrown
      • changeSessionId

        public java.lang.String changeSessionId()
        Specified by:
        changeSessionId in interface javax.servlet.http.HttpServletRequest
        Returns:
        null
      • getPart

        public javax.servlet.http.Part getPart​(java.lang.String name)
                                        throws java.io.IOException
        Specified by:
        getPart in interface javax.servlet.http.HttpServletRequest
        Parameters:
        name - the part name
        Returns:
        null
        Throws:
        java.io.IOException - never thrown
      • getParts

        public java.util.Collection<javax.servlet.http.Part> getParts()
                                                               throws java.io.IOException
        Specified by:
        getParts in interface javax.servlet.http.HttpServletRequest
        Returns:
        null
        Throws:
        java.io.IOException - never thrown
        See Also:
        HttpServletRequest.getParts()
      • login

        public void login​(java.lang.String username,
                          java.lang.String password)
        Specified by:
        login in interface javax.servlet.http.HttpServletRequest
        Parameters:
        username - The String value corresponding to the login identifier of the user.
        password - The password String corresponding to the identified user.
        See Also:
        HttpServletRequest.login(java.lang.String, java.lang.String)
      • logout

        public void logout()
        Specified by:
        logout in interface javax.servlet.http.HttpServletRequest
        See Also:
        HttpServletRequest.logout()
      • upgrade

        public javax.servlet.http.HttpUpgradeHandler upgrade​(java.lang.Class handlerClass)
                                                      throws java.io.IOException
        Specified by:
        upgrade in interface javax.servlet.http.HttpServletRequest
        Parameters:
        handlerClass - The HttpUpgradeHandler class used for the upgrade.
        Returns:
        null
        Throws:
        java.io.IOException - never thrown
        See Also:
        HttpServletRequest.upgrade(java.lang.Class)