Improve parity between Java and Kotlin router DSL

This commit adds following functions to the Kotlin DSL:
add, filter, before, after and onError.

Closes gh-23524
This commit is contained in:
Sebastien Deleuze
2019-09-17 12:04:37 +02:00
parent 7a1a8e1623
commit 1dfe304da4
7 changed files with 341 additions and 27 deletions

View File

@@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test
import org.springframework.core.io.ClassPathResource
import org.springframework.http.HttpHeaders.*
import org.springframework.http.HttpMethod.*
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType.*
import org.springframework.web.reactive.function.server.MockServerRequest.builder
import reactor.test.StepVerifier
@@ -172,6 +173,28 @@ class CoRouterFunctionDslTests {
}
path("/baz", ::handle)
GET("/rendering") { RenderingResponse.create("index").buildAndAwait() }
add(otherRouter)
}
private val otherRouter = router {
"/other" {
ok().build()
}
filter { request, next ->
next(request)
}
before {
it
}
after { _, response ->
response
}
onError({it is IllegalStateException}) { _, _ ->
ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).build()
}
onError<IllegalStateException> { _, _ ->
ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).build()
}
}
@Suppress("UNUSED_PARAMETER")

View File

@@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test
import org.springframework.core.io.ClassPathResource
import org.springframework.http.HttpHeaders.*
import org.springframework.http.HttpMethod.*
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType.*
import org.springframework.web.reactive.function.server.MockServerRequest.builder
import reactor.core.publisher.Mono
@@ -173,6 +174,28 @@ class RouterFunctionDslTests {
}
path("/baz", ::handle)
GET("/rendering") { RenderingResponse.create("index").build() }
add(otherRouter)
}
private val otherRouter = router {
"/other" {
ok().build()
}
filter { request, next ->
next(request)
}
before {
it
}
after { _, response ->
response
}
onError({it is IllegalStateException}) { _, _ ->
ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).build()
}
onError<IllegalStateException> { _, _ ->
ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).build()
}
}
@Suppress("UNUSED_PARAMETER")