public class MockHttpServletRequest
extends java.lang.Object
implements javax.servlet.http.HttpServletRequest
Mock implementation of an HttpServletRequest object. Allows for setting most values that are likely to be of interest (and can always be subclassed to affect others). Of key interest and perhaps not completely obvious, the way to get request parameters into an instance of MockHttpServletRequest is to fetch the parameter map using getParameterMap() and use the put() and putAll() methods on it. Values must be String arrays. Examples follow:
MockHttpServletRequest req = new MockHttpServletRequest("/foo", "/bar.action"); req.getParameterMap().put("param1", new String[] {"value"}); req.getParameterMap().put("param2", new String[] {"value1", "value2"});
It should also be noted that unless you generate an instance of MockHttpSession (or another implementation of HttpSession) and set it on the request, then your request will never have a session associated with it.
Constructor and Description |
---|
MockHttpServletRequest(java.lang.String contextPath,
java.lang.String servletPath)
Minimal constructor that makes sense.
|
Modifier and Type | Method and Description |
---|---|
void |
addHeader(java.lang.String name,
java.lang.Object value)
Allows headers to be set on the request.
|
void |
addLocale(java.util.Locale locale)
Adds a Locale to the set of requested locales.
|
java.lang.Object |
getAttribute(java.lang.String key)
Gets the named request attribute from an internal Map.
|
java.util.Enumeration<java.lang.String> |
getAttributeNames()
Gets an enumeration of all request attribute names.
|
java.lang.String |
getAuthType()
Gets the auth type being used by this request.
|
java.lang.String |
getCharacterEncoding()
Gets the character encoding, defaults to UTF-8.
|
int |
getContentLength()
Always returns -1 (unknown).
|
java.lang.String |
getContentType()
Always returns null.
|
java.lang.String |
getContextPath()
Returns the context path.
|
javax.servlet.http.Cookie[] |
getCookies()
Returns any cookies that are set on the request.
|
long |
getDateHeader(java.lang.String name)
Gets the named header as a long.
|
java.lang.String |
getForwardUrl()
Gets the URL that was forwarded to, if a forward was processed.
|
java.lang.String |
getHeader(java.lang.String name)
Returns any header as a String if it exists.
|
java.util.Enumeration<java.lang.String> |
getHeaderNames()
Returns an enumeration containing all the names of headers supplied.
|
java.util.Enumeration<java.lang.String> |
getHeaders(java.lang.String name)
Returns an enumeration with single value of the named header, or an empty enum if no value.
|
java.util.List<java.lang.String> |
getIncludedUrls()
Gets the list (potentially empty) or URLs that were included during the request.
|
javax.servlet.ServletInputStream |
getInputStream()
Always returns null.
|
int |
getIntHeader(java.lang.String name)
Gets the named header as an int.
|
java.lang.String |
getLocalAddr()
Always returns 127.0.0.1).
|
java.util.Locale |
getLocale()
Returns the preferred locale.
|
java.util.Enumeration<java.util.Locale> |
getLocales()
Returns an enumeration of requested locales.
|
java.lang.String |
getLocalName()
Always returns the same value as getServerName().
|
int |
getLocalPort()
Always returns the same value as getServerPort().
|
java.lang.String |
getMethod()
Gets the method used by the request.
|
java.lang.String |
getParameter(java.lang.String name)
Gets the first value of the named parameter or null if a value does not exist.
|
java.util.Map<java.lang.String,java.lang.String[]> |
getParameterMap()
Provides access to the parameter map.
|
java.util.Enumeration<java.lang.String> |
getParameterNames()
Gets an enumeration containing all the parameter names present.
|
java.lang.String[] |
getParameterValues(java.lang.String name)
Returns an array of all values for a parameter, or null if the parameter does not exist.
|
java.lang.String |
getPathInfo()
Returns the path info.
|
java.lang.String |
getPathTranslated()
Always returns the same as getPathInfo().
|
java.lang.String |
getProtocol()
Gets the protocol for the request.
|
java.lang.String |
getQueryString()
Returns the query string set on the request.
|
java.io.BufferedReader |
getReader()
Always returns null.
|
java.lang.String |
getRealPath(java.lang.String path)
Always returns the path passed in without any alteration.
|
java.lang.String |
getRemoteAddr()
Aways returns "127.0.0.1".
|
java.lang.String |
getRemoteHost()
Always returns "localhost".
|
int |
getRemotePort()
Always returns 1088 (and yes, that was picked arbitrarily).
|
java.lang.String |
getRemoteUser()
Returns the name from the user principal if one exists, otherwise null.
|
MockRequestDispatcher |
getRequestDispatcher(java.lang.String url)
Returns an instance of MockRequestDispatcher that just records what URLs are forwarded
to or included.
|
java.lang.String |
getRequestedSessionId()
Returns the ID of the session if one is attached to this request.
|
java.lang.String |
getRequestURI()
Returns the request URI as defined by the servlet spec.
|
java.lang.StringBuffer |
getRequestURL()
Returns (an attempt at) a reconstructed URL based on it's constituent parts.
|
java.lang.String |
getScheme()
Always returns the same as getProtocol.
|
java.lang.String |
getServerName()
Gets the server name.
|
int |
getServerPort()
Returns the server port.
|
java.lang.String |
getServletPath()
Gets the part of the path which matched the servlet.
|
javax.servlet.http.HttpSession |
getSession()
Gets the session object attached to this request.
|
javax.servlet.http.HttpSession |
getSession(boolean b)
Gets the session object attached to this request.
|
java.security.Principal |
getUserPrincipal()
Returns the Principal if one is set on the request.
|
boolean |
isRequestedSessionIdFromCookie()
Always returns true.
|
boolean |
isRequestedSessionIdFromUrl()
Always returns false.
|
boolean |
isRequestedSessionIdFromURL()
Always returns false.
|
boolean |
isRequestedSessionIdValid()
Always returns true.
|
boolean |
isSecure()
Returns true if the protocol is set to https (default), false otherwise.
|
boolean |
isUserInRole(java.lang.String role)
Returns true if the set of roles contains the role specified, false otherwise.
|
void |
removeAttribute(java.lang.String name)
Removes any value for the named request attribute.
|
void |
setAttribute(java.lang.String name,
java.lang.Object value)
Sets the supplied value for the named request attribute.
|
void |
setAuthType(java.lang.String authType)
Sets the auth type that will be reported by this request.
|
void |
setCharacterEncoding(java.lang.String encoding)
Sets the character encoding that will be returned by getCharacterEncoding().
|
void |
setContextPath(java.lang.String contextPath)
Sets the context path.
|
void |
setCookies(javax.servlet.http.Cookie[] cookies)
Sets the array of cookies that will be available from the request.
|
void |
setMethod(java.lang.String method)
Sets the method used by the request.
|
void |
setPathInfo(java.lang.String pathInfo)
Sets the path info.
|
void |
setProtocol(java.lang.String protocol)
Sets the protocol for the request.
|
void |
setQueryString(java.lang.String queryString)
Sets the query string set on the request; this value is not parsed for anything.
|
void |
setRoles(java.util.Set<java.lang.String> roles)
Sets the set of roles that the user is deemed to be in for the request.
|
void |
setServerName(java.lang.String serverName)
Sets the server name.
|
void |
setServerPort(int serverPort)
Sets the server port.
|
void |
setSession(javax.servlet.http.HttpSession session)
Allows a session to be associated with the request.
|
void |
setUserPrincipal(java.security.Principal userPrincipal)
Sets the Principal for the current request.
|
public MockHttpServletRequest(java.lang.String contextPath, java.lang.String servletPath)
contextPath
- servletPath
- public void setAuthType(java.lang.String authType)
public java.lang.String getAuthType()
getAuthType
in interface javax.servlet.http.HttpServletRequest
public void setCookies(javax.servlet.http.Cookie[] cookies)
public javax.servlet.http.Cookie[] getCookies()
getCookies
in interface javax.servlet.http.HttpServletRequest
public void addHeader(java.lang.String name, java.lang.Object value)
public long getDateHeader(java.lang.String name)
getDateHeader
in interface javax.servlet.http.HttpServletRequest
public java.lang.String getHeader(java.lang.String name)
getHeader
in interface javax.servlet.http.HttpServletRequest
public java.util.Enumeration<java.lang.String> getHeaders(java.lang.String name)
getHeaders
in interface javax.servlet.http.HttpServletRequest
public java.util.Enumeration<java.lang.String> getHeaderNames()
getHeaderNames
in interface javax.servlet.http.HttpServletRequest
public int getIntHeader(java.lang.String name)
getIntHeader
in interface javax.servlet.http.HttpServletRequest
public void setMethod(java.lang.String method)
public java.lang.String getMethod()
getMethod
in interface javax.servlet.http.HttpServletRequest
public void setPathInfo(java.lang.String pathInfo)
public java.lang.String getPathInfo()
getPathInfo
in interface javax.servlet.http.HttpServletRequest
public java.lang.String getPathTranslated()
getPathTranslated
in interface javax.servlet.http.HttpServletRequest
public void setContextPath(java.lang.String contextPath)
public java.lang.String getContextPath()
getContextPath
in interface javax.servlet.http.HttpServletRequest
public void setQueryString(java.lang.String queryString)
public java.lang.String getQueryString()
getQueryString
in interface javax.servlet.http.HttpServletRequest
public java.lang.String getRemoteUser()
getRemoteUser
in interface javax.servlet.http.HttpServletRequest
public void setRoles(java.util.Set<java.lang.String> roles)
public boolean isUserInRole(java.lang.String role)
isUserInRole
in interface javax.servlet.http.HttpServletRequest
public void setUserPrincipal(java.security.Principal userPrincipal)
public java.security.Principal getUserPrincipal()
getUserPrincipal
in interface javax.servlet.http.HttpServletRequest
public java.lang.String getRequestedSessionId()
getRequestedSessionId
in interface javax.servlet.http.HttpServletRequest
public java.lang.String getRequestURI()
getRequestURI
in interface javax.servlet.http.HttpServletRequest
public java.lang.StringBuffer getRequestURL()
getRequestURL
in interface javax.servlet.http.HttpServletRequest
public java.lang.String getServletPath()
getServletPath
in interface javax.servlet.http.HttpServletRequest
public javax.servlet.http.HttpSession getSession(boolean b)
getSession
in interface javax.servlet.http.HttpServletRequest
public javax.servlet.http.HttpSession getSession()
getSession
in interface javax.servlet.http.HttpServletRequest
public void setSession(javax.servlet.http.HttpSession session)
public boolean isRequestedSessionIdValid()
isRequestedSessionIdValid
in interface javax.servlet.http.HttpServletRequest
public boolean isRequestedSessionIdFromCookie()
isRequestedSessionIdFromCookie
in interface javax.servlet.http.HttpServletRequest
public boolean isRequestedSessionIdFromURL()
isRequestedSessionIdFromURL
in interface javax.servlet.http.HttpServletRequest
public boolean isRequestedSessionIdFromUrl()
isRequestedSessionIdFromUrl
in interface javax.servlet.http.HttpServletRequest
public java.lang.Object getAttribute(java.lang.String key)
getAttribute
in interface javax.servlet.ServletRequest
public java.util.Enumeration<java.lang.String> getAttributeNames()
getAttributeNames
in interface javax.servlet.ServletRequest
public java.lang.String getCharacterEncoding()
getCharacterEncoding
in interface javax.servlet.ServletRequest
public void setCharacterEncoding(java.lang.String encoding)
setCharacterEncoding
in interface javax.servlet.ServletRequest
public int getContentLength()
getContentLength
in interface javax.servlet.ServletRequest
public java.lang.String getContentType()
getContentType
in interface javax.servlet.ServletRequest
public javax.servlet.ServletInputStream getInputStream() throws java.io.IOException
getInputStream
in interface javax.servlet.ServletRequest
java.io.IOException
public java.lang.String getParameter(java.lang.String name)
getParameter
in interface javax.servlet.ServletRequest
public java.util.Enumeration<java.lang.String> getParameterNames()
getParameterNames
in interface javax.servlet.ServletRequest
public java.lang.String[] getParameterValues(java.lang.String name)
getParameterValues
in interface javax.servlet.ServletRequest
public java.util.Map<java.lang.String,java.lang.String[]> getParameterMap()
getParameterMap
in interface javax.servlet.ServletRequest
public void setProtocol(java.lang.String protocol)
public java.lang.String getProtocol()
getProtocol
in interface javax.servlet.ServletRequest
public java.lang.String getScheme()
getScheme
in interface javax.servlet.ServletRequest
public void setServerName(java.lang.String serverName)
public java.lang.String getServerName()
getServerName
in interface javax.servlet.ServletRequest
public void setServerPort(int serverPort)
public int getServerPort()
getServerPort
in interface javax.servlet.ServletRequest
public java.io.BufferedReader getReader() throws java.io.IOException
getReader
in interface javax.servlet.ServletRequest
java.io.IOException
public java.lang.String getRemoteAddr()
getRemoteAddr
in interface javax.servlet.ServletRequest
public java.lang.String getRemoteHost()
getRemoteHost
in interface javax.servlet.ServletRequest
public void setAttribute(java.lang.String name, java.lang.Object value)
setAttribute
in interface javax.servlet.ServletRequest
public void removeAttribute(java.lang.String name)
removeAttribute
in interface javax.servlet.ServletRequest
public void addLocale(java.util.Locale locale)
public java.util.Locale getLocale()
getLocale
in interface javax.servlet.ServletRequest
public java.util.Enumeration<java.util.Locale> getLocales()
getLocales
in interface javax.servlet.ServletRequest
public boolean isSecure()
isSecure
in interface javax.servlet.ServletRequest
public MockRequestDispatcher getRequestDispatcher(java.lang.String url)
getRequestDispatcher
in interface javax.servlet.ServletRequest
public java.lang.String getRealPath(java.lang.String path)
getRealPath
in interface javax.servlet.ServletRequest
public int getRemotePort()
getRemotePort
in interface javax.servlet.ServletRequest
public java.lang.String getLocalName()
getLocalName
in interface javax.servlet.ServletRequest
public java.lang.String getLocalAddr()
getLocalAddr
in interface javax.servlet.ServletRequest
public int getLocalPort()
getLocalPort
in interface javax.servlet.ServletRequest
public java.lang.String getForwardUrl()
public java.util.List<java.lang.String> getIncludedUrls()
? Copyright 2005-2006, Stripes Development Team.