Browse Source

Replace RouterFunction.route() by a router() Kotlin helper

pull/1357/merge
Sebastien Deleuze 8 years ago
parent
commit
abc0c6e3e8
  1. 6
      spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt
  2. 38
      spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt

6
spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt

@ -57,8 +57,10 @@ import reactor.core.publisher.Mono @@ -57,8 +57,10 @@ import reactor.core.publisher.Mono
typealias Routes = RouterDsl.() -> Unit
fun RouterFunction<*>.route(request: ServerRequest, configure: Routes) =
RouterDsl().apply(configure).invoke(request)
/**
* Allow to create easily a [RouterFunction] from [Routes]
*/
fun router(routes: Routes) = RouterDsl().apply(routes).router()
class RouterDsl {

38
spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt

@ -32,7 +32,7 @@ class RouterFunctionExtensionsTests { @@ -32,7 +32,7 @@ class RouterFunctionExtensionsTests {
@Test
fun header() {
val request = builder().header("bar", "bar").build()
StepVerifier.create(FooController().route(request))
StepVerifier.create(sampleRouter().route(request))
.expectNextCount(1)
.verifyComplete()
}
@ -40,7 +40,7 @@ class RouterFunctionExtensionsTests { @@ -40,7 +40,7 @@ class RouterFunctionExtensionsTests {
@Test
fun accept() {
val request = builder().header(ACCEPT, APPLICATION_ATOM_XML_VALUE).build()
StepVerifier.create(FooController().route(request))
StepVerifier.create(sampleRouter().route(request))
.expectNextCount(1)
.verifyComplete()
}
@ -48,7 +48,7 @@ class RouterFunctionExtensionsTests { @@ -48,7 +48,7 @@ class RouterFunctionExtensionsTests {
@Test
fun acceptAndPOST() {
val request = builder().method(POST).uri(URI("/api/foo/")).header(ACCEPT, APPLICATION_JSON_VALUE).build()
StepVerifier.create(FooController().route(request))
StepVerifier.create(sampleRouter().route(request))
.expectNextCount(1)
.verifyComplete()
}
@ -56,7 +56,7 @@ class RouterFunctionExtensionsTests { @@ -56,7 +56,7 @@ class RouterFunctionExtensionsTests {
@Test
fun contentType() {
val request = builder().header(CONTENT_TYPE, APPLICATION_OCTET_STREAM_VALUE).build()
StepVerifier.create(FooController().route(request))
StepVerifier.create(sampleRouter().route(request))
.expectNextCount(1)
.verifyComplete()
}
@ -64,7 +64,7 @@ class RouterFunctionExtensionsTests { @@ -64,7 +64,7 @@ class RouterFunctionExtensionsTests {
@Test
fun resourceByPath() {
val request = builder().uri(URI("/org/springframework/web/reactive/function/response.txt")).build()
StepVerifier.create(FooController().route(request))
StepVerifier.create(sampleRouter().route(request))
.expectNextCount(1)
.verifyComplete()
}
@ -72,7 +72,7 @@ class RouterFunctionExtensionsTests { @@ -72,7 +72,7 @@ class RouterFunctionExtensionsTests {
@Test
fun method() {
val request = builder().method(PATCH).build()
StepVerifier.create(FooController().route(request))
StepVerifier.create(sampleRouter().route(request))
.expectNextCount(1)
.verifyComplete()
}
@ -80,7 +80,7 @@ class RouterFunctionExtensionsTests { @@ -80,7 +80,7 @@ class RouterFunctionExtensionsTests {
@Test
fun path() {
val request = builder().uri(URI("/baz")).build()
StepVerifier.create(FooController().route(request))
StepVerifier.create(sampleRouter().route(request))
.expectNextCount(1)
.verifyComplete()
}
@ -88,7 +88,7 @@ class RouterFunctionExtensionsTests { @@ -88,7 +88,7 @@ class RouterFunctionExtensionsTests {
@Test
fun resource() {
val request = builder().uri(URI("/response.txt")).build()
StepVerifier.create(FooController().route(request))
StepVerifier.create(sampleRouter().route(request))
.expectNextCount(1)
.verifyComplete()
}
@ -100,22 +100,21 @@ class RouterFunctionExtensionsTests { @@ -100,22 +100,21 @@ class RouterFunctionExtensionsTests {
.header(ACCEPT, APPLICATION_PDF_VALUE)
.header(CONTENT_TYPE, APPLICATION_PDF_VALUE)
.build()
StepVerifier.create(FooController().route(request))
StepVerifier.create(sampleRouter().route(request))
.verifyComplete()
}
class FooController : RouterFunction<ServerResponse> {
override fun route(req: ServerRequest) = route(req) {
(GET("/foo/") or GET("/foos/")) { handle(req) }
fun sampleRouter() = router {
(GET("/foo/") or GET("/foos/")) { req -> handle(req) }
"/api".route {
POST("/foo/") { handleFromClass(req) }
PUT("/foo/") { handleFromClass(req) }
"/foo/" { handleFromClass(req) }
POST("/foo/", ::handleFromClass)
PUT("/foo/", :: handleFromClass)
"/foo/" { handleFromClass(it) }
}
accept(APPLICATION_ATOM_XML, ::handle)
contentType(APPLICATION_OCTET_STREAM) { handle(req) }
method(PATCH) { handle(req) }
contentType(APPLICATION_OCTET_STREAM, ::handle)
method(PATCH, ::handle)
headers({ it.accept().contains(APPLICATION_JSON) }).route {
GET("/api/foo/", ::handle)
}
@ -130,13 +129,12 @@ class RouterFunctionExtensionsTests { @@ -130,13 +129,12 @@ class RouterFunctionExtensionsTests {
Mono.empty()
}
}
path("/baz") { handle(req) }
path("/baz", ::handle)
}
}
@Suppress("UNUSED_PARAMETER")
fun handleFromClass(req: ServerRequest) = ok().build()
}
}
@Suppress("UNUSED_PARAMETER")
fun handle(req: ServerRequest) = ok().build()

Loading…
Cancel
Save