Class MockHttpServletRequest

java.lang.Object
org.springframework.mock.web.MockHttpServletRequest
All Implemented Interfaces:
jakarta.servlet.http.HttpServletRequest, jakarta.servlet.ServletRequest
Direct Known Subclasses:
MockMultipartHttpServletRequest

public class MockHttpServletRequest extends Object implements jakarta.servlet.http.HttpServletRequest
Mock implementation of the HttpServletRequest interface.

The default, preferred Locale for the server mocked by this request is Locale.ENGLISH. This value can be changed via addPreferredLocale(Locale) or setPreferredLocales(List).

As of Spring 6.0, this set of mocks is designed on a Servlet 6.0 baseline.

Since:
1.0.2
Author:
Juergen Hoeller, Rod Johnson, Rick Evans, Mark Fisher, Chris Beams, Sam Brannen, Brian Clozel
  • Field Details

    • DEFAULT_PROTOCOL

      public static final String DEFAULT_PROTOCOL
      The default protocol: 'HTTP/1.1'.
      Since:
      4.3.7
      See Also:
    • DEFAULT_SCHEME

      public static final String DEFAULT_SCHEME
      The default scheme: 'http'.
      Since:
      4.3.7
      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 jakarta.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 jakarta.servlet.ServletRequest
    • isActive

      public boolean isActive()
      Return whether this request is still active (that is, not completed yet).
    • 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 @Nullable Object getAttribute(String name)
      Specified by:
      getAttribute in interface jakarta.servlet.ServletRequest
    • getAttributeNames

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

      public @Nullable String getCharacterEncoding()
      Specified by:
      getCharacterEncoding in interface jakarta.servlet.ServletRequest
    • setCharacterEncoding

      public void setCharacterEncoding(@Nullable String characterEncoding)
      Specified by:
      setCharacterEncoding in interface jakarta.servlet.ServletRequest
    • setContent

      public void setContent(byte @Nullable [] content)
      Set the content of the request body as a byte array.

      If the supplied byte array represents text such as XML or JSON, the character encoding should typically be set as well.

      See Also:
    • getContentAsByteArray

      public byte @Nullable [] getContentAsByteArray()
      Get the content of the request body as a byte array.
      Returns:
      the content as a byte array (potentially null)
      Since:
      5.0
      See Also:
    • getContentAsString

      Get the content of the request body as a String, using the configured character encoding.
      Returns:
      the content as a String, potentially null
      Throws:
      IllegalStateException - if the character encoding has not been set
      UnsupportedEncodingException - if the character encoding is not supported
      Since:
      5.0
      See Also:
    • getContentLength

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

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

      public void setContentType(@Nullable String contentType)
    • getContentType

      public @Nullable String getContentType()
      Specified by:
      getContentType in interface jakarta.servlet.ServletRequest
    • getInputStream

      public jakarta.servlet.ServletInputStream getInputStream()
      Specified by:
      getInputStream in interface jakarta.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.

    • 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.

    • setParameters

      public void setParameters(Map<String,?> params)
      Set all provided parameters replacing any existing values for the provided parameter names. To add without replacing existing values, use addParameters(java.util.Map).
    • addParameter

      public void addParameter(String name, @Nullable 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.

    • 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.

    • addParameters

      public void addParameters(Map<String,?> params)
      Add all provided parameters without replacing any existing values. To replace existing values, use setParameters(java.util.Map).
    • removeParameter

      public void removeParameter(String name)
      Remove already registered values for the specified HTTP parameter, if any.
    • removeAllParameters

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

      public @Nullable String getParameter(String name)
      Specified by:
      getParameter in interface jakarta.servlet.ServletRequest
    • getParameterNames

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

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

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

      public void setProtocol(String protocol)
    • getProtocol

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

      public void setScheme(String scheme)
    • getScheme

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

      public void setServerName(String serverName)
    • getServerName

      public String getServerName()
      Specified by:
      getServerName in interface jakarta.servlet.ServletRequest
    • setServerPort

      public void setServerPort(int serverPort)
    • getServerPort

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

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

      public void setRemoteAddr(String remoteAddr)
    • getRemoteAddr

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

      public void setRemoteHost(String remoteHost)
    • getRemoteHost

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

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

      public void removeAttribute(String name)
      Specified by:
      removeAttribute in interface jakarta.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.
      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 jakarta.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 jakarta.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 jakarta.servlet.ServletRequest
      See Also:
      • ServletRequest.isSecure()
    • getRequestDispatcher

      public jakarta.servlet.RequestDispatcher getRequestDispatcher(String path)
      Specified by:
      getRequestDispatcher in interface jakarta.servlet.ServletRequest
    • setRemotePort

      public void setRemotePort(int remotePort)
    • getRemotePort

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

      public void setLocalName(String localName)
    • getLocalName

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

      public void setLocalAddr(String localAddr)
    • getLocalAddr

      public String getLocalAddr()
      Specified by:
      getLocalAddr in interface jakarta.servlet.ServletRequest
    • setLocalPort

      public void setLocalPort(int localPort)
    • getLocalPort

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

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

      public jakarta.servlet.AsyncContext startAsync(jakarta.servlet.ServletRequest request, @Nullable jakarta.servlet.ServletResponse response)
      Specified by:
      startAsync in interface jakarta.servlet.ServletRequest
    • setAsyncStarted

      public void setAsyncStarted(boolean asyncStarted)
    • isAsyncStarted

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

      public void setAsyncSupported(boolean asyncSupported)
    • isAsyncSupported

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

      public void setAsyncContext(@Nullable MockAsyncContext asyncContext)
    • getAsyncContext

      public @Nullable jakarta.servlet.AsyncContext getAsyncContext()
      Specified by:
      getAsyncContext in interface jakarta.servlet.ServletRequest
    • setDispatcherType

      public void setDispatcherType(jakarta.servlet.DispatcherType dispatcherType)
    • getDispatcherType

      public jakarta.servlet.DispatcherType getDispatcherType()
      Specified by:
      getDispatcherType in interface jakarta.servlet.ServletRequest
    • getRequestId

      public String getRequestId()
      Specified by:
      getRequestId in interface jakarta.servlet.ServletRequest
    • getProtocolRequestId

      public String getProtocolRequestId()
      Specified by:
      getProtocolRequestId in interface jakarta.servlet.ServletRequest
    • getServletConnection

      public jakarta.servlet.ServletConnection getServletConnection()
      Specified by:
      getServletConnection in interface jakarta.servlet.ServletRequest
    • setAuthType

      public void setAuthType(@Nullable String authType)
    • getAuthType

      public @Nullable String getAuthType()
      Specified by:
      getAuthType in interface jakarta.servlet.http.HttpServletRequest
    • setCookies

      public void setCookies(jakarta.servlet.http.Cookie @Nullable ... cookies)
    • getCookies

      public jakarta.servlet.http.Cookie @Nullable [] getCookies()
      Specified by:
      getCookies in interface jakarta.servlet.http.HttpServletRequest
    • addHeader

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

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

      See Also:
    • removeHeader

      public void removeHeader(String name)
      Remove already registered entries for the specified HTTP header, if any.
      Since:
      4.3.20
    • 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 jakarta.servlet.http.HttpServletRequest
      Parameters:
      name - the header name
      See Also:
    • getHeader

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

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

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

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

      public void setMethod(@Nullable String method)
    • getMethod

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

      public void setPathInfo(@Nullable String pathInfo)
    • getPathInfo

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

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

      public void setContextPath(String contextPath)
    • getContextPath

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

      public void setQueryString(@Nullable String queryString)
    • getQueryString

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

      public void setRemoteUser(@Nullable String remoteUser)
    • getRemoteUser

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

      public void addUserRole(String role)
    • isUserInRole

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

      public void setUserPrincipal(@Nullable Principal userPrincipal)
    • getUserPrincipal

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

      public void setRequestedSessionId(@Nullable String requestedSessionId)
    • getRequestedSessionId

      public @Nullable String getRequestedSessionId()
      Specified by:
      getRequestedSessionId in interface jakarta.servlet.http.HttpServletRequest
    • setUriTemplate

      public void setUriTemplate(@Nullable String uriTemplate)
      Set the original URI template used to prepare the request, if any.
      Parameters:
      uriTemplate - the URI template used to set up the request, if any
      Since:
      6.2
    • getUriTemplate

      public @Nullable String getUriTemplate()
      Return the original URI template used to prepare the request, if any.
      Since:
      6.2
    • setRequestURI

      public void setRequestURI(@Nullable String requestURI)
    • getRequestURI

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

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

      public void setServletPath(String servletPath)
    • getServletPath

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

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

      public @Nullable jakarta.servlet.http.HttpSession getSession(boolean create)
      Specified by:
      getSession in interface jakarta.servlet.http.HttpServletRequest
    • getSession

      public @Nullable jakarta.servlet.http.HttpSession getSession()
      Specified by:
      getSession in interface jakarta.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 jakarta.servlet.http.HttpServletRequest
      Since:
      4.0.3
    • setRequestedSessionIdValid

      public void setRequestedSessionIdValid(boolean requestedSessionIdValid)
    • isRequestedSessionIdValid

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

      public void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie)
    • isRequestedSessionIdFromCookie

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

      public void setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL)
    • isRequestedSessionIdFromURL

      public boolean isRequestedSessionIdFromURL()
      Specified by:
      isRequestedSessionIdFromURL in interface jakarta.servlet.http.HttpServletRequest
    • authenticate

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

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

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

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

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

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

      public void setHttpServletMapping(@Nullable jakarta.servlet.http.HttpServletMapping httpServletMapping)
    • getHttpServletMapping

      public jakarta.servlet.http.HttpServletMapping getHttpServletMapping()
      Specified by:
      getHttpServletMapping in interface jakarta.servlet.http.HttpServletRequest
    • upgrade

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