Modifier and Type | Method and Description |
---|---|
void |
byContent(Closure<?> closure)
Groovy friendly overload of
Context.byContent(ratpack.func.Action) . |
void |
byMethod(Closure<?> closure)
Groovy friendly overload of
Context.byMethod(ratpack.func.Action) . |
static GroovyContext |
from(Context ctx)
Creates a Groovy context from a context.
|
GroovyContext |
getContext()
Returns this.
|
void |
onClose(Closure<?> closure)
Adds a request close handler.
|
byContent, byMethod, clientError, error, file, getAllPathTokens, getDirectChannelAccess, getExecution, getFileSystemBinding, getPathTokens, getRequest, getResponse, getServerConfig, insert, insert, lastModified, next, next, onClose, parse, parse, parse, parse, parse, parse, redirect, redirect, render
static GroovyContext from(Context ctx)
ctx
- the actual contextGroovyContext getContext()
getContext
in interface Context
void byMethod(@DelegatesTo(value=ByMethodSpec.class,strategy=1) Closure<?> closure) throws Exception
Context.byMethod(ratpack.func.Action)
.
import ratpack.groovy.test.handling.GroovyRequestFixture import static ratpack.groovy.Groovy.groovyHandler def handler = groovyHandler { byMethod { def message = "hello!" get { render "$message from GET request" } post { render "$message from POST request" } } } def result = GroovyRequestFixture.handle(handler) { method "get" } assert result.rendered(CharSequence) == "hello! from GET request" result = GroovyRequestFixture.handle(handler) { method "post" } assert result.rendered(CharSequence) == "hello! from POST request"
closure
- defines the action to take for different HTTP methodsException
- any thrown by the closurevoid byContent(@DelegatesTo(value=ByContentSpec.class,strategy=1) Closure<?> closure) throws Exception
Context.byContent(ratpack.func.Action)
.
import ratpack.groovy.test.handling.GroovyRequestFixture
import static ratpack.groovy.Groovy.groovyHandler
def handler = groovyHandler {
byContent {
def message = "hello!"
json {
render "{\"msg\": \"$message\"}"
}
html {
render "<p>$message</p>"
}
}
}
def result = GroovyRequestFixture.handle(handler) {
header("Accept", "application/json");
}
assert result.rendered(CharSequence) == "{\"msg\": \"hello!\"}"
assert result.headers.get("content-type") == "application/json"
result = GroovyRequestFixture.handle(handler) {
header("Accept", "text/plain; q=1.0, text/html; q=0.8, application/json; q=0.7");
}
assert result.rendered(CharSequence) == "<p>hello!</p>";
assert result.headers.get("content-type") == "text/html";
closure
- defines the action to take for the different content typesException
- any thrown by the closurevoid onClose(@DelegatesTo(value=RequestOutcome.class,strategy=1) Closure<?> closure)
closure
- A closure to call when the request is closed