public interface GroovyRequestFixture extends RequestFixture
RequestFixture.| Modifier and Type | Method and Description |
|---|---|
GroovyRequestFixture |
body(byte[] bytes,
java.lang.String contentType)
Sets the request body to be the given bytes, and adds a
Content-Type request header of the given value. |
GroovyRequestFixture |
body(java.lang.String text,
java.lang.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. |
MultipartFileSpec |
file()
A specification of a file to upload (see RFC2388)
|
RequestFixture |
file(java.lang.String field,
java.lang.String filename,
java.lang.String data)
Uploads a file via a multipart form (see RFC2388)
|
MultipartFormSpec |
form()
A specification of a multipart form (see RFC2388)
|
GroovyRequestFixture |
form(java.util.Map<java.lang.String,java.lang.String> fields)
Sets the fields on a multipart form (see RFC2388)
|
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(java.lang.CharSequence name,
java.lang.String value)
Set a request header value.
|
GroovyRequestFixture |
localAddress(com.google.common.net.HostAndPort local)
Set the local address to which this request is made.
|
GroovyRequestFixture |
method(java.lang.String method)
Set the request method (case insensitive).
|
GroovyRequestFixture |
pathBinding(java.util.Map<java.lang.String,java.lang.String> pathTokens)
Adds a path binding, with the given path tokens.
|
GroovyRequestFixture |
pathBinding(java.lang.String boundTo,
java.lang.String pastBinding,
java.util.Map<java.lang.String,java.lang.String> pathTokens)
Adds a path binding, with the given path tokens and parts.
|
GroovyRequestFixture |
pathBinding(java.lang.String boundTo,
java.lang.String pastBinding,
java.util.Map<java.lang.String,java.lang.String> pathTokens,
java.lang.String description)
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(com.google.common.net.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(java.lang.CharSequence name,
java.lang.String value)
Set a response header value.
|
GroovyRequestFixture |
serverConfig(Action<? super ServerConfigBuilder> action)
Configures the server config to have no base dir and given configuration.
|
GroovyRequestFixture |
timeout(int timeoutSeconds)
Sets the maximum time to allow the handler under test to produce a result.
|
GroovyRequestFixture |
uri(java.lang.String uri)
The URI of the request.
|
getRegistry, handle, handle, handle, handleChain, protocolstatic HandlingResult handle(Handler handler, @DelegatesTo(value=GroovyRequestFixture.class) Closure<?> closure) throws java.lang.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 contextjava.lang.Exception - any thrown by closurestatic HandlingResult handle(Action<? super Chain> handlers, @DelegatesTo(value=GroovyRequestFixture.class) Closure<?> closure) throws java.lang.Exception
handlers.
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"
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 contextjava.lang.Exception - any thrown by closurestatic GroovyRequestFixture requestFixture()
Handler.requestFixture in interface RequestFixtureRequestFixture.handle(Handler, Action),
RequestFixture.handle(Action, 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(java.lang.CharSequence name, java.lang.String value)
By default there are no request headers.
header in interface RequestFixturename - the header namevalue - the header valueGroovyRequestFixture body(byte[] bytes, java.lang.String contentType)
Content-Type request header of the given value.
By default the body is empty.
body in interface RequestFixturebytes - the request body in bytescontentType - the content type of the request bodyGroovyRequestFixture body(java.lang.String text, java.lang.String contentType)
Content-Type request header of the given value.
By default the body is empty.
body in interface RequestFixturetext - the request body as a stringcontentType - the content type of the request bodyMultipartFileSpec file()
Can be used to construct a multipart form with files
file in interface RequestFixtureRequestFixture file(java.lang.String field, java.lang.String filename, java.lang.String data)
file in interface RequestFixturefield - form field namefilename - filename of uploaded filedata - content of fileMultipartFormSpec form()
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
form in interface RequestFixtureGroovyRequestFixture form(java.util.Map<java.lang.String,java.lang.String> fields)
form in interface RequestFixturefields - map of field name to field valueGroovyRequestFixture responseHeader(java.lang.CharSequence name, java.lang.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 RequestFixturename - the header namevalue - the header valueGroovyRequestFixture method(java.lang.String method)
The default method is "GET".
method in interface RequestFixturemethod - the request methodGroovyRequestFixture uri(java.lang.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 RequestFixtureuri - 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 RequestFixturetimeoutSeconds - the maximum number of seconds to allow the handler(s) under test to produce a resultGroovyRequestFixture registry(Action<? super RegistrySpec> action) throws java.lang.Exception
registry in interface RequestFixtureaction - a registry specification actionjava.lang.Exception - any thrown by actionGroovyRequestFixture pathBinding(java.util.Map<java.lang.String,java.lang.String> pathTokens)
By default, there are no path tokens and no path binding.
pathBinding in interface RequestFixturepathTokens - the path tokens to make available to the handler(s) under testGroovyRequestFixture pathBinding(java.lang.String boundTo, java.lang.String pastBinding, java.util.Map<java.lang.String,java.lang.String> pathTokens)
By default, there are no path tokens and no path binding.
pathBinding in interface RequestFixtureboundTo - the part of the request path that the binding bound topastBinding - the part of the request path past boundTopathTokens - the path tokens and binding to make available to the handler(s) under testGroovyRequestFixture pathBinding(java.lang.String boundTo, java.lang.String pastBinding, java.util.Map<java.lang.String,java.lang.String> pathTokens, java.lang.String description)
By default, there are no path tokens and no path binding.
pathBinding in interface RequestFixtureboundTo - the part of the request path that the binding bound topastBinding - the part of the request path past boundTopathTokens - the path tokens and binding to make available to the handler(s) under testdescription - the description of the request path bindingGroovyRequestFixture serverConfig(Action<? super ServerConfigBuilder> action) throws java.lang.Exception
By default the server config is equivalent to ServerConfig.builder().build().
serverConfig in interface RequestFixtureaction - configuration of the server configjava.lang.Exception - any thrown by actionGroovyRequestFixture remoteAddress(com.google.common.net.HostAndPort remote)
Effectively the return value of Request.getRemoteAddress().
remoteAddress in interface RequestFixtureremote - the remote host and port addressGroovyRequestFixture localAddress(com.google.common.net.HostAndPort local)
Effectively the return value of Request.getLocalAddress().
localAddress in interface RequestFixturelocal - the local host and port address