Skip to content

Commit

Permalink
GH-42 Support beforeMatched/afterMatched handlers in DSL module
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Feb 16, 2024
1 parent d051c3d commit 46196df
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ open class DefaultContextScopeConfiguration<
inline fun <reified PATH : Any> head(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.HEAD, handler)
inline fun <reified PATH : Any> options(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.OPTIONS, handler)
inline fun <reified PATH : Any> before(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.BEFORE, handler)
inline fun <reified PATH : Any> beforeMatched(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.BEFORE_MATCHED, handler)
inline fun <reified PATH : Any> after(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.AFTER, handler)
inline fun <reified PATH : Any> afterMatched(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.AFTER_MATCHED, handler)

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ class InPlaceRoutingDslTest : TestSpecification() {
Javalin.create { config ->
config.router.mount(Dsl) {
it.before("/before") { header("test", "before") }
it.beforeMatched("/before-matched") { header("test", "before-matched") }
it.get("/before-matched") { }
it.after("/after") { header("test", "after") }
it.afterMatched("/after-matched") { header("test", "after-matched") }
it.get("/after-matched") {}
it.get("/throwing") { throw RuntimeException() }
it.exception(Exception::class) { header("exception", it::class.java.name) }
it.get("/get") { header("test", "get") }
Expand Down Expand Up @@ -54,13 +58,27 @@ class InPlaceRoutingDslTest : TestSpecification() {
assertThat(it.headers.getFirst("test")).isEqualTo("before")
}

// when: a request is made to the before matched handler
get("${client.origin}/before-matched").asEmpty().also {
// then: the response is the same as the route name
assertThat(it.status).isEqualTo(200)
assertThat(it.headers.getFirst("test")).isEqualTo("before-matched")
}

// when: a request is made to the after handler
get("${client.origin}/after").asEmpty().also {
// then: the response is the same as the route name
assertThat(it.status).isEqualTo(404)
assertThat(it.headers.getFirst("test")).isEqualTo("after")
}

// when: a request is made to the after matched handler
get("${client.origin}/after-matched").asEmpty().also {
// then: the response is the same as the route name
assertThat(it.status).isEqualTo(200)
assertThat(it.headers.getFirst("test")).isEqualTo("after-matched")
}

// when: a request is made to the throwing handler
get("${client.origin}/throwing").asEmpty().also {
// then: the response is handled by exception handler
Expand Down Expand Up @@ -158,13 +176,15 @@ class InPlaceRoutingDslTest : TestSpecification() {
Javalin.create { config ->
config.router.mount(Dsl) {
it.before<ReifiedPath> { result("Before ") }
it.beforeMatched<ReifiedPath> { result(result() + "Before-Matched ") }
it.get<ReifiedPath> { result(result() + "GET") }
it.put<ReifiedPath> { result(result() + "PUT") }
it.post<ReifiedPath> { result(result() + "POST") }
it.patch<ReifiedPath> { result(result() + "PATCH") }
it.delete<ReifiedPath> { result(result() + "DELETE") }
it.head<ReifiedPath> { result(result() + "HEAD") }
it.options<ReifiedPath> { result(result() + "OPTIONS") }
it.afterMatched<ReifiedPath> { result(result() + " After-Matched") }
it.after<ReifiedPath> {
result(result() + " After")
header("test", result()!!)
Expand All @@ -180,7 +200,7 @@ class InPlaceRoutingDslTest : TestSpecification() {
.map { it to request(it.name, "${client.origin}/path").asString() }
.forEach { (method, response) ->
// then: the response is the same as the route name
assertThat(response.headers.getFirst("test")).isEqualTo("Before $method After")
assertThat(response.headers.getFirst("test")).isEqualTo("Before Before-Matched $method After-Matched After")
}
}

Expand Down

0 comments on commit 46196df

Please sign in to comment.