Improve parity between Java and Kotlin router DSL
This commit adds variants with pattern + predicate to Kotlin router DSLs, and vararg where necessary. Closes gh-23524
This commit is contained in:
@@ -145,13 +145,25 @@ class RouterFunctionDsl(private val init: (RouterFunctionDsl.() -> Unit)) {
|
||||
fun String.nest(r: (RouterFunctionDsl.() -> Unit)) = path(this).nest(r)
|
||||
|
||||
/**
|
||||
* Route to the given handler function if the given request predicate applies.
|
||||
* @see RouterFunctions.route
|
||||
* Adds a route to the given handler function that handles all HTTP `GET` requests
|
||||
* that match the given pattern.
|
||||
* @param pattern the pattern to match to
|
||||
*/
|
||||
fun GET(pattern: String, f: (ServerRequest) -> ServerResponse) {
|
||||
builder.GET(pattern, HandlerFunction(f))
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a route to the given handler function that handles all HTTP `GET` requests
|
||||
* that match the given pattern.
|
||||
* @param pattern the pattern to match to
|
||||
* @param predicate additional predicate to match
|
||||
* @since 5.2
|
||||
*/
|
||||
fun GET(pattern: String, predicate: RequestPredicate, f: (ServerRequest) -> ServerResponse) {
|
||||
builder.GET(pattern, predicate, HandlerFunction(f))
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a [RequestPredicate] that matches if request's HTTP method is `GET`
|
||||
* and the given `pattern` matches against the request path.
|
||||
@@ -160,13 +172,25 @@ class RouterFunctionDsl(private val init: (RouterFunctionDsl.() -> Unit)) {
|
||||
fun GET(pattern: String): RequestPredicate = RequestPredicates.GET(pattern)
|
||||
|
||||
/**
|
||||
* Route to the given handler function if the given request predicate applies.
|
||||
* @see RouterFunctions.route
|
||||
* Adds a route to the given handler function that handles all HTTP `HEAD` requests
|
||||
* that match the given pattern.
|
||||
* @param pattern the pattern to match to
|
||||
*/
|
||||
fun HEAD(pattern: String, f: (ServerRequest) -> ServerResponse) {
|
||||
builder.HEAD(pattern, HandlerFunction(f))
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a route to the given handler function that handles all HTTP `HEAD` requests
|
||||
* that match the given pattern.
|
||||
* @param pattern the pattern to match to
|
||||
* @param predicate additional predicate to match
|
||||
* @since 5.2
|
||||
*/
|
||||
fun HEAD(pattern: String, predicate: RequestPredicate, f: (ServerRequest) -> ServerResponse) {
|
||||
builder.HEAD(pattern, predicate, HandlerFunction(f))
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a [RequestPredicate] that matches if request's HTTP method is `HEAD`
|
||||
* and the given `pattern` matches against the request path.
|
||||
@@ -175,13 +199,25 @@ class RouterFunctionDsl(private val init: (RouterFunctionDsl.() -> Unit)) {
|
||||
fun HEAD(pattern: String): RequestPredicate = RequestPredicates.HEAD(pattern)
|
||||
|
||||
/**
|
||||
* Route to the given handler function if the given `POST` predicate applies.
|
||||
* @see RouterFunctions.route
|
||||
* Adds a route to the given handler function that handles all HTTP `POST` requests
|
||||
* that match the given pattern.
|
||||
* @param pattern the pattern to match to
|
||||
*/
|
||||
fun POST(pattern: String, f: (ServerRequest) -> ServerResponse) {
|
||||
builder.POST(pattern, HandlerFunction(f))
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a route to the given handler function that handles all HTTP `POST` requests
|
||||
* that match the given pattern.
|
||||
* @param pattern the pattern to match to
|
||||
* @param predicate additional predicate to match
|
||||
* @since 5.2
|
||||
*/
|
||||
fun POST(pattern: String, predicate: RequestPredicate, f: (ServerRequest) -> ServerResponse) {
|
||||
builder.POST(pattern, predicate, HandlerFunction(f))
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a [RequestPredicate] that matches if request's HTTP method is `POST`
|
||||
* and the given `pattern` matches against the request path.
|
||||
@@ -190,13 +226,25 @@ class RouterFunctionDsl(private val init: (RouterFunctionDsl.() -> Unit)) {
|
||||
fun POST(pattern: String): RequestPredicate = RequestPredicates.POST(pattern)
|
||||
|
||||
/**
|
||||
* Route to the given handler function if the given `PUT` predicate applies.
|
||||
* @see RouterFunctions.route
|
||||
* Adds a route to the given handler function that handles all HTTP `PUT` requests
|
||||
* that match the given pattern.
|
||||
* @param pattern the pattern to match to
|
||||
*/
|
||||
fun PUT(pattern: String, f: (ServerRequest) -> ServerResponse) {
|
||||
builder.PUT(pattern, HandlerFunction(f))
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a route to the given handler function that handles all HTTP `PUT` requests
|
||||
* that match the given pattern.
|
||||
* @param pattern the pattern to match to
|
||||
* @param predicate additional predicate to match
|
||||
* @since 5.2
|
||||
*/
|
||||
fun PUT(pattern: String, predicate: RequestPredicate, f: (ServerRequest) -> ServerResponse) {
|
||||
builder.PUT(pattern, predicate, HandlerFunction(f))
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a [RequestPredicate] that matches if request's HTTP method is `PUT`
|
||||
* and the given `pattern` matches against the request path.
|
||||
@@ -205,13 +253,25 @@ class RouterFunctionDsl(private val init: (RouterFunctionDsl.() -> Unit)) {
|
||||
fun PUT(pattern: String): RequestPredicate = RequestPredicates.PUT(pattern)
|
||||
|
||||
/**
|
||||
* Route to the given handler function if the given `PATCH` predicate applies.
|
||||
* @see RouterFunctions.route
|
||||
* Adds a route to the given handler function that handles all HTTP `PATCH` requests
|
||||
* that match the given pattern.
|
||||
* @param pattern the pattern to match to
|
||||
*/
|
||||
fun PATCH(pattern: String, f: (ServerRequest) -> ServerResponse) {
|
||||
builder.PATCH(pattern, HandlerFunction(f))
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a route to the given handler function that handles all HTTP `PATCH` requests
|
||||
* that match the given pattern.
|
||||
* @param pattern the pattern to match to
|
||||
* @param predicate additional predicate to match
|
||||
* @since 5.2
|
||||
*/
|
||||
fun PATCH(pattern: String, predicate: RequestPredicate, f: (ServerRequest) -> ServerResponse) {
|
||||
builder.PATCH(pattern, predicate, HandlerFunction(f))
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a [RequestPredicate] that matches if request's HTTP method is `PATCH`
|
||||
* and the given `pattern` matches against the request path.
|
||||
@@ -222,13 +282,25 @@ class RouterFunctionDsl(private val init: (RouterFunctionDsl.() -> Unit)) {
|
||||
fun PATCH(pattern: String): RequestPredicate = RequestPredicates.PATCH(pattern)
|
||||
|
||||
/**
|
||||
* Route to the given handler function if the given `DELETE` predicate applies.
|
||||
* @see RouterFunctions.route
|
||||
* Adds a route to the given handler function that handles all HTTP `DELETE` requests
|
||||
* that match the given pattern.
|
||||
* @param pattern the pattern to match to
|
||||
*/
|
||||
fun DELETE(pattern: String, f: (ServerRequest) -> ServerResponse) {
|
||||
builder.DELETE(pattern, HandlerFunction(f))
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a route to the given handler function that handles all HTTP `DELETE` requests
|
||||
* that match the given pattern.
|
||||
* @param pattern the pattern to match to
|
||||
* @param predicate additional predicate to match
|
||||
* @since 5.2
|
||||
*/
|
||||
fun DELETE(pattern: String, predicate: RequestPredicate, f: (ServerRequest) -> ServerResponse) {
|
||||
builder.DELETE(pattern, predicate, HandlerFunction(f))
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a [RequestPredicate] that matches if request's HTTP method is `DELETE`
|
||||
* and the given `pattern` matches against the request path.
|
||||
@@ -239,13 +311,25 @@ class RouterFunctionDsl(private val init: (RouterFunctionDsl.() -> Unit)) {
|
||||
fun DELETE(pattern: String): RequestPredicate = RequestPredicates.DELETE(pattern)
|
||||
|
||||
/**
|
||||
* Route to the given handler function if the given OPTIONS predicate applies.
|
||||
* @see RouterFunctions.route
|
||||
* Adds a route to the given handler function that handles all HTTP `OPTIONS` requests
|
||||
* that match the given pattern.
|
||||
* @param pattern the pattern to match to
|
||||
*/
|
||||
fun OPTIONS(pattern: String, f: (ServerRequest) -> ServerResponse) {
|
||||
builder.OPTIONS(pattern, HandlerFunction(f))
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a route to the given handler function that handles all HTTP `OPTIONS` requests
|
||||
* that match the given pattern.
|
||||
* @param pattern the pattern to match to
|
||||
* @param predicate additional predicate to match
|
||||
* @since 5.2
|
||||
*/
|
||||
fun OPTIONS(pattern: String, predicate: RequestPredicate, f: (ServerRequest) -> ServerResponse) {
|
||||
builder.OPTIONS(pattern, predicate, HandlerFunction(f))
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a [RequestPredicate] that matches if request's HTTP method is `OPTIONS`
|
||||
* and the given `pattern` matches against the request path.
|
||||
|
||||
@@ -55,6 +55,15 @@ class RouterFunctionDslTests {
|
||||
assertThat(sampleRouter().route(request).isPresent).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun acceptAndPOSTWithRequestPredicate() {
|
||||
val servletRequest = MockHttpServletRequest("POST", "/api/bar/")
|
||||
servletRequest.addHeader(ACCEPT, APPLICATION_JSON_VALUE)
|
||||
servletRequest.addHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE)
|
||||
val request = DefaultServerRequest(servletRequest, emptyList())
|
||||
assertThat(sampleRouter().route(request).isPresent).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun contentType() {
|
||||
val servletRequest = MockHttpServletRequest("GET", "/content")
|
||||
@@ -120,6 +129,7 @@ class RouterFunctionDslTests {
|
||||
(GET("/foo/") or GET("/foos/")) { req -> handle(req) }
|
||||
"/api".nest {
|
||||
POST("/foo/", ::handleFromClass)
|
||||
POST("/bar/", contentType(APPLICATION_JSON), ::handleFromClass)
|
||||
PUT("/foo/", :: handleFromClass)
|
||||
PATCH("/foo/") {
|
||||
ok().build()
|
||||
@@ -148,10 +158,10 @@ class RouterFunctionDslTests {
|
||||
path("/baz", ::handle)
|
||||
GET("/rendering") { RenderingResponse.create("index").build() }
|
||||
}
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
private fun handleFromClass(req: ServerRequest) = ServerResponse.ok().build()
|
||||
}
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
private fun handleFromClass(req: ServerRequest) = ServerResponse.ok().build()
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
private fun handle(req: ServerRequest) = ServerResponse.ok().build()
|
||||
|
||||
Reference in New Issue
Block a user