net.sourceforge.stripes.mock
Class MockHttpServletRequest

java.lang.Object
  extended by net.sourceforge.stripes.mock.MockHttpServletRequest
All Implemented Interfaces:
HttpServletRequest, ServletRequest

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.

Since:
Stripes 1.1.1
Author:
Tim Fennell

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

MockHttpServletRequest

public MockHttpServletRequest(String contextPath,
                              String servletPath)
Minimal constructor that makes sense. Requires a context path (should be the same as the name of the servlet context, prepended with a '/') and a servlet path. E.g. new MockHttpServletRequest("/myapp", "/actionType/foo.action").

Parameters:
contextPath -
servletPath -
Method Detail

setAuthType

public void setAuthType(String authType)
Sets the auth type that will be reported by this request.


getAuthType

public String getAuthType()
Gets the auth type being used by this request.

Specified by:
getAuthType in interface HttpServletRequest

setCookies

public void setCookies(Cookie[] cookies)
Sets the array of cookies that will be available from the request.


getCookies

public Cookie[] getCookies()
Returns any cookies that are set on the request.

Specified by:
getCookies in interface HttpServletRequest

addHeader

public void addHeader(String name,
                      Object value)
Allows headers to be set on the request. These will be returned by the various getXxHeader() methods. If the header is a date header it should be set with a Long. If the header is an Int header it should be set with an Integer.


getDateHeader

public long getDateHeader(String name)
Gets the named header as a long. Must have been set as a long with addHeader().

Specified by:
getDateHeader in interface HttpServletRequest

getHeader

public String getHeader(String name)
Returns any header as a String if it exists.

Specified by:
getHeader in interface HttpServletRequest

getHeaders

public Enumeration<String> getHeaders(String name)
Returns an enumeration with single value of the named header, or an empty enum if no value.

Specified by:
getHeaders in interface HttpServletRequest

getHeaderNames

public Enumeration<String> getHeaderNames()
Returns an enumeration containing all the names of headers supplied.

Specified by:
getHeaderNames in interface HttpServletRequest

getIntHeader

public int getIntHeader(String name)
Gets the named header as an int. Must have been set as an Integer with addHeader().

Specified by:
getIntHeader in interface HttpServletRequest

setMethod

public void setMethod(String method)
Sets the method used by the request. Defaults to POST.


getMethod

public String getMethod()
Gets the method used by the request. Defaults to POST.

Specified by:
getMethod in interface HttpServletRequest

setPathInfo

public void setPathInfo(String pathInfo)
Sets the path info. Defaults to the empty string.


getPathInfo

public String getPathInfo()
Returns the path info. Defaults to the empty string.

Specified by:
getPathInfo in interface HttpServletRequest

getPathTranslated

public String getPathTranslated()
Always returns the same as getPathInfo().

Specified by:
getPathTranslated in interface HttpServletRequest

setContextPath

public void setContextPath(String contextPath)
Sets the context path. Defaults to the empty string.


getContextPath

public String getContextPath()
Returns the context path. Defaults to the empty string.

Specified by:
getContextPath in interface HttpServletRequest

setQueryString

public void setQueryString(String queryString)
Sets the query string set on the request; this value is not parsed for anything.


getQueryString

public String getQueryString()
Returns the query string set on the request.

Specified by:
getQueryString in interface HttpServletRequest

getRemoteUser

public String getRemoteUser()
Returns the name from the user principal if one exists, otherwise null.

Specified by:
getRemoteUser in interface HttpServletRequest

setRoles

public void setRoles(Set<String> roles)
Sets the set of roles that the user is deemed to be in for the request.


isUserInRole

public boolean isUserInRole(String role)
Returns true if the set of roles contains the role specified, false otherwise.

Specified by:
isUserInRole in interface HttpServletRequest

setUserPrincipal

public void setUserPrincipal(Principal userPrincipal)
Sets the Principal for the current request.


getUserPrincipal

public Principal getUserPrincipal()
Returns the Principal if one is set on the request.

Specified by:
getUserPrincipal in interface HttpServletRequest

getRequestedSessionId

public String getRequestedSessionId()
Returns the ID of the session if one is attached to this request. Otherwise null.

Specified by:
getRequestedSessionId in interface HttpServletRequest

getRequestURI

public String getRequestURI()
Returns the request URI as defined by the servlet spec.

Specified by:
getRequestURI in interface HttpServletRequest

getRequestURL

public StringBuffer getRequestURL()
Returns (an attempt at) a reconstructed URL based on it's constituent parts.

Specified by:
getRequestURL in interface HttpServletRequest

getServletPath

public String getServletPath()
Gets the part of the path which matched the servlet.

Specified by:
getServletPath in interface HttpServletRequest

getSession

public HttpSession getSession(boolean b)
Gets the session object attached to this request.

Specified by:
getSession in interface HttpServletRequest

getSession

public HttpSession getSession()
Gets the session object attached to this request.

Specified by:
getSession in interface HttpServletRequest

setSession

public void setSession(HttpSession session)
Allows a session to be associated with the request.


isRequestedSessionIdValid

public boolean isRequestedSessionIdValid()
Always returns true.

Specified by:
isRequestedSessionIdValid in interface HttpServletRequest

isRequestedSessionIdFromCookie

public boolean isRequestedSessionIdFromCookie()
Always returns true.

Specified by:
isRequestedSessionIdFromCookie in interface HttpServletRequest

isRequestedSessionIdFromURL

public boolean isRequestedSessionIdFromURL()
Always returns false.

Specified by:
isRequestedSessionIdFromURL in interface HttpServletRequest

isRequestedSessionIdFromUrl

public boolean isRequestedSessionIdFromUrl()
Always returns false.

Specified by:
isRequestedSessionIdFromUrl in interface HttpServletRequest

getAttribute

public Object getAttribute(String key)
Gets the named request attribute from an internal Map.

Specified by:
getAttribute in interface ServletRequest

getAttributeNames

public Enumeration<String> getAttributeNames()
Gets an enumeration of all request attribute names.

Specified by:
getAttributeNames in interface ServletRequest

getCharacterEncoding

public String getCharacterEncoding()
Gets the character encoding, defaults to UTF-8.

Specified by:
getCharacterEncoding in interface ServletRequest

setCharacterEncoding

public void setCharacterEncoding(String encoding)
Sets the character encoding that will be returned by getCharacterEncoding().

Specified by:
setCharacterEncoding in interface ServletRequest

getContentLength

public int getContentLength()
Always returns -1 (unknown).

Specified by:
getContentLength in interface ServletRequest

getContentType

public String getContentType()
Always returns null.

Specified by:
getContentType in interface ServletRequest

getInputStream

public ServletInputStream getInputStream()
                                  throws IOException
Always returns null.

Specified by:
getInputStream in interface ServletRequest
Throws:
IOException

getParameter

public String getParameter(String name)
Gets the first value of the named parameter or null if a value does not exist.

Specified by:
getParameter in interface ServletRequest

getParameterNames

public Enumeration<String> getParameterNames()
Gets an enumeration containing all the parameter names present.

Specified by:
getParameterNames in interface ServletRequest

getParameterValues

public String[] getParameterValues(String name)
Returns an array of all values for a parameter, or null if the parameter does not exist.

Specified by:
getParameterValues in interface ServletRequest

getParameterMap

public Map<String,String[]> getParameterMap()
Provides access to the parameter map. Note that this returns a reference to the live, modifiable parameter map. As a result it can be used to insert parameters when constructing the request.

Specified by:
getParameterMap in interface ServletRequest

setProtocol

public void setProtocol(String protocol)
Sets the protocol for the request. Defaults to "https".


getProtocol

public String getProtocol()
Gets the protocol for the request. Defaults to "https".

Specified by:
getProtocol in interface ServletRequest

getScheme

public String getScheme()
Always returns the same as getProtocol.

Specified by:
getScheme in interface ServletRequest

setServerName

public void setServerName(String serverName)
Sets the server name. Defaults to "localhost".


getServerName

public String getServerName()
Gets the server name. Defaults to "localhost".

Specified by:
getServerName in interface ServletRequest

setServerPort

public void setServerPort(int serverPort)
Sets the server port. Defaults to 8080.


getServerPort

public int getServerPort()
Returns the server port. Defaults to 8080.

Specified by:
getServerPort in interface ServletRequest

getReader

public BufferedReader getReader()
                         throws IOException
Always returns null.

Specified by:
getReader in interface ServletRequest
Throws:
IOException

getRemoteAddr

public String getRemoteAddr()
Aways returns "127.0.0.1".

Specified by:
getRemoteAddr in interface ServletRequest

getRemoteHost

public String getRemoteHost()
Always returns "localhost".

Specified by:
getRemoteHost in interface ServletRequest

setAttribute

public void setAttribute(String name,
                         Object value)
Sets the supplied value for the named request attribute.

Specified by:
setAttribute in interface ServletRequest

removeAttribute

public void removeAttribute(String name)
Removes any value for the named request attribute.

Specified by:
removeAttribute in interface ServletRequest

addLocale

public void addLocale(Locale locale)
Adds a Locale to the set of requested locales.


getLocale

public Locale getLocale()
Returns the preferred locale. Defaults to the system locale.

Specified by:
getLocale in interface ServletRequest

getLocales

public Enumeration<Locale> getLocales()
Returns an enumeration of requested locales. Defaults to the system locale.

Specified by:
getLocales in interface ServletRequest

isSecure

public boolean isSecure()
Returns true if the protocol is set to https (default), false otherwise.

Specified by:
isSecure in interface ServletRequest

getRequestDispatcher

public MockRequestDispatcher getRequestDispatcher(String url)
Returns an instance of MockRequestDispatcher that just records what URLs are forwarded to or included. The results can be examined later by calling getForwardUrl() and getIncludedUrls().

Specified by:
getRequestDispatcher in interface ServletRequest

getRealPath

public String getRealPath(String path)
Always returns the path passed in without any alteration.

Specified by:
getRealPath in interface ServletRequest

getRemotePort

public int getRemotePort()
Always returns 1088 (and yes, that was picked arbitrarily).

Specified by:
getRemotePort in interface ServletRequest

getLocalName

public String getLocalName()
Always returns the same value as getServerName().

Specified by:
getLocalName in interface ServletRequest

getLocalAddr

public String getLocalAddr()
Always returns 127.0.0.1).

Specified by:
getLocalAddr in interface ServletRequest

getLocalPort

public int getLocalPort()
Always returns the same value as getServerPort().

Specified by:
getLocalPort in interface ServletRequest

getForwardUrl

public String getForwardUrl()
Gets the URL that was forwarded to, if a forward was processed. Null otherwise.


getIncludedUrls

public List<String> getIncludedUrls()
Gets the list (potentially empty) or URLs that were included during the request.



? Copyright 2005-2006, Stripes Development Team.