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
Constructors -
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.voidAdds aLocaleto 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.booleancontainsHeader(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[]longgetDateHeader(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.intgetIntHeader(String name) Retrieves all the parameters of this request.intgetServerRootUrl(int port) Returns the root URL of the server that is running this web applications.getSession(boolean create) booleanhasAttribute(String name) Checks if a request attribute exists.booleanChecks whether a cookie is present.booleanChecks if a particular file has been uploaded in this request.booleanhasParameter(String name) Checks whether a named parameter is present in this request.Sets a request header with the given name and value.voidinit()Sets a request header with the given name and integer value.booleanisSecure()Adds aLocaleto 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.voidremoveAttribute(String name) voidremoveHeader(String name) Removes a request header with the given name.secure(boolean secure) Set whether this request is secure.voidsetAttribute(String name, Object object) voidSets this request's bodyvoidsetBodyAsBytes(byte[] bytes) Sets this request's body as bytes.voidsetCharacterEncoding(String encoding) Set the character encoding of this request.voidsetContentType(String type) Set the content type of this request.voidsetDateHeader(String name, long value) Sets a request header with the given name and date-value.voidsetFile(String name, MockFileUpload file) Sets a file in this request.voidsetFiles(String name, MockFileUpload[] files) Sets files in this request.voidsetFiles(Map<String, MockFileUpload[]> files) Sets a map of files in this request.voidSets a request header with the given name and value.voidsetIntHeader(String name, int value) Sets a request header with the given name and integer value.voidsetMethod(RequestMethod method) Sets the method of this request.voidsetParameter(String name, Object... values) Sets a parameter in this request.voidsetParameter(String name, String... values) Sets a parameter in this request.voidsetParameters(Map<String, String[]> parameters) Sets a map of parameters in this request.voidsetProtocol(String protocol) Set the protocol of this request.voidsetRemoteAddr(String remoteAddr) Set the remote address of this request.voidsetRemoteHost(String remoteHost) Set the remote host of this request.voidsetRemoteUser(String remoteUser) Set the remote user of this request.voidsetSecure(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
MockRequestinstance - 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:
trueif the parameter is present; orfalseotherwise- Since:
- 1.0
- See Also:
-
getParameters
Retrieves all the parameters of this request.- Specified by:
getParametersin interfaceRequest- Returns:
- a
Mapof 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- aMapof 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- aMapof the parameters that will be set with the names as the keys and their value arrays as the values- Returns:
- this
MockRequestinstance - 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
MockRequestinstance - 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
MockRequestinstance - 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
MockRequestinstance - Since:
- 1.0
- See Also:
-
getBody
Description copied from interface:RequestRetrieves 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
MockRequestinstance - Since:
- 1.0
- See Also:
-
getBodyAsBytes
public byte[] getBodyAsBytes()Description copied from interface:RequestRetrieves the body of this request as a byte array.- Specified by:
getBodyAsBytesin interfaceRequest- Returns:
- the byte array of the request body
- See Also:
-
getFiles
Description copied from interface:RequestRetrieves the files that were uploaded in this request. -
hasFile
Description copied from interface:RequestChecks if a particular file has been uploaded in this request. -
getFile
Description copied from interface:RequestRetrieves an uploaded file. -
getFiles
Description copied from interface:RequestRetrieves 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- aMapof 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
MockRequestinstance - Since:
- 1.0
- See Also:
-
files
Sets a map of files in this request.- Parameters:
files- aMapof the files that will be set with the names as the keys and their file upload specifications as the values- Returns:
- this
MockRequestinstance - 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
MockRequestinstance - Since:
- 1.0
- See Also:
-
getServerRootUrl
Description copied from interface:RequestReturns 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:
getServerRootUrlin interfaceRequest- Returns:
- the server's root url
-
hasCookie
Description copied from interface:RequestChecks whether a cookie is present. -
getCookie
Description copied from interface:RequestRetrieves a cookie. -
getCookies
Description copied from interface:Request- Specified by:
getCookiesin interfaceRequest
-
getAttribute
Description copied from interface:Request- Specified by:
getAttributein interfaceRequest
-
hasAttribute
Description copied from interface:RequestChecks if a request attribute exists.- Specified by:
hasAttributein interfaceRequest- Parameters:
name- aStringspecifying the name of the attributefalseotherwise
-
getAttributeNames
Description copied from interface:Request- Specified by:
getAttributeNamesin interfaceRequest
-
removeAttribute
Description copied from interface:Request- Specified by:
removeAttributein interfaceRequest
-
setAttribute
Description copied from interface:Request- Specified by:
setAttributein interfaceRequest
-
getCharacterEncoding
Description copied from interface:Request- Specified by:
getCharacterEncodingin 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
MockRequestinstance - Since:
- 1.0
-
getContentType
Description copied from interface:Request- Specified by:
getContentTypein 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
MockRequestinstance - Since:
- 1.0
-
getDateHeader
Description copied from interface:Request- Specified by:
getDateHeaderin interfaceRequest
-
getHeader
Description copied from interface:Request -
getHeaderNames
Description copied from interface:Request- Specified by:
getHeaderNamesin interfaceRequest
-
getHeaders
Description copied from interface:Request- Specified by:
getHeadersin interfaceRequest
-
getIntHeader
Description copied from interface:Request- Specified by:
getIntHeaderin 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
MockRequestinstance - 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
MockRequestinstance - 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
MockRequestinstance - Since:
- 1.0
-
containsHeader
Checks whether a certain request header is present.- Parameters:
name- the name of the header to check- Returns:
trueif the header was present; orfalseotherwise- 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:
getLocalesin interfaceRequest
-
addLocale
Adds aLocaleto this request.- Parameters:
locale- the locale to add- Since:
- 1.0
-
locale
Adds aLocaleto this request.- Parameters:
locale- the locale to add- Returns:
- this
MockRequestinstance - Since:
- 1.0
-
getProtocol
Description copied from interface:Request- Specified by:
getProtocolin 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
MockRequestinstance - Since:
- 1.0
-
getRemoteAddr
Description copied from interface:Request- Specified by:
getRemoteAddrin 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
MockRequestinstance - Since:
- 1.0
-
getRemoteUser
Description copied from interface:Request- Specified by:
getRemoteUserin 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
MockRequestinstance - Since:
- 1.0
-
getRemoteHost
Description copied from interface:Request- Specified by:
getRemoteHostin 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
MockRequestinstance - Since:
- 1.0
-
getScheme
Description copied from interface:Request -
getServerName
Description copied from interface:Request- Specified by:
getServerNamein interfaceRequest
-
getServerPort
public int getServerPort()Description copied from interface:Request- Specified by:
getServerPortin interfaceRequest
-
getContextPath
Description copied from interface:Request- Specified by:
getContextPathin 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-trueif this request is secure; orfalseotherwise- Since:
- 1.0
-
secure
Set whether this request is secure.- Parameters:
secure-trueif this request is secure; orfalseotherwise- Returns:
- this
MockRequestinstance - Since:
- 1.0
-
getSession
Description copied from interface:Request- Specified by:
getSessionin interfaceRequest
-
getSession
Description copied from interface:Request- Specified by:
getSessionin interfaceRequest
-
getRequestDispatcher
Description copied from interface:Request- Specified by:
getRequestDispatcherin interfaceRequest
-
getHttpServletRequest
Description copied from interface:RequestRetrieves the underlyingHttpServletRequest.- Specified by:
getHttpServletRequestin interfaceRequest- Returns:
- the underlying
HttpServletRequestinstance; ornullif this request isn't backed byHttpServletRequest
-