Skip to content

Commit

Permalink
KTOR 1344 Add unified pre-typed routes (ktorio#2211)
Browse files Browse the repository at this point in the history
* KTOR-1344 Add failing test

* KTOR-1344 Add put and post typed overload

* KTOR-1344 Split typed functions into two, one with path and one without

* KTOR-1344 Refactor test

* KTOR-1344 Remove path from comments

Co-authored-by: hfhbd <[email protected]>
  • Loading branch information
hfhbd and hfhbd committed Jun 7, 2021
1 parent f8b3b2d commit 4087fe0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,26 @@ public fun Route.post(path: String, body: PipelineInterceptor<Unit, ApplicationC
}

/**
* Builds a route to match `POST` requests with specified [path] receiving request body content of type [R]
* Builds a route to match `POST` requests receiving request body content of type [R]
*/
@ContextDsl
@JvmName("postTyped")
public inline fun <reified R : Any> Route.post(
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
): Route = post {
body(call.receive())
}

/**
* Builds a route to match `POST` requests with specified [path] receiving request body content of type [R]
*/
@ContextDsl
@JvmName("postTypedPath")
public inline fun <reified R : Any> Route.post(
path: String,
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
): Route {
return route(path, HttpMethod.Post) {
handle {
body(call.receive())
}
}
): Route = post(path) {
body(call.receive())
}

/**
Expand Down Expand Up @@ -168,6 +175,29 @@ public fun Route.put(body: PipelineInterceptor<Unit, ApplicationCall>): Route {
return method(HttpMethod.Put) { handle(body) }
}

/**
* Builds a route to match `PUT` requests with receiving request body content of type [R]
*/
@ContextDsl
@JvmName("putTyped")
public inline fun <reified R : Any> Route.put(
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
): Route = put {
body(call.receive())
}

/**
* Builds a route to match `PUT` requests with specified [path] receiving request body content of type [R]
*/
@ContextDsl
@JvmName("putTypedPath")
public inline fun <reified R : Any> Route.put(
path: String,
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
): Route = put(path) {
body(call.receive())
}

/**
* Builds a route to match `PATCH` requests with specified [path]
*/
Expand All @@ -184,6 +214,29 @@ public fun Route.patch(body: PipelineInterceptor<Unit, ApplicationCall>): Route
return method(HttpMethod.Patch) { handle(body) }
}

/**
* Builds a route to match `PATCH` requests receiving request body content of type [R]
*/
@ContextDsl
@JvmName("patchTyped")
public inline fun <reified R : Any> Route.patch(
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
): Route = patch {
body(call.receive())
}

/**
* Builds a route to match `PATCH` requests with specified [path] receiving request body content of type [R]
*/
@ContextDsl
@JvmName("patchTypedPath")
public inline fun <reified R : Any> Route.patch(
path: String,
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
): Route = patch(path) {
body(call.receive())
}

/**
* Builds a route to match `DELETE` requests with specified [path]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,31 @@ class RoutingProcessingTest {
}
}

@Test
fun testRouteWithTypedBody(): Unit = withTestApplication {
application.routing {
post<String> { answer ->
assertEquals("42", answer)
}
put<String>("/put") { answer ->
assertEquals("42", answer)
}
patch<String>("/patching") { answer ->
assertEquals("42", answer)
}
}

handleRequest(HttpMethod.Post, "/") {
setBody("42")
}
handleRequest(HttpMethod.Put, "/put") {
setBody("42")
}
handleRequest(HttpMethod.Patch, "/patching") {
setBody("42")
}
}

@Test
fun testInterceptionOrderWhenOuterShouldBeAfter() = withTestApplication {
var userIntercepted = false
Expand Down

0 comments on commit 4087fe0

Please sign in to comment.