Package rife.test
Class MockRequest
java.lang.Object
rife.engine.SiteRequest
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
ConstructorsConstructorDescriptionCreates a newGETrequest that isn't secure unlessSiteRequest.setSecure(boolean)is used. -
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.Sets this request's bodybodyAsBytes(byte[] bytes) Sets this request's body as bytes.bodyAsStream(InputStream stream) Sets the body of this request as a stream.characterEncoding(String encoding) Set the character encoding of this request.contentType(String type) Set the content type of this request.protected SiteResponseCreates the response 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.Retrieves a cookie.Cookie[]intgetServerRootUrl(int port) Returns the root URL of the server that is running this web applications.booleanChecks whether a cookie is present.booleanhasParameter(String name) Checks whether a named parameter is present in this request.Sets a request header with the given name and value.htmx()Marks this request as coming from htmx by setting theHX-Requestheader that htmx sends along with every request.voidinit()Sets a request header with the given name and integer value.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.protected voidparseParameters(String queryString) Parses the parameters of this request from the query string and from the body.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.secure(boolean secure) Set whether this request is secure.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.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.Methods inherited from class rife.engine.SiteRequest
addLocale, cancel, containsHeader, getAttribute, getAttributeNames, getBody, getBodyAsBytes, getCharacterEncoding, getContentType, getDateHeader, getFile, getFiles, getFiles, getHeader, getHeaderNames, getHeaders, getHttpServletRequest, getIntHeader, getLocale, getLocales, getMethod, getParameters, getProtocol, getRemoteAddr, getRemoteHost, getRemoteUser, getRequestDispatcher, getSession, getSession, hasAttribute, hasFile, isCancelled, isSecure, removeAttribute, removeHeader, setAttribute, setBody, setBodyAsBytes, setBodyAsStream, setCharacterEncoding, setContentType, setDateHeader, setHeader, setIntHeader, setMethod, setProtocol, setRemoteAddr, setRemoteHost, setRemoteUser, setSecure, setUploadedFiles
-
Constructor Details
-
MockRequest
public MockRequest()Creates a newGETrequest that isn't secure unlessSiteRequest.setSecure(boolean)is used.- Since:
- 1.0
-
-
Method Details
-
init
public void init() -
createResponse
Description copied from class:SiteRequestCreates the response of this request. Extending classes can override this to provide their own type of response.- Overrides:
createResponsein classSiteRequest- Returns:
- a new response
-
parseParameters
Description copied from class:SiteRequestParses the parameters of this request from the query string and from the body.The values of the query string come first. Urlencoded bodies are read for
POST,PUTandPATCH, while multipart bodies are read for any method. A request with a malformed escape, or with a form beyond the limits of the runner, is answered with400.- Overrides:
parseParametersin classSiteRequest- Parameters:
queryString- the raw query string, ornullwhen there's none
-
method
Sets the method of this request.- Overrides:
methodin classSiteRequest- 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:
-
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:
-
body
Sets this request's body- Overrides:
bodyin classSiteRequest- Parameters:
body- the body as a string- Returns:
- this
MockRequestinstance - Since:
- 1.0
- See Also:
-
bodyAsBytes
Sets this request's body as bytes.- Overrides:
bodyAsBytesin classSiteRequest- Parameters:
bytes- the body as bytes- Returns:
- this
MockRequestinstance - Since:
- 1.0
- See Also:
-
bodyAsStream
Sets the body of this request as a stream.- Overrides:
bodyAsStreamin classSiteRequest- Parameters:
stream- the stream with the body- Returns:
- this
MockRequestinstance - Since:
- 1.11.0
- See Also:
-
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- Overrides:
getServerRootUrlin classSiteRequest- Returns:
- the server's root url
-
hasCookie
Description copied from interface:RequestChecks whether a cookie is present.- Specified by:
hasCookiein interfaceRequest- Overrides:
hasCookiein classSiteRequest- Parameters:
name- the name of the cookie- Returns:
trueif the cookie was present; orfalseotherwise- See Also:
-
getCookie
Description copied from interface:RequestRetrieves a cookie.- Specified by:
getCookiein interfaceRequest- Overrides:
getCookiein classSiteRequest- Parameters:
name- the name of the cookie.- Returns:
- the instance of the cookie; or
nullif no such cookie is present - See Also:
-
getCookies
Description copied from interface:Request- Specified by:
getCookiesin interfaceRequest- Overrides:
getCookiesin classSiteRequest
-
characterEncoding
Set the character encoding of this request.- Overrides:
characterEncodingin classSiteRequest- Parameters:
encoding- the name of the character encoding- Returns:
- this
MockRequestinstance - Since:
- 1.0
- See Also:
-
contentType
Set the content type of this request.- Overrides:
contentTypein classSiteRequest- Parameters:
type- the content type- Returns:
- this
MockRequestinstance - Since:
- 1.0
- See Also:
-
addHeader
Adds a request header with the given name and value. This method allows request headers to have multiple values.- Overrides:
addHeaderin classSiteRequest- 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.- Overrides:
addDateHeaderin classSiteRequest- 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.- Overrides:
addIntHeaderin classSiteRequest- Parameters:
name- the name of the header to setvalue- the additional integer value- Returns:
- this
MockRequestinstance - Since:
- 1.0
-
dateHeader
Sets a request header with the given name and date-value.- Overrides:
dateHeaderin classSiteRequest- Parameters:
name- the name of the header to setvalue- the assigned date value- Returns:
- this
SiteRequestinstance - Since:
- 1.0
- See Also:
-
header
Sets a request header with the given name and value.- Overrides:
headerin classSiteRequest- Parameters:
name- the name of the header to setvalue- the header value- Returns:
- this
SiteRequestinstance - Since:
- 1.0
- See Also:
-
htmx
Marks this request as coming from htmx by setting theHX-Requestheader that htmx sends along with every request.This makes it easy to test the htmx side of an element that serves both a full page and a fragment, so that a single test can check both:
var full = m.doRequest("/books"); var fragment = m.doRequest("/books", new MockRequest().htmx());You can add the other
HX-*request headers by chainingheader(java.lang.String, java.lang.String), for instancenew MockRequest().htmx().header("HX-Target", "list").- Returns:
- this
MockRequestinstance, so that method calls can be chained - Since:
- 1.10
- See Also:
-
intHeader
Sets a request header with the given name and integer value.- Overrides:
intHeaderin classSiteRequest- Parameters:
name- the name of the header to setvalue- the assigned integer value- Returns:
- this
SiteRequestinstance - Since:
- 1.0
- See Also:
-
locale
Adds aLocaleto this request.- Overrides:
localein classSiteRequest- Parameters:
locale- the locale to add- Returns:
- this
MockRequestinstance - Since:
- 1.0
- See Also:
-
protocol
Set the protocol of this request.- Overrides:
protocolin classSiteRequest- Parameters:
protocol- the protocol to set- Returns:
- this
MockRequestinstance - Since:
- 1.0
- See Also:
-
remoteAddr
Set the remote address of this request.- Overrides:
remoteAddrin classSiteRequest- Parameters:
remoteAddr- the remote address to set- Returns:
- this
MockRequestinstance - Since:
- 1.0
- See Also:
-
remoteUser
Set the remote user of this request.- Overrides:
remoteUserin classSiteRequest- Parameters:
remoteUser- the remote user to set- Returns:
- this
MockRequestinstance - Since:
- 1.0
- See Also:
-
remoteHost
Set the remote host of this request.- Overrides:
remoteHostin classSiteRequest- Parameters:
remoteHost- the remote host to set- Returns:
- this
MockRequestinstance - Since:
- 1.0
- See Also:
-
getScheme
Description copied from interface:Request- Specified by:
getSchemein interfaceRequest- Overrides:
getSchemein classSiteRequest
-
getServerName
Description copied from interface:Request- Specified by:
getServerNamein interfaceRequest- Overrides:
getServerNamein classSiteRequest
-
getServerPort
public int getServerPort()Description copied from interface:Request- Specified by:
getServerPortin interfaceRequest- Overrides:
getServerPortin classSiteRequest
-
getContextPath
Description copied from interface:Request- Specified by:
getContextPathin interfaceRequest- Overrides:
getContextPathin classSiteRequest
-
secure
Set whether this request is secure.- Overrides:
securein classSiteRequest- Parameters:
secure-trueif this request is secure; orfalseotherwise- Returns:
- this
MockRequestinstance - Since:
- 1.0
- See Also:
-