Spring Framework

org.springframework.mock.web
Class MockHttpServletRequest

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

public class MockHttpServletRequest
extends java.lang.Object
implements javax.servlet.http.HttpServletRequest

Mock implementation of the HttpServletRequest interface.

Compatible with Servlet 2.5 and partially with Servlet 3.0 (notable exceptions: the getPart(s) and startAsync families of methods).

Since:
1.0.2
Author:
Juergen Hoeller, Rod Johnson, Rick Evans, Mark Fisher

Field Summary
static java.lang.String DEFAULT_PROTOCOL
          The default protocol: 'http'.
static java.lang.String DEFAULT_REMOTE_ADDR
          The default remote address: '127.0.0.1'.
static java.lang.String DEFAULT_REMOTE_HOST
          The default remote host: 'localhost'.
static java.lang.String DEFAULT_SERVER_ADDR
          The default server address: '127.0.0.1'.
static java.lang.String DEFAULT_SERVER_NAME
          The default server name: 'localhost'.
static int DEFAULT_SERVER_PORT
          The default server port: '80'.
 
Fields inherited from interface javax.servlet.http.HttpServletRequest
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
 
Constructor Summary
MockHttpServletRequest()
          Create a new MockHttpServletRequest with a default MockServletContext.
MockHttpServletRequest(javax.servlet.ServletContext servletContext)
          Create a new MockHttpServletRequest.
MockHttpServletRequest(javax.servlet.ServletContext servletContext, java.lang.String method, java.lang.String requestURI)
          Create a new MockHttpServletRequest.
MockHttpServletRequest(java.lang.String method, java.lang.String requestURI)
          Create a new MockHttpServletRequest with a default MockServletContext.
 
Method Summary
 void addHeader(java.lang.String name, java.lang.Object value)
          Add a header entry for the given name.
 void addParameter(java.lang.String name, java.lang.String value)
          Add a single value for the specified HTTP parameter.
 void addParameter(java.lang.String name, java.lang.String[] values)
          Add an array of values for the specified HTTP parameter.
 void addParameters(java.util.Map params)
          Adds all provided parameters without replacing any existing values.
 void addPreferredLocale(java.util.Locale locale)
          Add a new preferred locale, before any existing locales.
 void addUserRole(java.lang.String role)
           
 boolean authenticate(javax.servlet.http.HttpServletResponse response)
           
protected  void checkActive()
          Check whether this request is still active (that is, not completed yet), throwing an IllegalStateException if not active anymore.
 void clearAttributes()
          Clear all of this request's attributes.
 void close()
          Mark this request as completed, keeping its state.
 java.lang.Object getAttribute(java.lang.String name)
           
 java.util.Enumeration<java.lang.String> getAttributeNames()
           
 java.lang.String getAuthType()
           
 java.lang.String getCharacterEncoding()
           
 int getContentLength()
           
 java.lang.String getContentType()
           
 java.lang.String getContextPath()
           
 javax.servlet.http.Cookie[] getCookies()
           
 long getDateHeader(java.lang.String name)
           
 java.lang.String getHeader(java.lang.String name)
           
 java.util.Enumeration<java.lang.String> getHeaderNames()
           
 java.util.Enumeration<java.lang.String> getHeaders(java.lang.String name)
           
 javax.servlet.ServletInputStream getInputStream()
           
 int getIntHeader(java.lang.String name)
           
 java.lang.String getLocalAddr()
           
 java.util.Locale getLocale()
           
 java.util.Enumeration<java.util.Locale> getLocales()
           
 java.lang.String getLocalName()
           
 int getLocalPort()
           
 java.lang.String getMethod()
           
 java.lang.String getParameter(java.lang.String name)
           
 java.util.Map<java.lang.String,java.lang.String[]> getParameterMap()
           
 java.util.Enumeration<java.lang.String> getParameterNames()
           
 java.lang.String[] getParameterValues(java.lang.String name)
           
 java.lang.String getPathInfo()
           
 java.lang.String getPathTranslated()
           
 java.lang.String getProtocol()
           
 java.lang.String getQueryString()
           
 java.io.BufferedReader getReader()
           
 java.lang.String getRealPath(java.lang.String path)
           
 java.lang.String getRemoteAddr()
           
 java.lang.String getRemoteHost()
           
 int getRemotePort()
           
 java.lang.String getRemoteUser()
           
 javax.servlet.RequestDispatcher getRequestDispatcher(java.lang.String path)
           
 java.lang.String getRequestedSessionId()
           
 java.lang.String getRequestURI()
           
 java.lang.StringBuffer getRequestURL()
           
 java.lang.String getScheme()
           
 java.lang.String getServerName()
           
 int getServerPort()
           
 javax.servlet.ServletContext getServletContext()
          Return the ServletContext that this request is associated with.
 java.lang.String getServletPath()
           
 javax.servlet.http.HttpSession getSession()
           
 javax.servlet.http.HttpSession getSession(boolean create)
           
 java.security.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(java.lang.String role)
           
 void login(java.lang.String username, java.lang.String password)
           
 void logout()
           
 void removeAllParameters()
          Removes all existing parameters.
 void removeAttribute(java.lang.String name)
           
 void removeParameter(java.lang.String name)
          Remove already registered values for the specified HTTP parameter, if any.
 void setAttribute(java.lang.String name, java.lang.Object value)
           
 void setAuthType(java.lang.String authType)
           
 void setCharacterEncoding(java.lang.String characterEncoding)
           
 void setContent(byte[] content)
           
 void setContentType(java.lang.String contentType)
           
 void setContextPath(java.lang.String contextPath)
           
 void setCookies(javax.servlet.http.Cookie... cookies)
           
 void setLocalAddr(java.lang.String localAddr)
           
 void setLocalName(java.lang.String localName)
           
 void setLocalPort(int localPort)
           
 void setMethod(java.lang.String method)
           
 void setParameter(java.lang.String name, java.lang.String value)
          Set a single value for the specified HTTP parameter.
 void setParameter(java.lang.String name, java.lang.String[] values)
          Set an array of values for the specified HTTP parameter.
 void setParameters(java.util.Map params)
          Sets all provided parameters replacing any existing values for the provided parameter names.
 void setPathInfo(java.lang.String pathInfo)
           
 void setProtocol(java.lang.String protocol)
           
 void setQueryString(java.lang.String queryString)
           
 void setRemoteAddr(java.lang.String remoteAddr)
           
 void setRemoteHost(java.lang.String remoteHost)
           
 void setRemotePort(int remotePort)
           
 void setRemoteUser(java.lang.String remoteUser)
           
 void setRequestedSessionId(java.lang.String requestedSessionId)
           
 void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie)
           
 void setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL)
           
 void setRequestedSessionIdValid(boolean requestedSessionIdValid)
           
 void setRequestURI(java.lang.String requestURI)
           
 void setScheme(java.lang.String scheme)
           
 void setSecure(boolean secure)
           
 void setServerName(java.lang.String serverName)
           
 void setServerPort(int serverPort)
           
 void setServletPath(java.lang.String servletPath)
           
 void setSession(javax.servlet.http.HttpSession session)
           
 void setUserPrincipal(java.security.Principal userPrincipal)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.servlet.http.HttpServletRequest
getPart, getParts
 
Methods inherited from interface javax.servlet.ServletRequest
getAsyncContext, getDispatcherType, isAsyncStarted, isAsyncSupported, startAsync, startAsync
 

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
Constructor Detail

MockHttpServletRequest

public MockHttpServletRequest()
Create a new MockHttpServletRequest with a default MockServletContext.

See Also:
MockServletContext

MockHttpServletRequest

public MockHttpServletRequest(java.lang.String method,
                              java.lang.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), MockServletContext

MockHttpServletRequest

public MockHttpServletRequest(javax.servlet.ServletContext servletContext)
Create a new MockHttpServletRequest.

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

MockHttpServletRequest

public MockHttpServletRequest(javax.servlet.ServletContext servletContext,
                              java.lang.String method,
                              java.lang.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)
See Also:
setMethod(java.lang.String), setRequestURI(java.lang.String), MockServletContext
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

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

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.


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.


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


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.


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.


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


removeParameter

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


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.


addPreferredLocale

public void addPreferredLocale(java.util.Locale locale)
Add a new preferred locale, before any existing locales.


getLocale

public java.util.Locale getLocale()
Specified by:
getLocale in interface javax.servlet.ServletRequest

getLocales

public java.util.Enumeration<java.util.Locale> getLocales()
Specified by:
getLocales in interface javax.servlet.ServletRequest

setSecure

public void setSecure(boolean secure)

isSecure

public boolean isSecure()
Specified by:
isSecure in interface javax.servlet.ServletRequest

getRequestDispatcher

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

getRealPath

public java.lang.String getRealPath(java.lang.String path)
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

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

addHeader

public void addHeader(java.lang.String name,
                      java.lang.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.

See Also:
getHeaderNames(), getHeader(java.lang.String), getHeaders(java.lang.String), getDateHeader(java.lang.String), getIntHeader(java.lang.String)

getDateHeader

public long getDateHeader(java.lang.String name)
Specified by:
getDateHeader in interface javax.servlet.http.HttpServletRequest

getIntHeader

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

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

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

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

public boolean isRequestedSessionIdFromUrl()
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

Spring Framework