Class MockHttpServletRequest

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

    public class MockHttpServletRequest
    extends Object
    implements javax.servlet.http.HttpServletRequest
    Mock servlet request. Implements all of the methods from the standard HttpServletRequest class plus helper methods to aid setting up a request.
    Author:
    Chris Turner
    • Constructor Detail

      • MockHttpServletRequest

        public MockHttpServletRequest​(Application application,
                                      javax.servlet.http.HttpSession session,
                                      javax.servlet.ServletContext context,
                                      Locale locale)
        Create the request using the supplied session object. Note that in order for temporary sessions to work, the supplied session must be an instance of MockHttpSession
        Parameters:
        application - The application that this request is for
        session - The session object
        context - The current servlet context
        locale - The current locale
    • Method Detail

      • addCookie

        public void addCookie​(javax.servlet.http.Cookie cookie)
        Add a new cookie.
        Parameters:
        cookie - The cookie
      • addCookies

        public void addCookies​(Iterable<javax.servlet.http.Cookie> cookies)
        Parameters:
        cookies -
      • addFile

        public void addFile​(String fieldName,
                            File file,
                            String contentType)
        Add an uploaded file to the request. Use this to simulate a file that has been uploaded to a field.
        Parameters:
        fieldName - The fieldname of the upload field.
        file - The file to upload.
        contentType - The content type of the file. Must be a correct mimetype.
      • addHeader

        public void addHeader​(String name,
                              String value)
        Add a header to the request.
        Parameters:
        name - The name of the header to add
        value - The value
      • setHeader

        public void setHeader​(String name,
                              String value)
        Sets a header to the request. Overrides any previous value of this header.
        Parameters:
        name - The name of the header to add
        value - The value
        See Also:
        addHeader(String, String)
      • addDateHeader

        public void addDateHeader​(String name,
                                  long date)
        Parameters:
        name -
        date -
      • getAttribute

        public Object getAttribute​(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 Enumeration<StringgetAttributeNames()
        Get the names of all of the values.
        Specified by:
        getAttributeNames in interface javax.servlet.ServletRequest
        Returns:
        The names
      • getAuthType

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

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

        public Charset getCharset()
        Get the current character set.
        Returns:
        The character set
      • setUseMultiPartContentType

        public void setUseMultiPartContentType​(boolean useMultiPartContentType)
        true will force Request generate multiPart ContentType and ContentLength
        Parameters:
        useMultiPartContentType -
      • 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.
      • getContentLengthLong

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

        public String getContentType()
        If useMultiPartContentType set as true return the correct content-type.
        Specified by:
        getContentType in interface javax.servlet.ServletRequest
        Returns:
        The correct multipart content-type if useMultiPartContentType is true. Else null.
      • getContextPath

        public String getContextPath()
        Get the context path. For this mock implementation the name of the application is always returned.
        Specified by:
        getContextPath in interface javax.servlet.http.HttpServletRequest
        Returns:
        The context path
      • getCookie

        public javax.servlet.http.Cookie getCookie​(String name)
        Parameters:
        name -
        Returns:
        Cookie
      • 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​(String name)
                           throws IllegalArgumentException
        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
        Throws:
        IllegalArgumentException - If the header cannot be converted
      • getHeader

        public String getHeader​(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 Enumeration<StringgetHeaderNames()
        Get the names of all of the headers.
        Specified by:
        getHeaderNames in interface javax.servlet.http.HttpServletRequest
        Returns:
        The header names
      • getHeaders

        public Enumeration<StringgetHeaders​(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
      • getInputStream

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

        public int getIntHeader​(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
        Throws:
        NumberFormatException - If the header is not formatted correctly
      • 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 javax.servlet.ServletRequest
        Returns:
        The locale
      • getLocales

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

        public String getMethod()
        Get the method.
        Specified by:
        getMethod in interface javax.servlet.http.HttpServletRequest
        Returns:
        The method
      • getParameter

        public String getParameter​(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 Map<String,​String[]> getParameterMap()
        Get the map of all of the parameters.
        Specified by:
        getParameterMap in interface javax.servlet.ServletRequest
        Returns:
        The parameters
      • getParameterNames

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

        public String[] getParameterValues​(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 String getPathInfo()
        Get the path info.
        Specified by:
        getPathInfo in interface javax.servlet.http.HttpServletRequest
        Returns:
        The path info
      • getPathTranslated

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

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

        public 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 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 javax.servlet.ServletRequest
        Returns:
        The reader
        Throws:
        IOException - If an I/O related problem occurs
      • getRealPath

        @Deprecated
        public String getRealPath​(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 String getRemoteAddr()
        Specified by:
        getRemoteAddr in interface javax.servlet.ServletRequest
        Returns:
        the remote address of the client
      • setRemoteAddr

        public void setRemoteAddr​(String addr)
        Parameters:
        addr - Format: "aaa.bbb.ccc.ddd"
      • getRemoteHost

        public String getRemoteHost()
        Get the remote host.
        Specified by:
        getRemoteHost in interface javax.servlet.ServletRequest
        Returns:
        Return 'localhost' by default
      • getRemoteUser

        public String getRemoteUser()
        Get the name of the remote user from the REMOTE_USER header.
        Specified by:
        getRemoteUser in interface javax.servlet.http.HttpServletRequest
        Returns:
        The name of the remote user
      • getRequestDispatcher

        public javax.servlet.RequestDispatcher getRequestDispatcher​(String name)
        Return a dummy dispatcher that just records that dispatch has occurred without actually doing anything.
        Specified by:
        getRequestDispatcher in interface javax.servlet.ServletRequest
        Parameters:
        name - The name to dispatch to
        Returns:
        The dispatcher
      • getRequestedSessionId

        public 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 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()
      • getRequestURL

        public StringBuffer getRequestURL()
        Try to build a rough URL.
        Specified by:
        getRequestURL in interface javax.servlet.http.HttpServletRequest
        Returns:
        The url
        See Also:
        HttpServletRequest.getRequestURL()
      • getScheme

        public String getScheme()
        Get the scheme.
        Specified by:
        getScheme in interface javax.servlet.ServletRequest
        Returns:
        the scheme of this request
      • setScheme

        public void setScheme​(String scheme)
        Sets the scheme of this request

        set the secure flag accordingly (true for 'https', false otherwise)

        Parameters:
        scheme - protocol scheme (e.g. https, http, ftp)
        See Also:
        isSecure()
      • getServerName

        public String getServerName()
        Get the server name.
        Specified by:
        getServerName in interface javax.servlet.ServletRequest
        Returns:
        by default returns 'localhost'
      • setServerName

        public void setServerName​(String serverName)
        Set the server name.
        Parameters:
        serverName - content of 'Host' header
      • getServerPort

        public int getServerPort()
        Specified by:
        getServerPort in interface javax.servlet.ServletRequest
        Returns:
        the server port of this request
      • setServerPort

        public void setServerPort​(int port)
        Sets the server port for this request
        Parameters:
        port -
      • getServletPath

        public String getServletPath()
        The servlet path may either be the application name or /. For test purposes we always return the servlet name.
        Specified by:
        getServletPath in interface javax.servlet.http.HttpServletRequest
        Returns:
        The servlet path
      • getSession

        public javax.servlet.http.HttpSession getSession()
        Get the sessions.
        Specified by:
        getSession in interface javax.servlet.http.HttpServletRequest
        Returns:
        The session
      • changeSessionId

        public String changeSessionId()
        Specified by:
        changeSessionId in interface javax.servlet.http.HttpServletRequest
      • getSession

        public javax.servlet.http.HttpSession getSession​(boolean createNew)
        Get the session.
        Specified by:
        getSession in interface javax.servlet.http.HttpServletRequest
        Parameters:
        createNew - Ignored, there is always a session
        Returns:
        The session
      • getUserPrincipal

        public Principal getUserPrincipal()
        Get the user principal.
        Specified by:
        getUserPrincipal in interface javax.servlet.http.HttpServletRequest
        Returns:
        A user principal
      • initialize

        public void initialize​(Locale locale)
        Reset the request back to a default state.
        Parameters:
        locale -
      • 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
      • authenticate

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

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

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

        public Collection<javax.servlet.http.Part> getParts()
                                                     throws IOException,
                                                            javax.servlet.ServletException
        Specified by:
        getParts in interface javax.servlet.http.HttpServletRequest
        Throws:
        IOException
        javax.servlet.ServletException
      • getPart

        public javax.servlet.http.Part getPart​(String name)
                                        throws IOException,
                                               javax.servlet.ServletException
        Specified by:
        getPart in interface javax.servlet.http.HttpServletRequest
        Throws:
        IOException
        javax.servlet.ServletException
      • upgrade

        public <T extends javax.servlet.http.HttpUpgradeHandler> T upgrade​(Class<T> aClass)
                                                                    throws IOException,
                                                                           javax.servlet.ServletException
        Specified by:
        upgrade in interface javax.servlet.http.HttpServletRequest
        Throws:
        IOException
        javax.servlet.ServletException
      • 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()
        Specified by:
        isSecure in interface javax.servlet.ServletRequest
        Returns:
        true if this request's scheme is 'https', false - otherwise
      • setSecure

        public void setSecure​(boolean secure)
        Parameters:
        secure -
      • isUserInRole

        public boolean isUserInRole​(String name)
        NOT IMPLEMENTED.
        Specified by:
        isUserInRole in interface javax.servlet.http.HttpServletRequest
        Parameters:
        name - The role name
        Returns:
        Always false
      • removeAttribute

        public void removeAttribute​(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​(String name,
                                 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​(String authType)
        Set the auth type.
        Parameters:
        authType - The auth type
      • setCookies

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

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

        public void setParameter​(String name,
                                 String value)
        Set a parameter.
        Parameters:
        name - The name
        value - The value
      • setParameters

        public void setParameters​(Map<String,​String[]> parameters)
        Sets a map of parameters.
        Parameters:
        parameters - the parameters to set
      • setPath

        public void setPath​(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 -
      • setURL

        public void setURL​(String url)
        Set the complete url for this request. The url will be analyzed.
        Parameters:
        url -
      • getLocalAddr

        public String getLocalAddr()
        Specified by:
        getLocalAddr in interface javax.servlet.ServletRequest
        Returns:
        local address
      • getLocalName

        public String getLocalName()
        Specified by:
        getLocalName in interface javax.servlet.ServletRequest
        Returns:
        local host name
      • getLocalPort

        public int getLocalPort()
        Specified by:
        getLocalPort in interface javax.servlet.ServletRequest
        Returns:
        local port
      • getRemotePort

        public int getRemotePort()
        Specified by:
        getRemotePort in interface javax.servlet.ServletRequest
        Returns:
        remote port
      • setUrl

        public void setUrl​(Url url)
        Parameters:
        url -
      • getUrl

        public Url getUrl()
        Returns:
        request url
      • getServletContext

        public javax.servlet.ServletContext getServletContext()
        Specified by:
        getServletContext in interface javax.servlet.ServletRequest
        Returns:
        ServletContext
      • startAsync

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

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

        public boolean isAsyncSupported()
        Specified by:
        isAsyncSupported in interface javax.servlet.ServletRequest
      • getAsyncContext

        public javax.servlet.AsyncContext getAsyncContext()
        Specified by:
        getAsyncContext in interface javax.servlet.ServletRequest
      • getDispatcherType

        public javax.servlet.DispatcherType getDispatcherType()
        Specified by:
        getDispatcherType in interface javax.servlet.ServletRequest