Class MockRequest

    • Constructor Detail

      • MockRequest

        public MockRequest()
        Create new MockRequest.
      • MockRequest

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

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

        public MockRequest​(Locale locale,
                           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​(Locale locale,
                           ServletContext servletContext,
                           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​(Locale locale,
                           String contextPath,
                           String servletPath,
                           ServletContext servletContext,
                           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​(ServletContext servletContext)
        Set the request's servletContext instance.
        Parameters:
        servletContext - the new ServletContext instance
        Returns:
        the MockRequest object
      • setHttpSession

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

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

        public MockRequest addHeader​(String name,
                                     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​(String name,
                                     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​(String name,
                                     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 Object getAttribute​(String name)
        Get an attribute.
        Specified by:
        getAttribute in interface ServletRequest
        Parameters:
        name - The attribute name
        Returns:
        The value, or null
      • 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 ServletRequest
        Returns:
        -1 if useMultiPartContentType is false. Else the length of the generated request.
      • setContentType

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

        public 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 ServletRequest
        Returns:
        The correct multipart content-type if useMultiPartContentType is true. Else null.
      • getDateHeader

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

        public String getHeader​(String name)
        Get the given header value.
        Specified by:
        getHeader in interface HttpServletRequest
        Parameters:
        name - The header name
        Returns:
        The header value or null
      • getHeaders

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

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

        public 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 ServletRequest
        Returns:
        The locale
      • getLocales

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

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

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

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

        public BufferedReader getReader()
                                 throws 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 ServletRequest
        Returns:
        The reader
        Throws:
        IOException - If an I/O related problem occurs
      • getRealPath

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

        public String getRemoteAddr()
        Get the remote address of the client.
        Specified by:
        getRemoteAddr in interface ServletRequest
        Returns:
        Always 127.0.0.1
      • getLocalAddr

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

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

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

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

        public RequestDispatcher getRequestDispatcher​(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 ServletRequest
        Parameters:
        path - a String specifying the pathname to the resource
        Returns:
        a dispatcher for the specified path
      • getRequestedSessionId

        public String getRequestedSessionId()
        Get the requested session id. Always returns the id of the current session.
        Specified by:
        getRequestedSessionId in interface HttpServletRequest
        Returns:
        The session id
      • setRequestURL

        public void setRequestURL​(String requestURL)
        Provide a convenience method for setting the request URL.
        Parameters:
        requestURL - the request URL to set
      • 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 String getScheme()
        Get the scheme http, https, or ftp.
        Specified by:
        getScheme in interface ServletRequest
        Returns:
        the scheme used by this request
      • setScheme

        public MockRequest setScheme​(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 String getServerName()
        Get the host server name to which the request was sent.
        Specified by:
        getServerName in interface ServletRequest
        Returns:
        always the host server name
      • setServerName

        public MockRequest setServerName​(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 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 String getContextPath()
        Returns the portion of the request URI that indicates the context of the request.
        Specified by:
        getContextPath in interface HttpServletRequest
        Returns:
        the portion of the request URI that indicates the context of the request.
      • setContextPath

        public MockRequest setContextPath​(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
      • getServletPath

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

        public MockRequest setServletPath​(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 HttpSession getSession()
        Returns the current HttpSession associated with this request.
        Specified by:
        getSession in interface HttpServletRequest
        Returns:
        the session associated with this request
      • setSession

        public MockRequest setSession​(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 HttpSession getSession​(boolean create)
        Returns the current HttpSession associated with this request.
        Specified by:
        getSession in interface HttpServletRequest
        Parameters:
        create - if true creates a new session if one does not exist
        Returns:
        the current HttpSession associated with this request.
      • setUserPrincipal

        public MockRequest setUserPrincipal​(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 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 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 HttpServletRequest
        Returns:
        Always false
      • isSecure

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

        public boolean isUserInRole​(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 HttpServletRequest
        Parameters:
        role - the role name
        Returns:
        true if the user is included in the specified role, false otherwise
      • removeAttribute

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

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

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

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

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

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

        public MockRequest setParameter​(String name,
                                        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​(String name)
        Remove the specified parameter.
        Parameters:
        name - the parameter name to remove
        Returns:
        the MockRequest object
      • setParameters

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

        public MockRequest setPathInfo​(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 String getForward()
        Returns the url that was forwarded to, otherwise return null. javax.servlet.ServletResponse)
        Returns:
        url that was forwarded to
      • getIncludes

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

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