Class MockHttpServletRequest

java.lang.Object
com.jalios.servlet.http.MockHttpServletRequest
All Implemented Interfaces:
javax.servlet.http.HttpServletRequest, javax.servlet.ServletRequest

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

    • DEFAULT_PROTOCOL

      public static final String DEFAULT_PROTOCOL
      The default protocol: 'http'.
      See Also:
    • DEFAULT_SERVER_ADDR

      public static final String DEFAULT_SERVER_ADDR
      The default server address: '127.0.0.1'.
      See Also:
    • DEFAULT_SERVER_NAME

      public static final String DEFAULT_SERVER_NAME
      The default server name: 'localhost'.
      See Also:
    • DEFAULT_SERVER_PORT

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

      public static final String DEFAULT_REMOTE_ADDR
      The default remote address: '127.0.0.1'.
      See Also:
    • DEFAULT_REMOTE_HOST

      public static final String DEFAULT_REMOTE_HOST
      The default remote host: 'localhost'.
      See Also:
  • Constructor Details

  • Method Details

    • 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 IllegalStateException
      Check whether this request is still active (that is, not completed yet), throwing an IllegalStateException if not active anymore.
      Throws:
      IllegalStateException
    • getAttribute

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

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

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

      public void setCharacterEncoding(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(String contentType)
    • getContentType

      public 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(String name, 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(String name, 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(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(String name, 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(String name, 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(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(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 String getParameter(String name)
      Specified by:
      getParameter in interface javax.servlet.ServletRequest
    • getParameterNames

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

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

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

      public void setProtocol(String protocol)
    • getProtocol

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

      public void setScheme(String scheme)
    • getScheme

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

      public void setServerName(String serverName)
    • getServerName

      public 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 BufferedReader getReader() throws UnsupportedEncodingException
      Specified by:
      getReader in interface javax.servlet.ServletRequest
      Throws:
      UnsupportedEncodingException
    • setRemoteAddr

      public void setRemoteAddr(String remoteAddr)
    • getRemoteAddr

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

      public void setRemoteHost(String remoteHost)
    • getRemoteHost

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

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

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

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

      public void addPreferredLocale(Locale locale)
      Add a new preferred locale, before any existing locales.
      Parameters:
      locale - locale
      See Also:
    • setPreferredLocales

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

      public 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:
    • getLocales

      public Enumeration<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:
    • 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

      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(String path)
      Specified by:
      getRequestDispatcher in interface javax.servlet.ServletRequest
    • getRealPath

      @Deprecated public String getRealPath(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(String localName)
    • getLocalName

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

      public void setLocalAddr(String localAddr)
    • getLocalAddr

      public 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(String authType)
    • getAuthType

      public 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
    • addHeader

      public void addHeader(String name, Object value)
      Add a header entry for the given name.

      While this method can take any Object as a parameter, it is recommended to use the following types:

      Parameters:
      name - name
      value - value
      See Also:
    • getDateHeader

      public long getDateHeader(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:
    • getHeader

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

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

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

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

      public void setMethod(String method)
    • getMethod

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

      public void setPathInfo(String pathInfo)
    • getPathInfo

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

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

      public void setContextPath(String contextPath)
    • getContextPath

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

      public void setQueryString(String queryString)
    • getQueryString

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

      public void setRemoteUser(String remoteUser)
    • getRemoteUser

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

      public void addUserRole(String role)
    • isUserInRole

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

      public void setUserPrincipal(Principal userPrincipal)
    • getUserPrincipal

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

      public void setRequestedSessionId(String requestedSessionId)
    • getRequestedSessionId

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

      public void setRequestURI(String requestURI)
    • getRequestURI

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

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

      public void setServletPath(String servletPath)
    • getServletPath

      public 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 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 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
    • addPart

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

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

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

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