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 GroovyRequestFixture
body(byte[] bytes, String contentType)
Sets the request body to be the given bytes, and adds aContent-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 aContent-Type
request header of the given value.MultipartFileSpec
file()
A specification of a file to upload (see RFC2388).RequestFixture
file(String field, String filename, String data)
Uploads a file via a multipart form (see RFC2388).MultipartFormSpec
form()
A specification of a multipart form (see RFC2388).GroovyRequestFixture
form(Map<String,String> fields)
Sets the fields on a multipart form (see RFC2388).static HandlingResult
handle(Handler handler, Closure<?> closure)
Unit test aHandler
.static HandlingResult
handle(Action<? super Chain> handlers, Closure<?> closure)
Unit test a chain ofhandlers
.GroovyRequestFixture
header(CharSequence name, 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(String method)
Set the request method (case insensitive).GroovyRequestFixture
pathBinding(String boundTo, String pastBinding, Map<String,String> pathTokens)
Adds a path binding, with the given path tokens and parts.GroovyRequestFixture
pathBinding(String boundTo, String pastBinding, Map<String,String> pathTokens, String description)
Adds a path binding, with the given path tokens and parts.GroovyRequestFixture
pathBinding(Map<String,String> pathTokens)
Adds a path binding, with the given path tokens.GroovyRequestFixture
registry(Closure<?> closure)
A closure friendly overload ofregistry(Action)
.GroovyRequestFixture
registry(Action<? super RegistrySpec> action)
Configures the context registry.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 aHandler
.static GroovyRequestFixture
requestFixture(RequestFixture requestFixture)
Create a Groovy request fixture, for unit testing aHandler
, by wrapping the givenRequestFixture
.GroovyRequestFixture
responseHeader(CharSequence name, 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(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:
header
in 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-Type
request header of the given value.By default the body is empty.
- Specified by:
body
in 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-Type
request header of the given value.By default the body is empty.
- Specified by:
body
in 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:
file
in 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:
file
in 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:
form
in 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:
form
in 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:
responseHeader
in 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:
method
in 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:
uri
in 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:
timeout
in 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:
registry
in 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:
pathBinding
in 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:
pathBinding
in interfaceRequestFixture
- Parameters:
boundTo
- the part of the request path that the binding bound topastBinding
- the part of the request path pastboundTo
pathTokens
- 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:
pathBinding
in interfaceRequestFixture
- Parameters:
boundTo
- the part of the request path that the binding bound topastBinding
- the part of the request path pastboundTo
pathTokens
- 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:
serverConfig
in 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:
remoteAddress
in 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:
localAddress
in interfaceRequestFixture
- Parameters:
local
- the local host and port address- Returns:
- this
-
-