public interface GroovyRequestFixture extends RequestFixture
RequestFixture
.Modifier and Type | Method and Description |
---|---|
GroovyRequestFixture |
body(byte[] bytes,
String contentType)
Sets the request body to be the given bytes, and adds a
Content-Type request header of the given value. |
GroovyRequestFixture |
body(String text,
String contentType)
Sets the request body to be the given string in utf8 bytes, and adds a
Content-Type request header of the given value. |
static HandlingResult |
handle(Action<? super Chain> handlers,
Closure<?> closure)
Unit test a chain of
handlers . |
static HandlingResult |
handle(Handler handler,
Closure<?> closure)
Unit test a
Handler . |
GroovyRequestFixture |
header(String name,
String value)
Set a request header value.
|
GroovyRequestFixture |
launchConfig(Action<? super LaunchConfigBuilder> action)
Configures the launch config to have no base dir and given configuration.
|
GroovyRequestFixture |
launchConfig(Path baseDir,
Action<? super LaunchConfigBuilder> action)
Configures the launch config to have the given base dir and given configuration.
|
GroovyRequestFixture |
localAddress(HostAndPort local)
Set the local address to which this request is made.
|
GroovyRequestFixture |
method(String method)
Set the request method (case insensitive).
|
GroovyRequestFixture |
pathBinding(Map<String,String> pathTokens)
Adds a path binding, with the given path tokens.
|
GroovyRequestFixture |
pathBinding(String boundTo,
String pastBinding,
Map<String,String> pathTokens)
Adds a path binding, with the given path tokens and parts.
|
GroovyRequestFixture |
registry(Action<? super RegistrySpec> action)
Configures the context registry.
|
GroovyRequestFixture |
registry(Closure<?> closure)
A closure friendly overload of
registry(Action) . |
GroovyRequestFixture |
remoteAddress(HostAndPort remote)
Set the remote address from which the request is made.
|
static GroovyRequestFixture |
requestFixture()
Create a Groovy request fixture, for unit testing a
Handler . |
static GroovyRequestFixture |
requestFixture(RequestFixture requestFixture)
Create a Groovy request fixture, for unit testing a
Handler , by wrapping the given RequestFixture . |
GroovyRequestFixture |
responseHeader(String name,
String value)
Set a response header value.
|
GroovyRequestFixture |
timeout(int timeoutSeconds)
Sets the maximum time to allow the handler under test to produce a result.
|
GroovyRequestFixture |
uri(String uri)
The URI of the request.
|
getRegistry, handle, handle, handle, handleChain
static HandlingResult handle(Handler handler, @DelegatesTo(value=GroovyRequestFixture.class) Closure<?> closure) throws Exception
Handler
.
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"
handler
- the handler to testclosure
- the configuration of the request fixtureHandlerTimeoutException
- if the handler takes more than RequestFixture.timeout(int)
seconds to send a response or call next()
on the contextException
- any thrown by closure
static HandlingResult handle(Action<? super Chain> handlers, @DelegatesTo(value=GroovyRequestFixture.class) Closure<?> closure) throws Exception
handlers
.
Example:
import ratpack.groovy.test.handling.GroovyRequestFixture
import ratpack.groovy.Groovy
def handlers = Groovy.chain {
handler {
def outputHeaderValue = request.headers.get("input-value") + ":bar"
response.headers.set("output-value", outputHeaderValue)
next()
}
handler {
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"
handlers
- the handlers to testclosure
- the configuration of the request fixtureHandlerTimeoutException
- if the handler takes more than RequestFixture.timeout(int)
seconds to send a response or call next()
on the contextException
- any thrown by closure
static GroovyRequestFixture requestFixture()
Handler
.requestFixture
in interface RequestFixture
RequestFixture.handle(ratpack.handling.Handler, ratpack.func.Action)
,
RequestFixture.handle(ratpack.func.Action, ratpack.func.Action)
static GroovyRequestFixture requestFixture(RequestFixture requestFixture)
Handler
, by wrapping the given RequestFixture
.requestFixture
- The request fixture to wrapGroovyRequestFixture registry(@DelegatesTo(value=RegistryBuilder.class,strategy=1) Closure<?> closure)
registry(Action)
.closure
- the registry configurationregistry(Action)
GroovyRequestFixture header(String name, String value)
By default there are no request headers.
header
in interface RequestFixture
name
- the header namevalue
- the header valueGroovyRequestFixture body(byte[] bytes, String contentType)
Content-Type
request header of the given value.
By default the body is empty.
body
in interface RequestFixture
bytes
- the request body in bytescontentType
- the content type of the request bodyGroovyRequestFixture body(String text, String contentType)
Content-Type
request header of the given value.
By default the body is empty.
body
in interface RequestFixture
text
- the request body as a stringcontentType
- the content type of the request bodyGroovyRequestFixture responseHeader(String name, String value)
Can be used to simulate the setting of a response header by an upstream handler.
By default there are no request headers.
responseHeader
in interface RequestFixture
name
- the header namevalue
- the header valueGroovyRequestFixture method(String method)
The default method is "GET"
.
method
in interface RequestFixture
method
- the request methodGroovyRequestFixture uri(String uri)
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)
uri
in interface RequestFixture
uri
- the URI of the requestGroovyRequestFixture timeout(int timeoutSeconds)
As handlers may execute asynchronously, a maximum time limit must be used to guard against never ending handlers.
timeout
in interface RequestFixture
timeoutSeconds
- the maximum number of seconds to allow the handler(s) under test to produce a resultGroovyRequestFixture registry(Action<? super RegistrySpec> action) throws Exception
registry
in interface RequestFixture
action
- a registry specification actionException
- any thrown by action
GroovyRequestFixture pathBinding(Map<String,String> pathTokens)
By default, there are no path tokens and no path binding.
pathBinding
in interface RequestFixture
pathTokens
- the path tokens to make available to the handler(s) under testGroovyRequestFixture pathBinding(String boundTo, String pastBinding, Map<String,String> pathTokens)
By default, there are no path tokens and no path binding.
pathBinding
in interface RequestFixture
boundTo
- the part of the request path that the binding bound topastBinding
- the part of the request path past boundTo
pathTokens
- the path tokens and binding to make available to the handler(s) under testGroovyRequestFixture launchConfig(Path baseDir, Action<? super LaunchConfigBuilder> action) throws Exception
By default the launch config is equivalent to LaunchConfigBuilder.noBaseDir()
.build()
.
launchConfig
in interface RequestFixture
baseDir
- the launch config base diraction
- configuration of the launch configException
- any thrown by action
GroovyRequestFixture launchConfig(Action<? super LaunchConfigBuilder> action) throws Exception
By default the launch config is equivalent to LaunchConfigBuilder.noBaseDir()
.build()
.
launchConfig
in interface RequestFixture
action
- configuration of the launch configException
- any thrown by action
GroovyRequestFixture remoteAddress(HostAndPort remote)
Effectively the return value of Request.getRemoteAddress()
.
remoteAddress
in interface RequestFixture
remote
- the remote host and port addressGroovyRequestFixture localAddress(HostAndPort local)
Effectively the return value of Request.getLocalAddress()
.
localAddress
in interface RequestFixture
local
- the local host and port address