extends Object implements 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(java.util.Locale) or setPreferredLocales(java.util.List<java.util.Locale>) .

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
Create a new MockHttpServletRequest with the supplied ServletContext .
Create a new MockHttpServletRequest with the supplied ServletContext , method , and requestURI .
Create a new MockHttpServletRequest with a default MockServletContext .
Add a new preferred locale, before any existing locales.
boolean
The implementation of this (Servlet 3.1+) method calls MockHttpSession.changeSessionId() if the session is a mock session.
protected void
Check whether this request is still active (that is, not completed yet), throwing an IllegalStateException if not active anymore.
Clear all of this request's attributes.
Mark this request as completed, keeping its state.
byte[]
Get the content of the request body as a byte array.
Get the content of the request body as a String , using the configured character encoding .
Return the long timestamp for the date header with the given name .
getHeaders ( String name)
Return the first preferred locale configured in this mock request.
Return an enumeration of the preferred locales configured in this mock request.
String
Return the ServletContext that this request is associated with.
getSession (boolean create)
Invalidate this request, clearing its state.
boolean
Return whether this request is still active (that is, not completed yet).
boolean
boolean
boolean
boolean
boolean
boolean
Return true if the secure flag has been set to true or if the scheme is https .
boolean
login ( String username, String password)
Remove all existing parameters.
Remove already registered entries for the specified HTTP header, if any.
Remove already registered values for the specified HTTP parameter, if any.
setAsyncStarted (boolean asyncStarted)
setAsyncSupported (boolean asyncSupported)
setCharacterEncoding ( String characterEncoding)
setContent (byte[] content)
Set the content of the request body as a byte array.
setCookies ( Cookie ... cookies)
setLocalPort (int localPort)
setParameter ( String name, String value)
Set a single value for the specified HTTP parameter.
Set an array of values for the specified HTTP parameter.
Set all provided parameters replacing any existing values for the provided parameter names.
Set the list of preferred locales, in descending order, effectively replacing any existing locales.
setRemotePort (int remotePort)
setRequestedSessionId ( String requestedSessionId)
setRequestedSessionIdFromCookie (boolean requestedSessionIdFromCookie)
setRequestedSessionIdFromURL (boolean requestedSessionIdFromURL)
setRequestedSessionIdValid (boolean requestedSessionIdValid)
setSecure (boolean secure)
Set the boolean secure flag indicating whether the mock request was made using a secure channel, such as HTTPS.
setServerPort (int serverPort)
<T extends HttpUpgradeHandler >
T
upgrade ( Class <T> handlerClass)

Methods inherited from class java.lang. Object

clone , equals , finalize , getClass , hashCode , notify , notifyAll , toString , wait , wait , wait

Methods inherited from interface jakarta.servlet.http. HttpServletRequest

getTrailerFields , isTrailerFieldsReady , newPushBuilder

DEFAULT_PROTOCOL

public static final String DEFAULT_PROTOCOL
The default protocol: 'HTTP/1.1'.
Since:
4.3.7
See Also:
  • Constant Field Values
  • DEFAULT_SERVER_ADDR

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

    public static final 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 String DEFAULT_REMOTE_ADDR
    The default remote address: '127.0.0.1'.
    See Also:
  • Constant Field Values
  • DEFAULT_REMOTE_HOST

    public static final String DEFAULT_REMOTE_HOST
    The default remote host: 'localhost'.
    See Also:
  • Constant Field Values
  • MockHttpServletRequest

    public MockHttpServletRequest ()
    Create a new MockHttpServletRequest with a default MockServletContext .
    See Also:
  • MockHttpServletRequest(ServletContext, String, String)
  • String requestURI)
    Create a new MockHttpServletRequest with a default MockServletContext .
    Parameters:
    method - the request method (may be null )
    requestURI - the request URI (may be null )
    See Also:
  • setMethod(java.lang.String)
  • setRequestURI(java.lang.String)
  • MockHttpServletRequest(ServletContext, String, String)
  • MockHttpServletRequest

    public MockHttpServletRequest ( @Nullable ServletContext servletContext)
    Create a new MockHttpServletRequest with the supplied ServletContext .
    Parameters:
    servletContext - the ServletContext that the request runs in (may be null to use a default MockServletContext )
    See Also:
  • MockHttpServletRequest(ServletContext, String, String)
  • MockHttpServletRequest

    public MockHttpServletRequest ( @Nullable ServletContext servletContext, @Nullable String method, @Nullable String requestURI)
    Create a new MockHttpServletRequest with the supplied ServletContext , method , and requestURI .

    The preferred locale will be set to Locale.ENGLISH .

    Parameters:
    servletContext - the ServletContext that the request runs in (may be null to use a default MockServletContext )
    method - the request method (may be null )
    requestURI - the request URI (may be null )
    See Also:
  • setMethod(java.lang.String)
  • setRequestURI(java.lang.String)
  • setPreferredLocales(java.util.List<java.util.Locale>)
  • MockServletContext
  • getServletContext

    public 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 ServletRequest
    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

    setCharacterEncoding

    public void setCharacterEncoding ( @Nullable String characterEncoding)
    Specified by:
    setCharacterEncoding in interface ServletRequest
    public void setContent ( @Nullable byte[] 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:
  • setCharacterEncoding(String)
  • getContentAsByteArray()
  • getContentAsString()
  • @Nullable public byte[] getContentAsByteArray ()
    Get the content of the request body as a byte array.
    Returns:
    the content as a byte array (potentially null )
    Since:
    See Also:
  • setContent(byte[])
  • getContentAsString()
  • public String getContentAsString () throws IllegalStateException , UnsupportedEncodingException
    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:
    See Also:
  • setContent(byte[])
  • setCharacterEncoding(String)
  • getContentAsByteArray()
  • 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.

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

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

    addPreferredLocale

    public void addPreferredLocale ( Locale locale)
    Add a new preferred locale, before any existing locales.
    See Also:
  • setPreferredLocales(java.util.List<java.util.Locale>)
  • setPreferredLocales

    public void setPreferredLocales ( List < Locale > locales)
    Set the list of preferred locales, in descending order, effectively replacing any existing locales.
    Since:
    See Also:
  • addPreferredLocale(java.util.Locale)
  • 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 ServletRequest
    See Also:
  • ServletRequest.getLocale()
  • addPreferredLocale(Locale)
  • setPreferredLocales(List)
  • 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 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)
  • 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 ServletRequest
    See Also:
  • ServletRequest.isSecure()
  • Specified by:
    startAsync in interface ServletRequest
    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:

  • String or any Object to be converted using toString() ; see getHeader(java.lang.String) .
  • String, Number, or Date for date headers; see getDateHeader(java.lang.String) .
  • String or Number for integer headers; see getIntHeader(java.lang.String) .
  • String[] or Collection<String> for multiple values; see getHeaders(java.lang.String) .
  • See Also:
  • getHeaderNames()
  • getHeaders(java.lang.String)
  • getHeader(java.lang.String)
  • getDateHeader(java.lang.String)
  • 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 HttpServletRequest
    Parameters:
    name - the header name
    See Also:
  • Section 7.1.1.1 of RFC 7231
  • 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 HttpServletRequest
    Since:
    4.0.3

    authenticate

    public boolean authenticate ( HttpServletResponse response) throws IOException , ServletException
    Specified by:
    authenticate in interface HttpServletRequest
    Throws:
    IOException
    ServletException

    upgrade

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