|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sourceforge.stripes.mock.MockHttpServletRequest
public class MockHttpServletRequest
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.
Field Summary |
---|
Fields inherited from interface javax.servlet.http.HttpServletRequest |
---|
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH |
Constructor Summary | |
---|---|
MockHttpServletRequest(String contextPath,
String servletPath)
Minimal constructor that makes sense. |
Method Summary | |
---|---|
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public MockHttpServletRequest(String contextPath, String servletPath)
contextPath
- servletPath
- Method Detail |
---|
public void setAuthType(String authType)
public String getAuthType()
getAuthType
in interface HttpServletRequest
public void setCookies(Cookie[] cookies)
public Cookie[] getCookies()
getCookies
in interface HttpServletRequest
public void addHeader(String name, Object value)
public long getDateHeader(String name)
getDateHeader
in interface HttpServletRequest
public String getHeader(String name)
getHeader
in interface HttpServletRequest
public Enumeration<String> getHeaders(String name)
getHeaders
in interface HttpServletRequest
public Enumeration<String> getHeaderNames()
getHeaderNames
in interface HttpServletRequest
public int getIntHeader(String name)
getIntHeader
in interface HttpServletRequest
public void setMethod(String method)
public String getMethod()
getMethod
in interface HttpServletRequest
public void setPathInfo(String pathInfo)
public String getPathInfo()
getPathInfo
in interface HttpServletRequest
public String getPathTranslated()
getPathTranslated
in interface HttpServletRequest
public void setContextPath(String contextPath)
public String getContextPath()
getContextPath
in interface HttpServletRequest
public void setQueryString(String queryString)
public String getQueryString()
getQueryString
in interface HttpServletRequest
public String getRemoteUser()
getRemoteUser
in interface HttpServletRequest
public void setRoles(Set<String> roles)
public boolean isUserInRole(String role)
isUserInRole
in interface HttpServletRequest
public void setUserPrincipal(Principal userPrincipal)
public Principal getUserPrincipal()
getUserPrincipal
in interface HttpServletRequest
public String getRequestedSessionId()
getRequestedSessionId
in interface HttpServletRequest
public String getRequestURI()
getRequestURI
in interface HttpServletRequest
public StringBuffer getRequestURL()
getRequestURL
in interface HttpServletRequest
public String getServletPath()
getServletPath
in interface HttpServletRequest
public HttpSession getSession(boolean b)
getSession
in interface HttpServletRequest
public HttpSession getSession()
getSession
in interface HttpServletRequest
public void setSession(HttpSession session)
public boolean isRequestedSessionIdValid()
isRequestedSessionIdValid
in interface HttpServletRequest
public boolean isRequestedSessionIdFromCookie()
isRequestedSessionIdFromCookie
in interface HttpServletRequest
public boolean isRequestedSessionIdFromURL()
isRequestedSessionIdFromURL
in interface HttpServletRequest
public boolean isRequestedSessionIdFromUrl()
isRequestedSessionIdFromUrl
in interface HttpServletRequest
public Object getAttribute(String key)
getAttribute
in interface ServletRequest
public Enumeration<String> getAttributeNames()
getAttributeNames
in interface ServletRequest
public String getCharacterEncoding()
getCharacterEncoding
in interface ServletRequest
public void setCharacterEncoding(String encoding)
setCharacterEncoding
in interface ServletRequest
public int getContentLength()
getContentLength
in interface ServletRequest
public String getContentType()
getContentType
in interface ServletRequest
public ServletInputStream getInputStream() throws IOException
getInputStream
in interface ServletRequest
IOException
public String getParameter(String name)
getParameter
in interface ServletRequest
public Enumeration<String> getParameterNames()
getParameterNames
in interface ServletRequest
public String[] getParameterValues(String name)
getParameterValues
in interface ServletRequest
public Map<String,String[]> getParameterMap()
getParameterMap
in interface ServletRequest
public void setProtocol(String protocol)
public String getProtocol()
getProtocol
in interface ServletRequest
public String getScheme()
getScheme
in interface ServletRequest
public void setServerName(String serverName)
public String getServerName()
getServerName
in interface ServletRequest
public void setServerPort(int serverPort)
public int getServerPort()
getServerPort
in interface ServletRequest
public BufferedReader getReader() throws IOException
getReader
in interface ServletRequest
IOException
public String getRemoteAddr()
getRemoteAddr
in interface ServletRequest
public String getRemoteHost()
getRemoteHost
in interface ServletRequest
public void setAttribute(String name, Object value)
setAttribute
in interface ServletRequest
public void removeAttribute(String name)
removeAttribute
in interface ServletRequest
public void addLocale(Locale locale)
public Locale getLocale()
getLocale
in interface ServletRequest
public Enumeration<Locale> getLocales()
getLocales
in interface ServletRequest
public boolean isSecure()
isSecure
in interface ServletRequest
public MockRequestDispatcher getRequestDispatcher(String url)
getRequestDispatcher
in interface ServletRequest
public String getRealPath(String path)
getRealPath
in interface ServletRequest
public int getRemotePort()
getRemotePort
in interface ServletRequest
public String getLocalName()
getLocalName
in interface ServletRequest
public String getLocalAddr()
getLocalAddr
in interface ServletRequest
public int getLocalPort()
getLocalPort
in interface ServletRequest
public String getForwardUrl()
public List<String> getIncludedUrls()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |