Class MockHttpServletRequest

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

    public class MockHttpServletRequest
    extends java.lang.Object
    implements javax.servlet.http.HttpServletRequest
    Mock implementation of the HttpServletRequest interface.
    • Field Detail

      • DEFAULT_PROTOCOL

        public static final java.lang.String DEFAULT_PROTOCOL
        The default protocol: 'http'.
        See Also:
        Constant Field Values
      • DEFAULT_SERVER_ADDR

        public static final java.lang.String DEFAULT_SERVER_ADDR
        The default server address: '127.0.0.1'.
        See Also:
        Constant Field Values
      • DEFAULT_SERVER_NAME

        public static final java.lang.String DEFAULT_SERVER_NAME
        The default server name: 'localhost'.
        See Also:
        Constant Field Values
      • DEFAULT_SERVER_PORT

        public static final int DEFAULT_SERVER_PORT
        The default server port: '80'.
        See Also:
        Constant Field Values
      • DEFAULT_REMOTE_ADDR

        public static final java.lang.String DEFAULT_REMOTE_ADDR
        The default remote address: '127.0.0.1'.
        See Also:
        Constant Field Values
      • DEFAULT_REMOTE_HOST

        public static final java.lang.String DEFAULT_REMOTE_HOST
        The default remote host: 'localhost'.
        See Also:
        Constant Field Values
    • Method Detail

      • getServletContext

        public javax.servlet.ServletContext getServletContext()
        Return the ServletContext that this request is associated with. (Not available in the standard HttpServletRequest interface for some reason.)
        Specified by:
        getServletContext in interface javax.servlet.ServletRequest
        Returns:
        ServletContext
      • isActive

        public boolean isActive()
        Return whether this request is still active (that is, not completed yet).
        Returns:
        boolean
      • close

        public void close()
        Mark this request as completed, keeping its state.
      • invalidate

        public void invalidate()
        Invalidate this request, clearing its state.
      • checkActive

        protected void checkActive()
                            throws java.lang.IllegalStateException
        Check whether this request is still active (that is, not completed yet), throwing an IllegalStateException if not active anymore.
        Throws:
        java.lang.IllegalStateException
      • getAttribute

        public java.lang.Object getAttribute​(java.lang.String name)
        Specified by:
        getAttribute in interface javax.servlet.ServletRequest
      • getAttributeNames

        public java.util.Enumeration<java.lang.String> getAttributeNames()
        Specified by:
        getAttributeNames in interface javax.servlet.ServletRequest
      • getCharacterEncoding

        public java.lang.String getCharacterEncoding()
        Specified by:
        getCharacterEncoding in interface javax.servlet.ServletRequest
      • setCharacterEncoding

        public void setCharacterEncoding​(java.lang.String characterEncoding)
        Specified by:
        setCharacterEncoding in interface javax.servlet.ServletRequest
      • setContent

        public void setContent​(byte[] content)
      • getContentLength

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

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

        public void setContentType​(java.lang.String contentType)
      • getContentType

        public java.lang.String getContentType()
        Specified by:
        getContentType in interface javax.servlet.ServletRequest
      • getInputStream

        public javax.servlet.ServletInputStream getInputStream()
        Specified by:
        getInputStream in interface javax.servlet.ServletRequest
      • setParameter

        public void setParameter​(java.lang.String name,
                                 java.lang.String value)
        Set a single value for the specified HTTP parameter.

        If there are already one or more values registered for the given parameter name, they will be replaced.

        Parameters:
        name - name
        value - value
      • setParameter

        public void setParameter​(java.lang.String name,
                                 java.lang.String[] values)
        Set an array of values for the specified HTTP parameter.

        If there are already one or more values registered for the given parameter name, they will be replaced.

        Parameters:
        name - name
        values - values
      • setParameters

        public void setParameters​(java.util.Map params)
        Sets all provided parameters replacing any existing values for the provided parameter names. To add without replacing existing values, use addParameters(java.util.Map).
        Parameters:
        params - map of params
      • addParameter

        public void addParameter​(java.lang.String name,
                                 java.lang.String value)
        Add a single value for the specified HTTP parameter.

        If there are already one or more values registered for the given parameter name, the given value will be added to the end of the list.

        Parameters:
        name - name
        value - value
      • addParameter

        public void addParameter​(java.lang.String name,
                                 java.lang.String[] values)
        Add an array of values for the specified HTTP parameter.

        If there are already one or more values registered for the given parameter name, the given values will be added to the end of the list.

        Parameters:
        name - name
        values - value
      • addParameters

        public void addParameters​(java.util.Map<?,​?> params)
        Adds all provided parameters without replacing any existing values. To replace existing values, use setParameters(java.util.Map).
        Parameters:
        params - map of params
      • removeParameter

        public void removeParameter​(java.lang.String name)
        Remove already registered values for the specified HTTP parameter, if any.
        Parameters:
        name - name
      • removeAllParameters

        public void removeAllParameters()
        Removes all existing parameters.
      • getParameter

        public java.lang.String getParameter​(java.lang.String name)
        Specified by:
        getParameter in interface javax.servlet.ServletRequest
      • getParameterNames

        public java.util.Enumeration<java.lang.String> getParameterNames()
        Specified by:
        getParameterNames in interface javax.servlet.ServletRequest
      • getParameterValues

        public java.lang.String[] getParameterValues​(java.lang.String name)
        Specified by:
        getParameterValues in interface javax.servlet.ServletRequest
      • getParameterMap

        public java.util.Map<java.lang.String,​java.lang.String[]> getParameterMap()
        Specified by:
        getParameterMap in interface javax.servlet.ServletRequest
      • setProtocol

        public void setProtocol​(java.lang.String protocol)
      • getProtocol

        public java.lang.String getProtocol()
        Specified by:
        getProtocol in interface javax.servlet.ServletRequest
      • setScheme

        public void setScheme​(java.lang.String scheme)
      • getScheme

        public java.lang.String getScheme()
        Specified by:
        getScheme in interface javax.servlet.ServletRequest
      • setServerName

        public void setServerName​(java.lang.String serverName)
      • getServerName

        public java.lang.String getServerName()
        Specified by:
        getServerName in interface javax.servlet.ServletRequest
      • setServerPort

        public void setServerPort​(int serverPort)
      • getServerPort

        public int getServerPort()
        Specified by:
        getServerPort in interface javax.servlet.ServletRequest
      • getReader

        public java.io.BufferedReader getReader()
                                         throws java.io.UnsupportedEncodingException
        Specified by:
        getReader in interface javax.servlet.ServletRequest
        Throws:
        java.io.UnsupportedEncodingException
      • setRemoteAddr

        public void setRemoteAddr​(java.lang.String remoteAddr)
      • getRemoteAddr

        public java.lang.String getRemoteAddr()
        Specified by:
        getRemoteAddr in interface javax.servlet.ServletRequest
      • setRemoteHost

        public void setRemoteHost​(java.lang.String remoteHost)
      • getRemoteHost

        public java.lang.String getRemoteHost()
        Specified by:
        getRemoteHost in interface javax.servlet.ServletRequest
      • setAttribute

        public void setAttribute​(java.lang.String name,
                                 java.lang.Object value)
        Specified by:
        setAttribute in interface javax.servlet.ServletRequest
      • removeAttribute

        public void removeAttribute​(java.lang.String name)
        Specified by:
        removeAttribute in interface javax.servlet.ServletRequest
      • clearAttributes

        public void clearAttributes()
        Clear all of this request's attributes.
      • setPreferredLocales

        public void setPreferredLocales​(java.util.List<java.util.Locale> locales)
        Set the list of preferred locales, in descending order, effectively replacing any existing locales.
        Since:
        3.2
        See Also:
        addPreferredLocale(java.util.Locale)
      • getLocale

        public java.util.Locale getLocale()
        Return the first preferred locale configured in this mock request.

        If no locales have been explicitly configured, the default, preferred Locale for the server mocked by this request is Locale.ENGLISH.

        In contrast to the Servlet specification, this mock implementation does not take into consideration any locales specified via the Accept-Language header.

        Specified by:
        getLocale in interface javax.servlet.ServletRequest
        See Also:
        ServletRequest.getLocale(), addPreferredLocale(Locale), setPreferredLocales(List)
      • getLocales

        public java.util.Enumeration<java.util.Locale> getLocales()
        Return an enumeration of the preferred locales configured in this mock request.

        If no locales have been explicitly configured, the default, preferred Locale for the server mocked by this request is Locale.ENGLISH.

        In contrast to the Servlet specification, this mock implementation does not take into consideration any locales specified via the Accept-Language header.

        Specified by:
        getLocales in interface javax.servlet.ServletRequest
        See Also:
        ServletRequest.getLocales(), addPreferredLocale(Locale), setPreferredLocales(List)
      • setSecure

        public void setSecure​(boolean secure)
        Set the boolean secure flag indicating whether the mock request was made using a secure channel, such as HTTPS.
        See Also:
        isSecure(), getScheme(), setScheme(String)
      • isSecure

        public boolean isSecure()
        Return true if the secure flag has been set to true or if the scheme is https.
        Specified by:
        isSecure in interface javax.servlet.ServletRequest
        See Also:
        ServletRequest.isSecure()
      • getRequestDispatcher

        public javax.servlet.RequestDispatcher getRequestDispatcher​(java.lang.String path)
        Specified by:
        getRequestDispatcher in interface javax.servlet.ServletRequest
      • getRealPath

        @Deprecated
        public java.lang.String getRealPath​(java.lang.String path)
        Deprecated.
        Specified by:
        getRealPath in interface javax.servlet.ServletRequest
      • setRemotePort

        public void setRemotePort​(int remotePort)
      • getRemotePort

        public int getRemotePort()
        Specified by:
        getRemotePort in interface javax.servlet.ServletRequest
      • setLocalName

        public void setLocalName​(java.lang.String localName)
      • getLocalName

        public java.lang.String getLocalName()
        Specified by:
        getLocalName in interface javax.servlet.ServletRequest
      • setLocalAddr

        public void setLocalAddr​(java.lang.String localAddr)
      • getLocalAddr

        public java.lang.String getLocalAddr()
        Specified by:
        getLocalAddr in interface javax.servlet.ServletRequest
      • setLocalPort

        public void setLocalPort​(int localPort)
      • getLocalPort

        public int getLocalPort()
        Specified by:
        getLocalPort in interface javax.servlet.ServletRequest
      • startAsync

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

        public javax.servlet.AsyncContext startAsync​(javax.servlet.ServletRequest request,
                                                     javax.servlet.ServletResponse response)
        Specified by:
        startAsync in interface javax.servlet.ServletRequest
      • setAsyncStarted

        public void setAsyncStarted​(boolean asyncStarted)
      • isAsyncStarted

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

        public void setAsyncSupported​(boolean asyncSupported)
      • isAsyncSupported

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

        public void setAsyncContext​(MockAsyncContext asyncContext)
      • getAsyncContext

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

        public void setDispatcherType​(javax.servlet.DispatcherType dispatcherType)
      • getDispatcherType

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

        public void setAuthType​(java.lang.String authType)
      • getAuthType

        public java.lang.String getAuthType()
        Specified by:
        getAuthType in interface javax.servlet.http.HttpServletRequest
      • setCookies

        public void setCookies​(javax.servlet.http.Cookie... cookies)
      • getCookies

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

        public long getDateHeader​(java.lang.String name)
        Return the long timestamp for the date header with the given name.

        If the internal value representation is a String, this method will try to parse it as a date using the supported date formats:

        • "EEE, dd MMM yyyy HH:mm:ss zzz"
        • "EEE, dd-MMM-yy HH:mm:ss zzz"
        • "EEE MMM dd HH:mm:ss yyyy"
        Specified by:
        getDateHeader in interface javax.servlet.http.HttpServletRequest
        Parameters:
        name - the header name
        See Also:
        Section 7.1.1.1 of RFC 7231
      • getHeader

        public java.lang.String getHeader​(java.lang.String name)
        Specified by:
        getHeader in interface javax.servlet.http.HttpServletRequest
      • getHeaders

        public java.util.Enumeration<java.lang.String> getHeaders​(java.lang.String name)
        Specified by:
        getHeaders in interface javax.servlet.http.HttpServletRequest
      • getHeaderNames

        public java.util.Enumeration<java.lang.String> getHeaderNames()
        Specified by:
        getHeaderNames in interface javax.servlet.http.HttpServletRequest
      • getIntHeader

        public int getIntHeader​(java.lang.String name)
        Specified by:
        getIntHeader in interface javax.servlet.http.HttpServletRequest
      • setMethod

        public void setMethod​(java.lang.String method)
      • getMethod

        public java.lang.String getMethod()
        Specified by:
        getMethod in interface javax.servlet.http.HttpServletRequest
      • setPathInfo

        public void setPathInfo​(java.lang.String pathInfo)
      • getPathInfo

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

        public java.lang.String getPathTranslated()
        Specified by:
        getPathTranslated in interface javax.servlet.http.HttpServletRequest
      • setContextPath

        public void setContextPath​(java.lang.String contextPath)
      • getContextPath

        public java.lang.String getContextPath()
        Specified by:
        getContextPath in interface javax.servlet.http.HttpServletRequest
      • setQueryString

        public void setQueryString​(java.lang.String queryString)
      • getQueryString

        public java.lang.String getQueryString()
        Specified by:
        getQueryString in interface javax.servlet.http.HttpServletRequest
      • setRemoteUser

        public void setRemoteUser​(java.lang.String remoteUser)
      • getRemoteUser

        public java.lang.String getRemoteUser()
        Specified by:
        getRemoteUser in interface javax.servlet.http.HttpServletRequest
      • addUserRole

        public void addUserRole​(java.lang.String role)
      • isUserInRole

        public boolean isUserInRole​(java.lang.String role)
        Specified by:
        isUserInRole in interface javax.servlet.http.HttpServletRequest
      • setUserPrincipal

        public void setUserPrincipal​(java.security.Principal userPrincipal)
      • getUserPrincipal

        public java.security.Principal getUserPrincipal()
        Specified by:
        getUserPrincipal in interface javax.servlet.http.HttpServletRequest
      • setRequestedSessionId

        public void setRequestedSessionId​(java.lang.String requestedSessionId)
      • getRequestedSessionId

        public java.lang.String getRequestedSessionId()
        Specified by:
        getRequestedSessionId in interface javax.servlet.http.HttpServletRequest
      • setRequestURI

        public void setRequestURI​(java.lang.String requestURI)
      • getRequestURI

        public java.lang.String getRequestURI()
        Specified by:
        getRequestURI in interface javax.servlet.http.HttpServletRequest
      • getRequestURL

        public java.lang.StringBuffer getRequestURL()
        Specified by:
        getRequestURL in interface javax.servlet.http.HttpServletRequest
      • setServletPath

        public void setServletPath​(java.lang.String servletPath)
      • getServletPath

        public java.lang.String getServletPath()
        Specified by:
        getServletPath in interface javax.servlet.http.HttpServletRequest
      • setSession

        public void setSession​(javax.servlet.http.HttpSession session)
      • getSession

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

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

        public java.lang.String changeSessionId()
        The implementation of this (Servlet 3.1+) method calls MockHttpSession.changeSessionId() if the session is a mock session. Otherwise it simply returns the current session id.
        Specified by:
        changeSessionId in interface javax.servlet.http.HttpServletRequest
        Since:
        4.0.3
      • setRequestedSessionIdValid

        public void setRequestedSessionIdValid​(boolean requestedSessionIdValid)
      • isRequestedSessionIdValid

        public boolean isRequestedSessionIdValid()
        Specified by:
        isRequestedSessionIdValid in interface javax.servlet.http.HttpServletRequest
      • setRequestedSessionIdFromCookie

        public void setRequestedSessionIdFromCookie​(boolean requestedSessionIdFromCookie)
      • isRequestedSessionIdFromCookie

        public boolean isRequestedSessionIdFromCookie()
        Specified by:
        isRequestedSessionIdFromCookie in interface javax.servlet.http.HttpServletRequest
      • setRequestedSessionIdFromURL

        public void setRequestedSessionIdFromURL​(boolean requestedSessionIdFromURL)
      • isRequestedSessionIdFromURL

        public boolean isRequestedSessionIdFromURL()
        Specified by:
        isRequestedSessionIdFromURL in interface javax.servlet.http.HttpServletRequest
      • isRequestedSessionIdFromUrl

        @Deprecated
        public boolean isRequestedSessionIdFromUrl()
        Deprecated.
        Specified by:
        isRequestedSessionIdFromUrl in interface javax.servlet.http.HttpServletRequest
      • authenticate

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

        public void login​(java.lang.String username,
                          java.lang.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
      • addPart

        public void addPart​(javax.servlet.http.Part part)
      • getPart

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

        public java.util.Collection<javax.servlet.http.Part> getParts()
                                                               throws java.io.IOException,
                                                                      java.lang.IllegalStateException,
                                                                      javax.servlet.ServletException
        Specified by:
        getParts in interface javax.servlet.http.HttpServletRequest
        Throws:
        java.io.IOException
        java.lang.IllegalStateException
        javax.servlet.ServletException
      • upgrade

        public <T extends javax.servlet.http.HttpUpgradeHandler> T upgrade​(java.lang.Class<T> arg0)
                                                                    throws java.io.IOException,
                                                                           javax.servlet.ServletException
        Specified by:
        upgrade in interface javax.servlet.http.HttpServletRequest
        Throws:
        java.io.IOException
        javax.servlet.ServletException