diff --git a/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/defaults/DefaultContextScope.kt b/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/defaults/DefaultContextScope.kt index a521b17..66fddfc 100644 --- a/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/defaults/DefaultContextScope.kt +++ b/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/defaults/DefaultContextScope.kt @@ -79,6 +79,8 @@ open class DefaultContextScopeConfiguration< inline fun head(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.HEAD, handler) inline fun options(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.OPTIONS, handler) inline fun before(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.BEFORE, handler) + inline fun beforeMatched(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.BEFORE_MATCHED, handler) inline fun after(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.AFTER, handler) + inline fun afterMatched(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.AFTER_MATCHED, handler) } diff --git a/routing-dsl/src/test/kotlin/io/javalin/community/routing/dsl/InPlaceRoutingDslTest.kt b/routing-dsl/src/test/kotlin/io/javalin/community/routing/dsl/InPlaceRoutingDslTest.kt index 73c6698..7829bbb 100644 --- a/routing-dsl/src/test/kotlin/io/javalin/community/routing/dsl/InPlaceRoutingDslTest.kt +++ b/routing-dsl/src/test/kotlin/io/javalin/community/routing/dsl/InPlaceRoutingDslTest.kt @@ -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") } @@ -54,6 +58,13 @@ 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 @@ -61,6 +72,13 @@ class InPlaceRoutingDslTest : TestSpecification() { 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 @@ -158,6 +176,7 @@ class InPlaceRoutingDslTest : TestSpecification() { Javalin.create { config -> config.router.mount(Dsl) { it.before { result("Before ") } + it.beforeMatched { result(result() + "Before-Matched ") } it.get { result(result() + "GET") } it.put { result(result() + "PUT") } it.post { result(result() + "POST") } @@ -165,6 +184,7 @@ class InPlaceRoutingDslTest : TestSpecification() { it.delete { result(result() + "DELETE") } it.head { result(result() + "HEAD") } it.options { result(result() + "OPTIONS") } + it.afterMatched { result(result() + " After-Matched") } it.after { result(result() + " After") header("test", result()!!) @@ -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") } }