public class

MockHttpServletRequest

extends Object
implements HttpServletRequest
java.lang.Object
   ↳ org.springframework.mock.web.MockHttpServletRequest
Known Direct Subclasses

Class Overview

Mock implementation of the javax.servlet.http.HttpServletRequest interface. Supports the Servlet 2.5 API level.

Used for testing the web framework; also useful for testing application controllers.

Summary

Constants
String DEFAULT_PROTOCOL The default protocol: 'http'.
String DEFAULT_REMOTE_ADDR The default remote address: '127.0.0.1'.
String DEFAULT_REMOTE_HOST The default remote host: 'localhost'.
String DEFAULT_SERVER_ADDR The default server address: '127.0.0.1'.
String DEFAULT_SERVER_NAME The default server name: 'localhost'.
int DEFAULT_SERVER_PORT The default server port: '80'.
[Expand]
Inherited Constants
From interface javax.servlet.http.HttpServletRequest
Public Constructors
MockHttpServletRequest()
Create a new MockHttpServletRequest with a default MockServletContext.
MockHttpServletRequest(String method, String requestURI)
Create a new MockHttpServletRequest with a default MockServletContext.
MockHttpServletRequest(ServletContext servletContext)
Create a new MockHttpServletRequest.
MockHttpServletRequest(ServletContext servletContext, String method, String requestURI)
Create a new MockHttpServletRequest.
Public Methods
void addHeader(String name, Object value)
Add a header entry for the given name.
void addParameter(String name, String value)
Add a single value for the specified HTTP parameter.
void addParameter(String name, String[] values)
Add an array of values for the specified HTTP parameter.
void addParameters(Map params)
Adds all provided parameters without replacing any existing values.
void addPreferredLocale(Locale locale)
Add a new preferred locale, before any existing locales.
void addUserRole(String role)
void clearAttributes()
Clear all of this request's attributes.
void close()
Mark this request as completed, keeping its state.
Object getAttribute(String name)
Enumeration<String> getAttributeNames()
String getAuthType()
String getCharacterEncoding()
int getContentLength()
String getContentType()
String getContextPath()
Cookie[] getCookies()
long getDateHeader(String name)
String getHeader(String name)
Enumeration<String> getHeaderNames()
Enumeration<String> getHeaders(String name)
ServletInputStream getInputStream()
int getIntHeader(String name)
String getLocalAddr()
String getLocalName()
int getLocalPort()
Locale getLocale()
Enumeration<Locale> getLocales()
String getMethod()
String getParameter(String name)
Map<StringString[]> getParameterMap()
Enumeration<String> getParameterNames()
String[] getParameterValues(String name)
String getPathInfo()
String getPathTranslated()
String getProtocol()
String getQueryString()
BufferedReader getReader()
String getRealPath(String path)
String getRemoteAddr()
String getRemoteHost()
int getRemotePort()
String getRemoteUser()
RequestDispatcher getRequestDispatcher(String path)
String getRequestURI()
StringBuffer getRequestURL()
String getRequestedSessionId()
String getScheme()
String getServerName()
int getServerPort()
ServletContext getServletContext()
Return the ServletContext that this request is associated with.
String getServletPath()
HttpSession getSession()
HttpSession getSession(boolean create)
Principal getUserPrincipal()
void invalidate()
Invalidate this request, clearing its state.
boolean isActive()
Return whether this request is still active (that is, not completed yet).
boolean isRequestedSessionIdFromCookie()
boolean isRequestedSessionIdFromURL()
boolean isRequestedSessionIdFromUrl()
boolean isRequestedSessionIdValid()
boolean isSecure()
boolean isUserInRole(String role)
void removeAllParameters()
Removes all existing parameters.
void removeAttribute(String name)
void removeParameter(String name)
Remove already registered values for the specified HTTP parameter, if any.
void setAttribute(String name, Object value)
void setAuthType(String authType)
void setCharacterEncoding(String characterEncoding)
void setContent(byte[] content)
void setContentType(String contentType)
void setContextPath(String contextPath)
void setCookies(Cookie... cookies)
void setLocalAddr(String localAddr)
void setLocalName(String localName)
void setLocalPort(int localPort)
void setMethod(String method)
void setParameter(String name, String[] values)
Set an array of values for the specified HTTP parameter.
void setParameter(String name, String value)
Set a single value for the specified HTTP parameter.
void setParameters(Map params)
Sets all provided parameters replacing any existing values for the provided parameter names.
void setPathInfo(String pathInfo)
void setProtocol(String protocol)
void setQueryString(String queryString)
void setRemoteAddr(String remoteAddr)
void setRemoteHost(String remoteHost)
void setRemotePort(int remotePort)
void setRemoteUser(String remoteUser)
void setRequestURI(String requestURI)
void setRequestedSessionId(String requestedSessionId)
void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie)
void setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL)
void setRequestedSessionIdValid(boolean requestedSessionIdValid)
void setScheme(String scheme)
void setSecure(boolean secure)
void setServerName(String serverName)
void setServerPort(int serverPort)
void setServletPath(String servletPath)
void setSession(HttpSession session)
void setUserPrincipal(Principal userPrincipal)
Protected Methods
void checkActive()
Check whether this request is still active (that is, not completed yet), throwing an IllegalStateException if not active anymore.
[Expand]
Inherited Methods
From class java.lang.Object
From interface javax.servlet.ServletRequest
From interface javax.servlet.http.HttpServletRequest

Constants

public static final String DEFAULT_PROTOCOL

The default protocol: 'http'.

Constant Value: "http"

public static final String DEFAULT_REMOTE_ADDR

The default remote address: '127.0.0.1'.

Constant Value: "127.0.0.1"

public static final String DEFAULT_REMOTE_HOST

The default remote host: 'localhost'.

Constant Value: "localhost"

public static final String DEFAULT_SERVER_ADDR

The default server address: '127.0.0.1'.

Constant Value: "127.0.0.1"

public static final String DEFAULT_SERVER_NAME

The default server name: 'localhost'.

Constant Value: "localhost"

public static final int DEFAULT_SERVER_PORT

The default server port: '80'.

Constant Value: 80 (0x00000050)

Public Constructors

public MockHttpServletRequest ()

Create a new MockHttpServletRequest with a default MockServletContext.

public MockHttpServletRequest (String method, 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)

public MockHttpServletRequest (ServletContext servletContext)

Create a new MockHttpServletRequest.

Parameters
servletContext the ServletContext that the request runs in (may be null to use a default MockServletContext)

public MockHttpServletRequest (ServletContext servletContext, String method, String requestURI)

Create a new MockHttpServletRequest.

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)

Public Methods

public void addHeader (String name, Object value)

Add a header entry for the given name.

If there was no entry for that header name before, the value will be used as-is. In case of an existing entry, a String array will be created, adding the given value (more specifically, its toString representation) as further element.

Multiple values can only be stored as list of Strings, following the Servlet spec (see getHeaders accessor). As alternative to repeated addHeader calls for individual elements, you can use a single call with an entire array or Collection of values as parameter.

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.

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.

public void addParameters (Map params)

Adds all provided parameters without replacing any existing values. To replace existing values, use setParameters(java.util.Map).

public void addPreferredLocale (Locale locale)

Add a new preferred locale, before any existing locales.

public void addUserRole (String role)

public void clearAttributes ()

Clear all of this request's attributes.

public void close ()

Mark this request as completed, keeping its state.

public Object getAttribute (String name)

public Enumeration<String> getAttributeNames ()

public String getAuthType ()

public String getCharacterEncoding ()

public int getContentLength ()

public String getContentType ()

public String getContextPath ()

public Cookie[] getCookies ()

public long getDateHeader (String name)

public String getHeader (String name)

public Enumeration<String> getHeaderNames ()

public Enumeration<String> getHeaders (String name)

public ServletInputStream getInputStream ()

public int getIntHeader (String name)

public String getLocalAddr ()

public String getLocalName ()

public int getLocalPort ()

public Locale getLocale ()

public Enumeration<Locale> getLocales ()

public String getMethod ()

public String getParameter (String name)

public Map<StringString[]> getParameterMap ()

public Enumeration<String> getParameterNames ()

public String[] getParameterValues (String name)

public String getPathInfo ()

public String getPathTranslated ()

public String getProtocol ()

public String getQueryString ()

public BufferedReader getReader ()

public String getRealPath (String path)

public String getRemoteAddr ()

public String getRemoteHost ()

public int getRemotePort ()

public String getRemoteUser ()

public RequestDispatcher getRequestDispatcher (String path)

public String getRequestURI ()

public StringBuffer getRequestURL ()

public String getRequestedSessionId ()

public String getScheme ()

public String getServerName ()

public int getServerPort ()

public ServletContext getServletContext ()

Return the ServletContext that this request is associated with. (Not available in the standard HttpServletRequest interface for some reason.)

public String getServletPath ()

public HttpSession getSession ()

public HttpSession getSession (boolean create)

public Principal getUserPrincipal ()

public void invalidate ()

Invalidate this request, clearing its state.

public boolean isActive ()

Return whether this request is still active (that is, not completed yet).

public boolean isRequestedSessionIdFromCookie ()

public boolean isRequestedSessionIdFromURL ()

public boolean isRequestedSessionIdFromUrl ()

public boolean isRequestedSessionIdValid ()

public boolean isSecure ()

public boolean isUserInRole (String role)

public void removeAllParameters ()

Removes all existing parameters.

public void removeAttribute (String name)

public void removeParameter (String name)

Remove already registered values for the specified HTTP parameter, if any.

public void setAttribute (String name, Object value)

public void setAuthType (String authType)

public void setCharacterEncoding (String characterEncoding)

public void setContent (byte[] content)

public void setContentType (String contentType)

public void setContextPath (String contextPath)

public void setCookies (Cookie... cookies)

public void setLocalAddr (String localAddr)

public void setLocalName (String localName)

public void setLocalPort (int localPort)

public void setMethod (String method)

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.

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

public void setPathInfo (String pathInfo)

public void setProtocol (String protocol)

public void setQueryString (String queryString)

public void setRemoteAddr (String remoteAddr)

public void setRemoteHost (String remoteHost)

public void setRemotePort (int remotePort)

public void setRemoteUser (String remoteUser)

public void setRequestURI (String requestURI)

public void setRequestedSessionId (String requestedSessionId)

public void setRequestedSessionIdFromCookie (boolean requestedSessionIdFromCookie)

public void setRequestedSessionIdFromURL (boolean requestedSessionIdFromURL)

public void setRequestedSessionIdValid (boolean requestedSessionIdValid)

public void setScheme (String scheme)

public void setSecure (boolean secure)

public void setServerName (String serverName)

public void setServerPort (int serverPort)

public void setServletPath (String servletPath)

public void setSession (HttpSession session)

public void setUserPrincipal (Principal userPrincipal)

Protected Methods

protected void checkActive ()

Check whether this request is still active (that is, not completed yet), throwing an IllegalStateException if not active anymore.