Add nested route support to Kotlin DSL

Issue: SPR-14954
This commit is contained in:
Sebastien Deleuze 2017-02-16 17:18:50 +01:00
parent 0da8dee289
commit 03f34e24ca
2 changed files with 8 additions and 5 deletions

View File

@ -69,6 +69,9 @@ class RouterDsl {
operator fun RequestPredicate.not(): RequestPredicate = this.negate()
fun RequestPredicate.route(routes: RouterDsl.() -> Unit) =
RouterFunctions.nest(this, RouterDsl().apply(routes).router())
operator fun RequestPredicate.invoke(f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(this, HandlerFunction { f(it) })
}

View File

@ -112,15 +112,15 @@ class RouterFunctionExtensionsTests {
override fun route(req: ServerRequest) = route(req) {
(GET("/foo/") or GET("/foos/")) { handle(req) }
accept(APPLICATION_JSON).apply {
POST("/api/foo/") { handleFromClass(req) }
PUT("/api/foo/") { handleFromClass(req) }
DELETE("/api/foo/") { handleFromClass(req) }
(pathPrefix("/api") and accept(APPLICATION_JSON)).route {
POST("/foo/") { handleFromClass(req) }
PUT("/foo/") { handleFromClass(req) }
DELETE("/foo/") { handleFromClass(req) }
}
accept(APPLICATION_ATOM_XML, ::handle)
contentType(APPLICATION_OCTET_STREAM) { handle(req) }
method(HttpMethod.PATCH) { handle(req) }
headers({ it.accept().contains(APPLICATION_JSON) }).apply {
headers({ it.accept().contains(APPLICATION_JSON) }).route {
GET("/api/foo/", ::handle)
}
headers({ it.header("bar").isNotEmpty() }, ::handle)