Package rife.test
Class MockRequest
java.lang.Object
rife.test.MockRequest
- All Implemented Interfaces:
Request
Provides a
Request
implementation that is suitable for testing a
web application outside a servlet container.- Since:
- 1.0
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionaddDateHeader
(String name, long value) Adds a request header with the given name and date-value.Adds a request header with the given name and value.addIntHeader
(String name, int value) Adds a request header with the given name and integer value.void
Adds aLocale
to this request.Sets this request's bodybodyAsBytes
(byte[] bytes) Sets this request's body as bytes.characterEncoding
(String encoding) Set the character encoding of this request.boolean
containsHeader
(String name) Checks whether a certain request header is present.contentType
(String type) Set the content type of this request.dateHeader
(String name, long value) Sets a request header with the given name and date-value.file
(String name, MockFileUpload file) Sets a file in this request.files
(String name, MockFileUpload[] files) Sets files in this request.files
(Map<String, MockFileUpload[]> files) Sets a map of files in this request.getAttribute
(String name) getBody()
Retrieves the body of this request as a string.byte[]
Retrieves the body of this request as a byte array.Retrieves a cookie.Cookie[]
long
getDateHeader
(String name) Retrieves an uploaded file.getFiles()
Retrieves the files that were uploaded in this request.Retrieves all files that have been uploaded for a particular name.getHeaders
(String name) Retrieves the underlyingHttpServletRequest
.int
getIntHeader
(String name) Retrieves all the parameters of this request.int
getServerRootUrl
(int port) Returns the root URL of the server that is running this web applications.getSession
(boolean create) boolean
hasAttribute
(String name) Checks if a request attribute exists.boolean
Checks whether a cookie is present.boolean
Checks if a particular file has been uploaded in this request.boolean
hasParameter
(String name) Checks whether a named parameter is present in this request.Sets a request header with the given name and value.void
init()
Sets a request header with the given name and integer value.boolean
isSecure()
Adds aLocale
to this request.method
(RequestMethod method) Sets the method of this request.Sets a parameter in this request.Sets a parameter in this request.parameters
(Map<String, String[]> parameters) Sets a map of parameters in this request.Set the protocol of this request.remoteAddr
(String remoteAddr) Set the remote address of this request.remoteHost
(String remoteHost) Set the remote host of this request.remoteUser
(String remoteUser) Set the remote user of this request.void
removeAttribute
(String name) void
removeHeader
(String name) Removes a request header with the given name.secure
(boolean secure) Set whether this request is secure.void
setAttribute
(String name, Object object) void
Sets this request's bodyvoid
setBodyAsBytes
(byte[] bytes) Sets this request's body as bytes.void
setCharacterEncoding
(String encoding) Set the character encoding of this request.void
setContentType
(String type) Set the content type of this request.void
setDateHeader
(String name, long value) Sets a request header with the given name and date-value.void
setFile
(String name, MockFileUpload file) Sets a file in this request.void
setFiles
(String name, MockFileUpload[] files) Sets files in this request.void
setFiles
(Map<String, MockFileUpload[]> files) Sets a map of files in this request.void
Sets a request header with the given name and value.void
setIntHeader
(String name, int value) Sets a request header with the given name and integer value.void
setMethod
(RequestMethod method) Sets the method of this request.void
setParameter
(String name, Object... values) Sets a parameter in this request.void
setParameter
(String name, String... values) Sets a parameter in this request.void
setParameters
(Map<String, String[]> parameters) Sets a map of parameters in this request.void
setProtocol
(String protocol) Set the protocol of this request.void
setRemoteAddr
(String remoteAddr) Set the remote address of this request.void
setRemoteHost
(String remoteHost) Set the remote host of this request.void
setRemoteUser
(String remoteUser) Set the remote user of this request.void
setSecure
(boolean secure) Set whether this request is secure.
-
Constructor Details
-
MockRequest
public MockRequest()
-
-
Method Details
-
init
public void init() -
getMethod
Description copied from interface:Request
-
setMethod
Sets the method of this request.The method defaults to
RequestMethod.GET
.- Parameters:
method
- the method that will be used by this request- Since:
- 1.0
- See Also:
-
method
Sets the method of this request.- Parameters:
method
- the method that will be used by this request- Returns:
- this
MockRequest
instance - Since:
- 1.0
- See Also:
-
hasParameter
Checks whether a named parameter is present in this request.- Parameters:
name
- the name of the parameter to check- Returns:
true
if the parameter is present; orfalse
otherwise- Since:
- 1.0
- See Also:
-
getParameters
Retrieves all the parameters of this request.- Specified by:
getParameters
in interfaceRequest
- Returns:
- a
Map
of the parameters with the names as the keys and their value arrays as the values - Since:
- 1.0
- See Also:
-
setParameters
Sets a map of parameters in this request.- Parameters:
parameters
- aMap
of the parameters that will be set with the names as the keys and their value arrays as the values- Since:
- 1.0
- See Also:
-
parameters
Sets a map of parameters in this request.- Parameters:
parameters
- aMap
of the parameters that will be set with the names as the keys and their value arrays as the values- Returns:
- this
MockRequest
instance - Since:
- 1.0
- See Also:
-
setParameter
Sets a parameter in this request.- Parameters:
name
- the name of the parametervalues
- the value array of the parameter- Since:
- 1.0
- See Also:
-
setParameter
Sets a parameter in this request.- Parameters:
name
- the name of the parametervalues
- the value array of the parameter- Since:
- 1.0
- See Also:
-
parameter
Sets a parameter in this request.- Parameters:
name
- the name of the parametervalues
- the value array of the parameter- Returns:
- this
MockRequest
instance - Since:
- 1.0
- See Also:
-
parameter
Sets a parameter in this request.- Parameters:
name
- the name of the parametervalues
- the value array of the parameter- Returns:
- this
MockRequest
instance - Since:
- 1.0
- See Also:
-
setBody
Sets this request's body- Parameters:
body
- the body as a string- Since:
- 1.0
- See Also:
-
body
Sets this request's body- Parameters:
body
- the body as a string- Returns:
- this
MockRequest
instance - Since:
- 1.0
- See Also:
-
getBody
Description copied from interface:Request
Retrieves the body of this request as a string. -
setBodyAsBytes
public void setBodyAsBytes(byte[] bytes) Sets this request's body as bytes.- Parameters:
bytes
- the body as bytes- Since:
- 1.0
- See Also:
-
bodyAsBytes
Sets this request's body as bytes.- Parameters:
bytes
- the body as bytes- Returns:
- this
MockRequest
instance - Since:
- 1.0
- See Also:
-
getBodyAsBytes
public byte[] getBodyAsBytes()Description copied from interface:Request
Retrieves the body of this request as a byte array.- Specified by:
getBodyAsBytes
in interfaceRequest
- Returns:
- the byte array of the request body
- See Also:
-
getFiles
Description copied from interface:Request
Retrieves the files that were uploaded in this request. -
hasFile
Description copied from interface:Request
Checks if a particular file has been uploaded in this request. -
getFile
Description copied from interface:Request
Retrieves an uploaded file. -
getFiles
Description copied from interface:Request
Retrieves all files that have been uploaded for a particular name. -
setFile
Sets a file in this request.- Parameters:
name
- the parameter name of the filefile
- the file specification that will be uploaded- Since:
- 1.0
- See Also:
-
setFiles
Sets a map of files in this request.- Parameters:
files
- aMap
of the files that will be set with the names as the keys and their file upload specifications as the values- Since:
- 1.0
- See Also:
-
setFiles
Sets files in this request.- Parameters:
name
- the parameter name of the filefiles
- the file specifications that will be uploaded- Since:
- 1.0
- See Also:
-
file
Sets a file in this request.- Parameters:
name
- the parameter name of the filefile
- the file specification that will be uploaded- Returns:
- this
MockRequest
instance - Since:
- 1.0
- See Also:
-
files
Sets a map of files in this request.- Parameters:
files
- aMap
of the files that will be set with the names as the keys and their file upload specifications as the values- Returns:
- this
MockRequest
instance - Since:
- 1.0
- See Also:
-
files
Sets files in this request.- Parameters:
name
- the parameter name of the filefiles
- the file specifications that will be uploaded- Returns:
- this
MockRequest
instance - Since:
- 1.0
- See Also:
-
getServerRootUrl
Description copied from interface:Request
Returns the root URL of the server that is running this web applications.This includes the protocol, the server name and the server port, for example:
http://www.somehost.com:8080
.- Specified by:
getServerRootUrl
in interfaceRequest
- Returns:
- the server's root url
-
hasCookie
Description copied from interface:Request
Checks whether a cookie is present. -
getCookie
Description copied from interface:Request
Retrieves a cookie. -
getCookies
Description copied from interface:Request
- Specified by:
getCookies
in interfaceRequest
-
getAttribute
Description copied from interface:Request
- Specified by:
getAttribute
in interfaceRequest
-
hasAttribute
Description copied from interface:Request
Checks if a request attribute exists.- Specified by:
hasAttribute
in interfaceRequest
- Parameters:
name
- aString
specifying the name of the attributefalse
otherwise
-
getAttributeNames
Description copied from interface:Request
- Specified by:
getAttributeNames
in interfaceRequest
-
removeAttribute
Description copied from interface:Request
- Specified by:
removeAttribute
in interfaceRequest
-
setAttribute
Description copied from interface:Request
- Specified by:
setAttribute
in interfaceRequest
-
getCharacterEncoding
Description copied from interface:Request
- Specified by:
getCharacterEncoding
in interfaceRequest
-
setCharacterEncoding
Set the character encoding of this request.- Parameters:
encoding
- the name of the character encoding- Since:
- 1.0
-
characterEncoding
Set the character encoding of this request.- Parameters:
encoding
- the name of the character encoding- Returns:
- this
MockRequest
instance - Since:
- 1.0
-
getContentType
Description copied from interface:Request
- Specified by:
getContentType
in interfaceRequest
-
setContentType
Set the content type of this request.- Parameters:
type
- the content type- Since:
- 1.0
-
contentType
Set the content type of this request.- Parameters:
type
- the content type- Returns:
- this
MockRequest
instance - Since:
- 1.0
-
getDateHeader
Description copied from interface:Request
- Specified by:
getDateHeader
in interfaceRequest
-
getHeader
Description copied from interface:Request
-
getHeaderNames
Description copied from interface:Request
- Specified by:
getHeaderNames
in interfaceRequest
-
getHeaders
Description copied from interface:Request
- Specified by:
getHeaders
in interfaceRequest
-
getIntHeader
Description copied from interface:Request
- Specified by:
getIntHeader
in interfaceRequest
-
addHeader
Adds a request header with the given name and value. This method allows request headers to have multiple values.- Parameters:
name
- the name of the header to setvalue
- the additional header value- Returns:
- this
MockRequest
instance - Since:
- 1.0
-
addDateHeader
Adds a request header with the given name and date-value. The date is specified in terms of milliseconds since the epoch. This method allows request headers to have multiple values.- Parameters:
name
- the name of the header to setvalue
- the additional date value- Returns:
- this
MockRequest
instance - Since:
- 1.0
-
addIntHeader
Adds a request header with the given name and integer value. This method allows request headers to have multiple values.- Parameters:
name
- the name of the header to setvalue
- the additional integer value- Returns:
- this
MockRequest
instance - Since:
- 1.0
-
containsHeader
Checks whether a certain request header is present.- Parameters:
name
- the name of the header to check- Returns:
true
if the header was present; orfalse
otherwise- Since:
- 1.0
-
setDateHeader
Sets a request header with the given name and date-value. The date is specified in terms of milliseconds since the epoch. If the header had already been set, the new value overwrites the previous one. ThecontainsHeader(java.lang.String)
method can be used to test for the presence of a header before setting its value.- Parameters:
name
- the name of the header to setvalue
- the assigned date value- Since:
- 1.0
-
dateHeader
Sets a request header with the given name and date-value.- Parameters:
name
- the name of the header to setvalue
- the assigned date value- Since:
- 1.0
- See Also:
-
setHeader
Sets a request header with the given name and value. If the header had already been set, the new value overwrites the previous one. ThecontainsHeader(java.lang.String)
method can be used to test for the presence of a header before setting its value.- Parameters:
name
- the name of the header to setvalue
- the header value- Since:
- 1.0
-
header
Sets a request header with the given name and value.- Parameters:
name
- the name of the header to setvalue
- the header value- Since:
- 1.0
- See Also:
-
setIntHeader
Sets a request header with the given name and integer value. If the header had already been set, the new value overwrites the previous one. The containsHeader method can be used to test for the presence of a header before setting its value.- Parameters:
name
- the name of the header to setvalue
- the assigned integer value- Since:
- 1.0
-
intHeader
Sets a request header with the given name and integer value.- Parameters:
name
- the name of the header to setvalue
- the assigned integer value- Since:
- 1.0
- See Also:
-
removeHeader
Removes a request header with the given name.- Parameters:
name
- the name of the header to remove- Since:
- 1.0
-
getLocale
Description copied from interface:Request
-
getLocales
Description copied from interface:Request
- Specified by:
getLocales
in interfaceRequest
-
addLocale
Adds aLocale
to this request.- Parameters:
locale
- the locale to add- Since:
- 1.0
-
locale
Adds aLocale
to this request.- Parameters:
locale
- the locale to add- Returns:
- this
MockRequest
instance - Since:
- 1.0
-
getProtocol
Description copied from interface:Request
- Specified by:
getProtocol
in interfaceRequest
-
setProtocol
Set the protocol of this request.The default protocol is
"HTTP/1.1"
.- Parameters:
protocol
- the protocol to set- Since:
- 1.0
-
protocol
Set the protocol of this request.- Parameters:
protocol
- the protocol to set- Returns:
- this
MockRequest
instance - Since:
- 1.0
-
getRemoteAddr
Description copied from interface:Request
- Specified by:
getRemoteAddr
in interfaceRequest
-
setRemoteAddr
Set the remote address of this request.The default remote address is "
127.0.0.1"
.- Parameters:
remoteAddr
- the remote address to set- Since:
- 1.0
-
remoteAddr
Set the remote address of this request.- Parameters:
remoteAddr
- the remote address to set- Returns:
- this
MockRequest
instance - Since:
- 1.0
-
getRemoteUser
Description copied from interface:Request
- Specified by:
getRemoteUser
in interfaceRequest
-
setRemoteUser
Set the remote user of this request.The default remote user is
null
.- Parameters:
remoteUser
- the remote user to set- Since:
- 1.0
-
remoteUser
Set the remote user of this request.- Parameters:
remoteUser
- the remote user to set- Returns:
- this
MockRequest
instance - Since:
- 1.0
-
getRemoteHost
Description copied from interface:Request
- Specified by:
getRemoteHost
in interfaceRequest
-
setRemoteHost
Set the remote host of this request.The default remote host is "
localhost
".- Parameters:
remoteHost
- the remote host to set- Since:
- 1.0
-
remoteHost
Set the remote host of this request.- Parameters:
remoteHost
- the remote host to set- Returns:
- this
MockRequest
instance - Since:
- 1.0
-
getScheme
Description copied from interface:Request
-
getServerName
Description copied from interface:Request
- Specified by:
getServerName
in interfaceRequest
-
getServerPort
public int getServerPort()Description copied from interface:Request
- Specified by:
getServerPort
in interfaceRequest
-
getContextPath
Description copied from interface:Request
- Specified by:
getContextPath
in interfaceRequest
-
isSecure
public boolean isSecure()Description copied from interface:Request
-
setSecure
public void setSecure(boolean secure) Set whether this request is secure.A request is not secure by default.
- Parameters:
secure
-true
if this request is secure; orfalse
otherwise- Since:
- 1.0
-
secure
Set whether this request is secure.- Parameters:
secure
-true
if this request is secure; orfalse
otherwise- Returns:
- this
MockRequest
instance - Since:
- 1.0
-
getSession
Description copied from interface:Request
- Specified by:
getSession
in interfaceRequest
-
getSession
Description copied from interface:Request
- Specified by:
getSession
in interfaceRequest
-
getRequestDispatcher
Description copied from interface:Request
- Specified by:
getRequestDispatcher
in interfaceRequest
-
getHttpServletRequest
Description copied from interface:Request
Retrieves the underlyingHttpServletRequest
.- Specified by:
getHttpServletRequest
in interfaceRequest
- Returns:
- the underlying
HttpServletRequest
instance; ornull
if this request isn't backed byHttpServletRequest
-