public interface RequestFixture
Handler
implementations.
A request fixture emulates a request, and the effective state of the request handling in the handler pipeline.
A request fixture can be obtained by the UnitTest.requestFixture()
method.
However it is often more convenient to use the alternative UnitTest.handle(ratpack.handling.Handler, ratpack.func.Action<? super ratpack.test.handling.RequestFixture>)
methods.
See UnitTest
for usage examples.
UnitTest
,
handle(ratpack.handling.Handler)
Modifier and Type | Method and Description |
---|---|
RequestFixture |
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. |
RequestFixture |
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. |
RegistrySpec |
getRegistry()
A specification of the context registry.
|
HandlingResult |
handle(Action<? super Chain> chainAction)
Similar to
handle(ratpack.handling.Handler) , but for testing a handler chain. |
HandlingResult |
handle(Handler handler)
Invokes the given handler with a newly created
Context based on the state of this fixture. |
RequestFixture |
header(String name,
String value)
Set a request header value.
|
RequestFixture |
launchConfig(Action<? super LaunchConfigBuilder> action)
Configures the launch config to have no base dir and given configuration.
|
RequestFixture |
launchConfig(Path baseDir,
Action<? super LaunchConfigBuilder> action)
Configures the launch config to have the given base dir and given configuration.
|
RequestFixture |
method(String method)
Set the request method (case insensitive).
|
RequestFixture |
pathBinding(Map<String,String> pathTokens)
Adds a path binding, with the given path tokens.
|
RequestFixture |
pathBinding(String boundTo,
String pastBinding,
Map<String,String> pathTokens)
Adds a path binding, with the given path tokens and parts.
|
RequestFixture |
registry(Action<? super RegistrySpec> action)
Configures the context registry.
|
RequestFixture |
responseHeader(String name,
String value)
Set a response header value.
|
RequestFixture |
timeout(int timeoutSeconds)
Sets the maximum time to allow the handler under test to produce a result.
|
RequestFixture |
uri(String uri)
The URI of the request.
|
RequestFixture body(byte[] bytes, String contentType)
Content-Type
request header of the given value.
By default the body is empty.
bytes
- the request body in bytescontentType
- the content type of the request bodyRequestFixture body(String text, String contentType)
Content-Type
request header of the given value.
By default the body is empty.
text
- the request body as a stringcontentType
- the content type of the request bodyRegistrySpec getRegistry()
Can be used to make objects (e.g. support services) available via context registry lookup.
By default, only a ServerErrorHandler
and ClientErrorHandler
are in the context registry.
HandlingResult handle(Handler handler) throws HandlerTimeoutException
Context
based on the state of this fixture.
The return value can be used to examine the effective result of the handler.
A result may be one of the following:
Response.send()
methodsContext.render(Object)
Context.clientError(int)
Context.error(Exception)
Context.next()
methodsinserted
by the handler under test will be invoked.
If the last inserted handler delegates to the next handler, the handling will terminate with a result indicating that the effective result was delegating to the next handler.
This method blocks until a result is achieved, even if the handler performs an asynchronous operation (such as performing blocking IO
).
As such, a time limit on the execution is imposed which by default is 5 seconds.
The time limit can be changed via the timeout(int)
method.
If the time limit is reached, a HandlerTimeoutException
is thrown.
handler
- the handler to testHandlerTimeoutException
- if the handler does not produce a result in the time limit defined by this fixtureHandlingResult handle(Action<? super Chain> chainAction) throws HandlerTimeoutException
handle(ratpack.handling.Handler)
, but for testing a handler chain.chainAction
- the handler chain to testHandlerTimeoutException
- if the handler does not produce a result in the time limit defined by this fixtureRequestFixture header(String name, String value)
By default there are no request headers.
name
- the header namevalue
- the header valueRequestFixture launchConfig(Path baseDir, Action<? super LaunchConfigBuilder> action) throws Exception
By default the launch config is equivalent to LaunchConfigBuilder.noBaseDir()
.build()
.
baseDir
- the launch config base diraction
- configuration of the launch configException
- any thrown by action
RequestFixture launchConfig(Action<? super LaunchConfigBuilder> action) throws Exception
By default the launch config is equivalent to LaunchConfigBuilder.noBaseDir()
.build()
.
action
- configuration of the launch configException
- any thrown by action
RequestFixture method(String method)
The default method is "GET"
.
method
- the request methodRequestFixture pathBinding(Map<String,String> pathTokens)
By default, there are no path tokens and no path binding.
pathTokens
- the path tokens to make available to the handler(s) under testRequestFixture pathBinding(String boundTo, String pastBinding, Map<String,String> pathTokens)
By default, there are no path tokens and no path binding.
pathTokens
- the path tokens and binding to make available to the handler(s) under testRequestFixture registry(Action<? super RegistrySpec> action) throws Exception
action
- a registry specification actionException
- any thrown by action
RequestFixture 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.
name
- the header namevalue
- the header valueRequestFixture timeout(int timeoutSeconds)
As handlers may execute asynchronously, a maximum time limit must be used to guard against never ending handlers.
timeoutSeconds
- the maximum number of seconds to allow the handler(s) under test to produce a resultRequestFixture 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
- the URI of the request