Interface GroovyRequestFixture
-
- All Superinterfaces:
RequestFixture
public interface GroovyRequestFixture extends RequestFixture
A more Groovy friendly version ofRequestFixture.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description GroovyRequestFixturebody(byte[] bytes, String contentType)Sets the request body to be the given bytes, and adds aContent-Typerequest header of the given value.GroovyRequestFixturebody(String text, String contentType)Sets the request body to be the given string in utf8 bytes, and adds aContent-Typerequest header of the given value.MultipartFileSpecfile()A specification of a file to upload (see RFC2388).RequestFixturefile(String field, String filename, String data)Uploads a file via a multipart form (see RFC2388).MultipartFormSpecform()A specification of a multipart form (see RFC2388).GroovyRequestFixtureform(Map<String,String> fields)Sets the fields on a multipart form (see RFC2388).static HandlingResulthandle(Handler handler, Closure<?> closure)Unit test aHandler.static HandlingResulthandle(Action<? super Chain> handlers, Closure<?> closure)Unit test a chain ofhandlers.GroovyRequestFixtureheader(CharSequence name, String value)Set a request header value.GroovyRequestFixturelocalAddress(com.google.common.net.HostAndPort local)Set the local address to which this request is made.GroovyRequestFixturemethod(String method)Set the request method (case insensitive).GroovyRequestFixturepathBinding(String boundTo, String pastBinding, Map<String,String> pathTokens)Adds a path binding, with the given path tokens and parts.GroovyRequestFixturepathBinding(String boundTo, String pastBinding, Map<String,String> pathTokens, String description)Adds a path binding, with the given path tokens and parts.GroovyRequestFixturepathBinding(Map<String,String> pathTokens)Adds a path binding, with the given path tokens.GroovyRequestFixtureregistry(Closure<?> closure)A closure friendly overload ofregistry(Action).GroovyRequestFixtureregistry(Action<? super RegistrySpec> action)Configures the context registry.GroovyRequestFixtureremoteAddress(com.google.common.net.HostAndPort remote)Set the remote address from which the request is made.static GroovyRequestFixturerequestFixture()Create a Groovy request fixture, for unit testing aHandler.static GroovyRequestFixturerequestFixture(RequestFixture requestFixture)Create a Groovy request fixture, for unit testing aHandler, by wrapping the givenRequestFixture.GroovyRequestFixtureresponseHeader(CharSequence name, String value)Set a response header value.GroovyRequestFixtureserverConfig(Action<? super ServerConfigBuilder> action)Configures the server config to have no base dir and given configuration.GroovyRequestFixturetimeout(int timeoutSeconds)Sets the maximum time to allow the handler under test to produce a result.GroovyRequestFixtureuri(String uri)The URI of the request.-
Methods inherited from interface ratpack.test.handling.RequestFixture
getRegistry, handle, handleChain, protocol
-
-
-
-
Method Detail
-
handle
static HandlingResult handle(Handler handler, @DelegatesTo(GroovyRequestFixture.class) Closure<?> closure) throws Exception
Unit test aHandler.Example:
import ratpack.groovy.handling.GroovyHandler import ratpack.groovy.handling.GroovyContext import ratpack.groovy.test.handling.GroovyRequestFixture class MyHandler extends GroovyHandler { void handle(GroovyContext context) { context.with { def outputHeaderValue = request.headers.get("input-value") + ":bar" response.headers.set("output-value", outputHeaderValue) render "received: " + request.path } } } def result = GroovyRequestFixture.handle(new MyHandler()) { header "input-value", "foo" uri "some/path" } assert result.rendered(String) == "received: some/path" assert result.headers.get("output-value") == "foo:bar"- Parameters:
handler- the handler to testclosure- the configuration of the request fixture- Returns:
- The result of the invocation
- Throws:
HandlerTimeoutException- if the handler takes more thanRequestFixture.timeout(int)seconds to send a response or callnext()on the contextException- any thrown byclosure
-
handle
static HandlingResult handle(Action<? super Chain> handlers, @DelegatesTo(GroovyRequestFixture.class) Closure<?> closure) throws Exception
Unit test a chain ofhandlers.Example:
import ratpack.groovy.test.handling.GroovyRequestFixture import ratpack.groovy.Groovy def handlers = Groovy.chain { all { def outputHeaderValue = request.headers.get("input-value") + ":bar" response.headers.set("output-value", outputHeaderValue) next() } all { render "received: " + request.path } } def result = GroovyRequestFixture.handle(handlers) { header "input-value", "foo" uri "some/path" } assert result.rendered(String) == "received: some/path" assert result.headers.get("output-value") == "foo:bar"- Parameters:
handlers- the handlers to testclosure- the configuration of the request fixture- Returns:
- The result of the invocation
- Throws:
HandlerTimeoutException- if the handler takes more thanRequestFixture.timeout(int)seconds to send a response or callnext()on the contextException- any thrown byclosure
-
requestFixture
static GroovyRequestFixture requestFixture()
Create a Groovy request fixture, for unit testing aHandler.- Returns:
- a Groovy request fixture
- See Also:
RequestFixture.handle(Handler, Action),RequestFixture.handle(Action, Action)
-
requestFixture
static GroovyRequestFixture requestFixture(RequestFixture requestFixture)
Create a Groovy request fixture, for unit testing aHandler, by wrapping the givenRequestFixture.- Parameters:
requestFixture- The request fixture to wrap- Returns:
- a Groovy request fixture
-
registry
GroovyRequestFixture registry(@DelegatesTo(value=RegistryBuilder.class,strategy=1) Closure<?> closure)
A closure friendly overload ofregistry(Action).- Parameters:
closure- the registry configuration- Returns:
- this
- See Also:
registry(Action)
-
header
GroovyRequestFixture header(CharSequence name, String value)
Set a request header value.By default there are no request headers.
- Specified by:
headerin interfaceRequestFixture- Parameters:
name- the header namevalue- the header value- Returns:
- this
-
body
GroovyRequestFixture body(byte[] bytes, String contentType)
Sets the request body to be the given bytes, and adds aContent-Typerequest header of the given value.By default the body is empty.
- Specified by:
bodyin interfaceRequestFixture- Parameters:
bytes- the request body in bytescontentType- the content type of the request body- Returns:
- this
-
body
GroovyRequestFixture body(String text, String contentType)
Sets the request body to be the given string in utf8 bytes, and adds aContent-Typerequest header of the given value.By default the body is empty.
- Specified by:
bodyin interfaceRequestFixture- Parameters:
text- the request body as a stringcontentType- the content type of the request body- Returns:
- this
-
file
MultipartFileSpec file()
A specification of a file to upload (see RFC2388).Can be used to construct a multipart form with files
- Specified by:
filein interfaceRequestFixture- Returns:
- a specification of a multipart file
-
file
RequestFixture file(String field, String filename, String data)
Uploads a file via a multipart form (see RFC2388).- Specified by:
filein interfaceRequestFixture- Parameters:
field- form field namefilename- filename of uploaded filedata- content of file- Returns:
- this
-
form
MultipartFormSpec form()
A specification of a multipart form (see RFC2388).Can be used to construct a multipart form with name value pairs and files
Note that more than one value and more than one file can be associated with a single field
- Specified by:
formin interfaceRequestFixture- Returns:
- a specification of a multipart form
-
form
GroovyRequestFixture form(Map<String,String> fields)
Sets the fields on a multipart form (see RFC2388).- Specified by:
formin interfaceRequestFixture- Parameters:
fields- map of field name to field value- Returns:
- this
-
responseHeader
GroovyRequestFixture responseHeader(CharSequence name, String value)
Set a response header value.Can be used to simulate the setting of a response header by an upstream handler.
By default there are no request headers.
- Specified by:
responseHeaderin interfaceRequestFixture- Parameters:
name- the header namevalue- the header value- Returns:
- this
-
method
GroovyRequestFixture method(String method)
Set the request method (case insensitive).The default method is
"GET".- Specified by:
methodin interfaceRequestFixture- Parameters:
method- the request method- Returns:
- this
-
uri
GroovyRequestFixture uri(String uri)
The URI of the request.No encoding is performed on the given value. It is expected to be a well formed URI path string (potentially including query and fragment strings)
- Specified by:
uriin interfaceRequestFixture- Parameters:
uri- the URI of the request- Returns:
- this
-
timeout
GroovyRequestFixture timeout(int timeoutSeconds)
Sets the maximum time to allow the handler under test to produce a result.As handlers may execute asynchronously, a maximum time limit must be used to guard against never ending handlers.
- Specified by:
timeoutin interfaceRequestFixture- Parameters:
timeoutSeconds- the maximum number of seconds to allow the handler(s) under test to produce a result- Returns:
- this
-
registry
GroovyRequestFixture registry(Action<? super RegistrySpec> action) throws Exception
Configures the context registry.- Specified by:
registryin interfaceRequestFixture- Parameters:
action- a registry specification action- Returns:
- this
- Throws:
Exception- any thrown byaction
-
pathBinding
GroovyRequestFixture pathBinding(Map<String,String> pathTokens)
Adds a path binding, with the given path tokens.By default, there are no path tokens and no path binding.
- Specified by:
pathBindingin interfaceRequestFixture- Parameters:
pathTokens- the path tokens to make available to the handler(s) under test- Returns:
- this
-
pathBinding
GroovyRequestFixture pathBinding(String boundTo, String pastBinding, Map<String,String> pathTokens)
Adds a path binding, with the given path tokens and parts.By default, there are no path tokens and no path binding.
- Specified by:
pathBindingin interfaceRequestFixture- Parameters:
boundTo- the part of the request path that the binding bound topastBinding- the part of the request path pastboundTopathTokens- the path tokens and binding to make available to the handler(s) under test- Returns:
- this
-
pathBinding
GroovyRequestFixture pathBinding(String boundTo, String pastBinding, Map<String,String> pathTokens, String description)
Adds a path binding, with the given path tokens and parts.By default, there are no path tokens and no path binding.
- Specified by:
pathBindingin interfaceRequestFixture- Parameters:
boundTo- the part of the request path that the binding bound topastBinding- the part of the request path pastboundTopathTokens- the path tokens and binding to make available to the handler(s) under testdescription- the description of the request path binding- Returns:
- this
-
serverConfig
GroovyRequestFixture serverConfig(Action<? super ServerConfigBuilder> action) throws Exception
Configures the server config to have no base dir and given configuration.By default the server config is equivalent to
ServerConfig.builder().build().- Specified by:
serverConfigin interfaceRequestFixture- Parameters:
action- configuration of the server config- Returns:
- this
- Throws:
Exception- any thrown byaction
-
remoteAddress
GroovyRequestFixture remoteAddress(com.google.common.net.HostAndPort remote)
Set the remote address from which the request is made.Effectively the return value of
Request.getRemoteAddress().- Specified by:
remoteAddressin interfaceRequestFixture- Parameters:
remote- the remote host and port address- Returns:
- this
-
localAddress
GroovyRequestFixture localAddress(com.google.common.net.HostAndPort local)
Set the local address to which this request is made.Effectively the return value of
Request.getLocalAddress().- Specified by:
localAddressin interfaceRequestFixture- Parameters:
local- the local host and port address- Returns:
- this
-
-