public class MockHttpServletRequest extends Object implements 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.
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH| Constructor and Description |
|---|
MockHttpServletRequest(String contextPath,
String servletPath)
Minimal constructor that makes sense.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addHeader(String name,
Object value)
Allows headers to be set on the request.
|
void |
addLocale(Locale locale)
Adds a Locale to the set of requested locales.
|
Object |
getAttribute(String key)
Gets the named request attribute from an internal Map.
|
Enumeration<String> |
getAttributeNames()
Gets an enumeration of all request attribute names.
|
String |
getAuthType()
Gets the auth type being used by this request.
|
String |
getCharacterEncoding()
Gets the character encoding, defaults to UTF-8.
|
int |
getContentLength()
Always returns -1 (unknown).
|
String |
getContentType()
Always returns null.
|
String |
getContextPath()
Returns the context path.
|
Cookie[] |
getCookies()
Returns any cookies that are set on the request.
|
long |
getDateHeader(String name)
Gets the named header as a long.
|
String |
getForwardUrl()
Gets the URL that was forwarded to, if a forward was processed.
|
String |
getHeader(String name)
Returns any header as a String if it exists.
|
Enumeration<String> |
getHeaderNames()
Returns an enumeration containing all the names of headers supplied.
|
Enumeration<String> |
getHeaders(String name)
Returns an enumeration with single value of the named header, or an empty enum if no value.
|
List<String> |
getIncludedUrls()
Gets the list (potentially empty) or URLs that were included during the request.
|
ServletInputStream |
getInputStream()
Always returns null.
|
int |
getIntHeader(String name)
Gets the named header as an int.
|
String |
getLocalAddr()
Always returns 127.0.0.1).
|
Locale |
getLocale()
Returns the preferred locale.
|
Enumeration<Locale> |
getLocales()
Returns an enumeration of requested locales.
|
String |
getLocalName()
Always returns the same value as getServerName().
|
int |
getLocalPort()
Always returns the same value as getServerPort().
|
String |
getMethod()
Gets the method used by the request.
|
String |
getParameter(String name)
Gets the first value of the named parameter or null if a value does not exist.
|
Map<String,String[]> |
getParameterMap()
Provides access to the parameter map.
|
Enumeration<String> |
getParameterNames()
Gets an enumeration containing all the parameter names present.
|
String[] |
getParameterValues(String name)
Returns an array of all values for a parameter, or null if the parameter does not exist.
|
String |
getPathInfo()
Returns the path info.
|
String |
getPathTranslated()
Always returns the same as getPathInfo().
|
String |
getProtocol()
Gets the protocol for the request.
|
String |
getQueryString()
Returns the query string set on the request.
|
BufferedReader |
getReader()
Always returns null.
|
String |
getRealPath(String path)
Always returns the path passed in without any alteration.
|
String |
getRemoteAddr()
Aways returns "127.0.0.1".
|
String |
getRemoteHost()
Always returns "localhost".
|
int |
getRemotePort()
Always returns 1088 (and yes, that was picked arbitrarily).
|
String |
getRemoteUser()
Returns the name from the user principal if one exists, otherwise null.
|
MockRequestDispatcher |
getRequestDispatcher(String url)
Returns an instance of MockRequestDispatcher that just records what URLs are forwarded
to or included.
|
String |
getRequestedSessionId()
Returns the ID of the session if one is attached to this request.
|
String |
getRequestURI()
Returns the request URI as defined by the servlet spec.
|
StringBuffer |
getRequestURL()
Returns (an attempt at) a reconstructed URL based on it's constituent parts.
|
String |
getScheme()
Always returns the same as getProtocol.
|
String |
getServerName()
Gets the server name.
|
int |
getServerPort()
Returns the server port.
|
String |
getServletPath()
Gets the part of the path which matched the servlet.
|
HttpSession |
getSession()
Gets the session object attached to this request.
|
HttpSession |
getSession(boolean b)
Gets the session object attached to this request.
|
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(String role)
Returns true if the set of roles contains the role specified, false otherwise.
|
void |
removeAttribute(String name)
Removes any value for the named request attribute.
|
void |
setAttribute(String name,
Object value)
Sets the supplied value for the named request attribute.
|
void |
setAuthType(String authType)
Sets the auth type that will be reported by this request.
|
void |
setCharacterEncoding(String encoding)
Sets the character encoding that will be returned by getCharacterEncoding().
|
void |
setContextPath(String contextPath)
Sets the context path.
|
void |
setCookies(Cookie[] cookies)
Sets the array of cookies that will be available from the request.
|
void |
setMethod(String method)
Sets the method used by the request.
|
void |
setPathInfo(String pathInfo)
Sets the path info.
|
void |
setProtocol(String protocol)
Sets the protocol for the request.
|
void |
setQueryString(String queryString)
Sets the query string set on the request; this value is not parsed for anything.
|
void |
setRoles(Set<String> roles)
Sets the set of roles that the user is deemed to be in for the request.
|
void |
setServerName(String serverName)
Sets the server name.
|
void |
setServerPort(int serverPort)
Sets the server port.
|
void |
setSession(HttpSession session)
Allows a session to be associated with the request.
|
void |
setUserPrincipal(Principal userPrincipal)
Sets the Principal for the current request.
|
public MockHttpServletRequest(String contextPath, String servletPath)
contextPath - servletPath - public void setAuthType(String authType)
public String getAuthType()
getAuthType in interface HttpServletRequestpublic void setCookies(Cookie[] cookies)
public Cookie[] getCookies()
getCookies in interface HttpServletRequestpublic void addHeader(String name, Object value)
public long getDateHeader(String name)
getDateHeader in interface HttpServletRequestpublic String getHeader(String name)
getHeader in interface HttpServletRequestpublic Enumeration<String> getHeaders(String name)
getHeaders in interface HttpServletRequestpublic Enumeration<String> getHeaderNames()
getHeaderNames in interface HttpServletRequestpublic int getIntHeader(String name)
getIntHeader in interface HttpServletRequestpublic void setMethod(String method)
public String getMethod()
getMethod in interface HttpServletRequestpublic void setPathInfo(String pathInfo)
public String getPathInfo()
getPathInfo in interface HttpServletRequestpublic String getPathTranslated()
getPathTranslated in interface HttpServletRequestpublic void setContextPath(String contextPath)
public String getContextPath()
getContextPath in interface HttpServletRequestpublic void setQueryString(String queryString)
public String getQueryString()
getQueryString in interface HttpServletRequestpublic String getRemoteUser()
getRemoteUser in interface HttpServletRequestpublic void setRoles(Set<String> roles)
public boolean isUserInRole(String role)
isUserInRole in interface HttpServletRequestpublic void setUserPrincipal(Principal userPrincipal)
public Principal getUserPrincipal()
getUserPrincipal in interface HttpServletRequestpublic String getRequestedSessionId()
getRequestedSessionId in interface HttpServletRequestpublic String getRequestURI()
getRequestURI in interface HttpServletRequestpublic StringBuffer getRequestURL()
getRequestURL in interface HttpServletRequestpublic String getServletPath()
getServletPath in interface HttpServletRequestpublic HttpSession getSession(boolean b)
getSession in interface HttpServletRequestpublic HttpSession getSession()
getSession in interface HttpServletRequestpublic void setSession(HttpSession session)
public boolean isRequestedSessionIdValid()
isRequestedSessionIdValid in interface HttpServletRequestpublic boolean isRequestedSessionIdFromCookie()
isRequestedSessionIdFromCookie in interface HttpServletRequestpublic boolean isRequestedSessionIdFromURL()
isRequestedSessionIdFromURL in interface HttpServletRequestpublic boolean isRequestedSessionIdFromUrl()
isRequestedSessionIdFromUrl in interface HttpServletRequestpublic Object getAttribute(String key)
getAttribute in interface ServletRequestpublic Enumeration<String> getAttributeNames()
getAttributeNames in interface ServletRequestpublic String getCharacterEncoding()
getCharacterEncoding in interface ServletRequestpublic void setCharacterEncoding(String encoding)
setCharacterEncoding in interface ServletRequestpublic int getContentLength()
getContentLength in interface ServletRequestpublic String getContentType()
getContentType in interface ServletRequestpublic ServletInputStream getInputStream() throws IOException
getInputStream in interface ServletRequestIOExceptionpublic String getParameter(String name)
getParameter in interface ServletRequestpublic Enumeration<String> getParameterNames()
getParameterNames in interface ServletRequestpublic String[] getParameterValues(String name)
getParameterValues in interface ServletRequestpublic Map<String,String[]> getParameterMap()
getParameterMap in interface ServletRequestpublic void setProtocol(String protocol)
public String getProtocol()
getProtocol in interface ServletRequestpublic String getScheme()
getScheme in interface ServletRequestpublic void setServerName(String serverName)
public String getServerName()
getServerName in interface ServletRequestpublic void setServerPort(int serverPort)
public int getServerPort()
getServerPort in interface ServletRequestpublic BufferedReader getReader() throws IOException
getReader in interface ServletRequestIOExceptionpublic String getRemoteAddr()
getRemoteAddr in interface ServletRequestpublic String getRemoteHost()
getRemoteHost in interface ServletRequestpublic void setAttribute(String name, Object value)
setAttribute in interface ServletRequestpublic void removeAttribute(String name)
removeAttribute in interface ServletRequestpublic void addLocale(Locale locale)
public Locale getLocale()
getLocale in interface ServletRequestpublic Enumeration<Locale> getLocales()
getLocales in interface ServletRequestpublic boolean isSecure()
isSecure in interface ServletRequestpublic MockRequestDispatcher getRequestDispatcher(String url)
getRequestDispatcher in interface ServletRequestpublic String getRealPath(String path)
getRealPath in interface ServletRequestpublic int getRemotePort()
getRemotePort in interface ServletRequestpublic String getLocalName()
getLocalName in interface ServletRequestpublic String getLocalAddr()
getLocalAddr in interface ServletRequestpublic int getLocalPort()
getLocalPort in interface ServletRequestpublic String getForwardUrl()
© Copyright 2005-2006, Stripes Development Team.