diff --git a/routing-core/src/main/kotlin/io/javalin/community/routing/JavalinRoutingExtensions.kt b/routing-core/src/main/kotlin/io/javalin/community/routing/JavalinRoutingExtensions.kt index 6a52be5..b1b45a9 100644 --- a/routing-core/src/main/kotlin/io/javalin/community/routing/JavalinRoutingExtensions.kt +++ b/routing-core/src/main/kotlin/io/javalin/community/routing/JavalinRoutingExtensions.kt @@ -2,6 +2,8 @@ package io.javalin.community.routing import io.javalin.Javalin import io.javalin.http.Handler +import io.javalin.http.HandlerType +import io.javalin.router.InternalRouter import io.javalin.security.RouteRole class JavalinRoutingExtensions(private val javalin: Javalin) { @@ -20,7 +22,7 @@ class JavalinRoutingExtensions(private val javalin: Javalin) { fun register(): Javalin { routes .sortRoutes() - .forEach { javalin.registerRoute(it) } + .forEach { javalin.unsafeConfig().pvt.internalRouter.registerRoute(it) } return javalin } @@ -34,19 +36,19 @@ data class HandlerEntry @JvmOverloads constructor( val roles: List = emptyList(), ) : Routed -fun Javalin.registerRoute(handlerEntry: HandlerEntry) = +fun InternalRouter.registerRoute(handlerEntry: HandlerEntry) = registerRoute(handlerEntry.route, handlerEntry.path, handlerEntry.handler, *handlerEntry.roles.toTypedArray()) -fun Javalin.registerRoute(route: Route, path: String, handler: Handler, vararg roles: RouteRole) { +fun InternalRouter.registerRoute(route: Route, path: String, handler: Handler, vararg roles: RouteRole) { when (route) { - Route.HEAD -> head(path, handler, *roles) - Route.PATCH -> patch(path, handler, *roles) - Route.OPTIONS -> options(path, handler, *roles) - Route.GET -> get(path, handler, *roles) - Route.PUT -> put(path, handler, *roles) - Route.POST -> post(path, handler, *roles) - Route.DELETE -> delete(path, handler, *roles) - Route.AFTER -> after(path, handler) - Route.BEFORE -> before(path, handler) + Route.HEAD -> addHttpHandler(HandlerType.HEAD, path, handler, *roles) + Route.PATCH -> addHttpHandler(HandlerType.PATCH, path, handler, *roles) + Route.OPTIONS -> addHttpHandler(HandlerType.OPTIONS, path, handler, *roles) + Route.GET -> addHttpHandler(HandlerType.GET, path, handler, *roles) + Route.PUT -> addHttpHandler(HandlerType.PUT, path, handler, *roles) + Route.POST -> addHttpHandler(HandlerType.POST, path, handler, *roles) + Route.DELETE -> addHttpHandler(HandlerType.DELETE, path, handler, *roles) + Route.AFTER -> addHttpHandler(HandlerType.AFTER, path, handler) + Route.BEFORE -> addHttpHandler(HandlerType.BEFORE, path, handler) } } \ No newline at end of file diff --git a/routing-core/src/test/kotlin/io/javalin/community/routing/JavalinRoutesTest.kt b/routing-core/src/test/kotlin/io/javalin/community/routing/JavalinRoutesTest.kt index 664c43b..050e411 100644 --- a/routing-core/src/test/kotlin/io/javalin/community/routing/JavalinRoutesTest.kt +++ b/routing-core/src/test/kotlin/io/javalin/community/routing/JavalinRoutesTest.kt @@ -5,6 +5,7 @@ import io.javalin.community.routing.Route.GET import io.javalin.community.routing.Route.PUT import io.javalin.http.Handler import io.javalin.http.HandlerType +import io.javalin.util.Util.firstOrNull import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test @@ -13,7 +14,7 @@ class JavalinRoutesTest { @Test fun `should properly register handler by given enum`() { // given: a list of routes - val routes = Route.values() + val routes = Route.entries .map { it to Handler { ctx -> ctx.result(it.name) } } // when: routes are registered @@ -23,9 +24,8 @@ class JavalinRoutesTest { // then: all routes are registered by as proper HandlerType routes.forEach { (method, handler) -> assertThat( - app.javalinServlet() - .matcher - .findEntries(HandlerType.findByName(method.name), "/") + app.unsafeConfig().pvt.internalRouter + .findHttpHandlerEntries(HandlerType.findByName(method.name), "/") .firstOrNull() ?.handler ).isEqualTo(handler) @@ -41,9 +41,8 @@ class JavalinRoutesTest { listOf("GET", "PUT").forEach { method -> assertThat( - app.javalinServlet() - .matcher - .findEntries(HandlerType.findByName(method), "/") + app.unsafeConfig().pvt.internalRouter + .findHttpHandlerEntries(HandlerType.findByName(method), "/") .firstOrNull() ).isNotNull } diff --git a/routing-coroutines/src/main/kotlin/io/javalin/community/routing/coroutines/ReactiveRoutingPlugin.kt b/routing-coroutines/src/main/kotlin/io/javalin/community/routing/coroutines/ReactiveRoutingPlugin.kt index 08cb406..8e78d9d 100644 --- a/routing-coroutines/src/main/kotlin/io/javalin/community/routing/coroutines/ReactiveRoutingPlugin.kt +++ b/routing-coroutines/src/main/kotlin/io/javalin/community/routing/coroutines/ReactiveRoutingPlugin.kt @@ -1,39 +1,46 @@ package io.javalin.community.routing.coroutines -import io.javalin.Javalin +import io.javalin.community.routing.Route import io.javalin.community.routing.coroutines.servlet.CoroutinesServlet -import io.javalin.community.routing.registerRoute import io.javalin.community.routing.sortRoutes import io.javalin.config.JavalinConfig import io.javalin.http.Handler -import io.javalin.plugin.Plugin +import io.javalin.http.HandlerType +import io.javalin.router.InternalRouter +import io.javalin.router.RoutingApiInitializer +import java.util.function.Consumer -class ReactiveRoutingPlugin, CONTEXT, RESPONSE : Any>( - private val servlet: CoroutinesServlet -) : Plugin { +class CoroutinesRouting { + internal val routes = mutableListOf>() - private val routes = mutableListOf() + fun route(route: ReactiveRoute) { + routes.add(route) + } - override fun apply(app: Javalin) { - routes - .sortRoutes() - .map { it to Handler { ctx -> servlet.handle(ctx, it) } } - .forEach { (reactiveRoute, handler) -> app.registerRoute(reactiveRoute.method, reactiveRoute.path, handler) } + fun route(method: Route, path: String, async: Boolean = true, handler: suspend CONTEXT.() -> RESPONSE) { + routes.add(ReactiveRoute(path, method, async, handler)) } - fun > routing(vararg reactiveRoutes: ROUTES) { - reactiveRoutes.forEach { - routes.addAll(it.routes()) - } + fun routes(exampleEndpoint: ReactiveRoutes, CONTEXT, RESPONSE>): CoroutinesRouting { + exampleEndpoint.routes().forEach { route(it) } + return this } } -fun , ROUTES : ReactiveRoutes, CONTEXT, RESPONSE : Any> JavalinConfig.reactiveRouting( - servlet: CoroutinesServlet, - vararg routes: ROUTES -) { - val reactiveRoutingPlugin = ReactiveRoutingPlugin(servlet) - reactiveRoutingPlugin.routing(*routes) - this.plugins.register(reactiveRoutingPlugin) +class Coroutines, CONTEXT, RESPONSE : Any>( + private val servlet: CoroutinesServlet, +) : RoutingApiInitializer> { + + override fun initialize(cfg: JavalinConfig, internalRouter: InternalRouter, setup: Consumer>) { + val coroutinesRouting = CoroutinesRouting() + setup.accept(coroutinesRouting) + + coroutinesRouting + .routes + .sortRoutes() + .map { it to Handler { ctx -> servlet.handle(ctx, it) } } + .forEach { (route, handler) -> internalRouter.addHttpHandler(HandlerType.valueOf(route.method.toString()), route.path, handler) } + } + } \ No newline at end of file diff --git a/routing-coroutines/src/test/kotlin/io/javalin/community/routing/CoroutinesRoutingTest.kt b/routing-coroutines/src/test/kotlin/io/javalin/community/routing/CoroutinesRoutingTest.kt index 7db244c..9b2f704 100644 --- a/routing-coroutines/src/test/kotlin/io/javalin/community/routing/CoroutinesRoutingTest.kt +++ b/routing-coroutines/src/test/kotlin/io/javalin/community/routing/CoroutinesRoutingTest.kt @@ -1,17 +1,14 @@ package io.javalin.community.routing import io.javalin.Javalin +import io.javalin.community.routing.coroutines.Coroutines import io.javalin.community.routing.coroutines.servlet.DefaultContextCoroutinesServlet -import io.javalin.community.routing.coroutines.ReactiveRoute -import io.javalin.community.routing.coroutines.ReactiveRoutes -import io.javalin.community.routing.coroutines.reactiveRouting -import io.javalin.http.Context import io.javalin.testtools.JavalinTest +import java.util.concurrent.Executors import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test -import java.util.concurrent.Executors class CoroutinesRoutingTest { @@ -26,22 +23,19 @@ class CoroutinesRoutingTest { assertThat(executor.isShutdown).isTrue } + private val coroutines = Coroutines(DefaultContextCoroutinesServlet(Executors.newSingleThreadExecutor()) { it }) + @Test fun `should properly execute coroutine`() = JavalinTest.test( Javalin.create { config -> - config.reactiveRouting( - DefaultContextCoroutinesServlet(Executors.newSingleThreadExecutor()) { it }, - object : ReactiveRoutes, Context, Unit>() { - override fun routes() = setOf( - reactiveRoute("/test", Route.GET) { - withContext(Dispatchers.IO) { - result(Thread.currentThread().name) - } - } - ) + config.router.mount(coroutines) { + it.route(Route.GET, "/test") { + withContext(Dispatchers.IO) { + result(Thread.currentThread().name) + } } - ) + } } ) { _, client -> assertThat(client.get("/test").body?.string()).contains("DefaultDispatcher") @@ -53,18 +47,13 @@ class CoroutinesRoutingTest { fun `javalin should be able to handle exceptions from coroutines`() = JavalinTest.test( Javalin.create { config -> - config.reactiveRouting( - DefaultContextCoroutinesServlet(Executors.newSingleThreadExecutor()) { it }, - object : ReactiveRoutes, Context, Unit>() { - override fun routes() = setOf( - reactiveRoute("/test", Route.GET) { - withContext(Dispatchers.IO) { - throw TestException() - } - } - ) + config.router.mount(coroutines) { + it.route(Route.GET, "/test") { + withContext(Dispatchers.IO) { + throw TestException() + } } - ) + } }.exception(TestException::class.java) { _, ctx -> ctx.result("Handled") } @@ -76,16 +65,11 @@ class CoroutinesRoutingTest { fun `should execute non-async handlers in regular thread pool`() = JavalinTest.test( Javalin.create { config -> - config.reactiveRouting( - DefaultContextCoroutinesServlet(Executors.newSingleThreadExecutor()) { it }, - object : ReactiveRoutes, Context, Unit>() { - override fun routes() = setOf( - reactiveRoute("/test", Route.GET, async = false) { - result(Thread.currentThread().name) - } - ) + config.router.mount(coroutines) { + it.route(Route.GET, "/test", async = false) { + result(Thread.currentThread().name) } - ) + } } ) { _, client -> assertThat(client.get("/test").body?.string()).contains("JettyServerThreadPool") diff --git a/routing-coroutines/src/test/kotlin/io/javalin/community/routing/examples/ReactiveRoutingExample.kt b/routing-coroutines/src/test/kotlin/io/javalin/community/routing/examples/ReactiveRoutingExample.kt index 143e549..6817219 100644 --- a/routing-coroutines/src/test/kotlin/io/javalin/community/routing/examples/ReactiveRoutingExample.kt +++ b/routing-coroutines/src/test/kotlin/io/javalin/community/routing/examples/ReactiveRoutingExample.kt @@ -1,16 +1,16 @@ package io.javalin.community.routing.examples -import io.javalin.community.routing.coroutines.ReactiveRoutes -import io.javalin.community.routing.Route.GET import io.javalin.Javalin -import io.javalin.community.routing.coroutines.servlet.DefaultContextCoroutinesServlet +import io.javalin.community.routing.Route.GET +import io.javalin.community.routing.coroutines.Coroutines import io.javalin.community.routing.coroutines.ReactiveRoute -import io.javalin.community.routing.coroutines.reactiveRouting +import io.javalin.community.routing.coroutines.ReactiveRoutes +import io.javalin.community.routing.coroutines.servlet.DefaultContextCoroutinesServlet import io.javalin.http.Context -import kotlinx.coroutines.delay import java.lang.Thread.sleep import java.util.concurrent.Executors import java.util.concurrent.atomic.AtomicInteger +import kotlinx.coroutines.delay // Some dependencies class ExampleService { @@ -78,7 +78,9 @@ fun main() { // setup Javalin with reactive routing Javalin .create { config -> - config.reactiveRouting(coroutinesServlet, ExampleEndpoint(exampleService)) + config.router.mount(Coroutines(coroutinesServlet)) { + it.routes(ExampleEndpoint(exampleService)) + } } .events { it.serverStopping { coroutinesServlet.prepareShutdown() } diff --git a/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/DslFactory.kt b/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/DslFactory.kt index 2a31b75..d799fc3 100644 --- a/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/DslFactory.kt +++ b/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/DslFactory.kt @@ -1,6 +1,15 @@ package io.javalin.community.routing.dsl import io.javalin.community.routing.Route +import io.javalin.community.routing.Route.AFTER +import io.javalin.community.routing.Route.BEFORE +import io.javalin.community.routing.Route.DELETE +import io.javalin.community.routing.Route.GET +import io.javalin.community.routing.Route.HEAD +import io.javalin.community.routing.Route.OPTIONS +import io.javalin.community.routing.Route.PATCH +import io.javalin.community.routing.Route.POST +import io.javalin.community.routing.Route.PUT import io.javalin.community.routing.Routes import io.javalin.http.ExceptionHandler import io.javalin.http.Handler @@ -28,42 +37,23 @@ open class RoutingDslConfiguration, CONTEXT, internal val routes = mutableSetOf() internal val exceptionHandlers = mutableMapOf, DslExceptionHandler>() - fun get(path: String, handler: CONTEXT.() -> RESPONSE) = - addRoute(Route.GET, path, handler) - - fun post(path: String, handler: CONTEXT.() -> RESPONSE) = - addRoute(Route.POST, path, handler) - - fun put(path: String, handler: CONTEXT.() -> RESPONSE) = - addRoute(Route.PUT, path, handler) - - fun delete(path: String, handler: CONTEXT.() -> RESPONSE) = - addRoute(Route.DELETE, path, handler) - - fun patch(path: String, handler: CONTEXT.() -> RESPONSE) = - addRoute(Route.PATCH, path, handler) - - fun head(path: String, handler: CONTEXT.() -> RESPONSE) = - addRoute(Route.HEAD, path, handler) - - fun options(path: String, handler: CONTEXT.() -> RESPONSE) = - addRoute(Route.OPTIONS, path, handler) - - fun before(path: String = "", handler: CONTEXT.() -> RESPONSE) = - addRoute(Route.BEFORE, path, handler) - - fun after(path: String = "", handler: CONTEXT.() -> RESPONSE) = - addRoute(Route.AFTER, path, handler) - - @Suppress("UNCHECKED_CAST") - fun addRoutes(routesToAdd: Collection>) { - routesToAdd.forEach { - routes.add(it as ROUTE) - } + fun get(path: String, handler: CONTEXT.() -> RESPONSE) = route(GET, path, handler) + fun post(path: String, handler: CONTEXT.() -> RESPONSE) = route(POST, path, handler) + fun put(path: String, handler: CONTEXT.() -> RESPONSE) = route(PUT, path, handler) + fun delete(path: String, handler: CONTEXT.() -> RESPONSE) = route(DELETE, path, handler) + fun patch(path: String, handler: CONTEXT.() -> RESPONSE) = route(PATCH, path, handler) + fun head(path: String, handler: CONTEXT.() -> RESPONSE) = route(HEAD, path, handler) + fun options(path: String, handler: CONTEXT.() -> RESPONSE) = route(OPTIONS, path, handler) + fun before(path: String = "", handler: CONTEXT.() -> RESPONSE) = route(BEFORE, path, handler) + fun after(path: String = "", handler: CONTEXT.() -> RESPONSE) = route(AFTER, path, handler) + + fun routes(container: DslContainer) = routes(container.routes()) + fun routes(routesToAdd: Collection>) = routesToAdd.forEach { + @Suppress("UNCHECKED_CAST") + routes.add(it as ROUTE) } - - fun addRoute(method: Route, path: String, handler: CONTEXT.() -> RESPONSE) { - addRoutes( + fun route(method: Route, path: String, handler: CONTEXT.() -> RESPONSE) { + routes( listOf( DefaultDslRoute( method = method, @@ -79,7 +69,6 @@ open class RoutingDslConfiguration, CONTEXT, if (exceptionHandlers.containsKey(type)) { throw IllegalArgumentException("Exception handler for type ${type.simpleName} is already registered") } - exceptionHandlers[type] = handler as DslExceptionHandler } diff --git a/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/DslRouting.kt b/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/DslRouting.kt index f1cc789..7b8fad1 100644 --- a/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/DslRouting.kt +++ b/routing-dsl/src/main/kotlin/io/javalin/community/routing/dsl/DslRouting.kt @@ -37,4 +37,5 @@ open class DslRouting< internalRouter.addHttpExceptionHandler(exceptionClass.java, factory.createExceptionHandler(handler)) } } + } \ No newline at end of file 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 46496d2..da0f21c 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 @@ -26,10 +26,7 @@ open class DefaultContextScopeConfiguration< > : RoutingDslConfiguration() { @OptIn(ExperimentalStdlibApi::class) - inline fun RoutingDslConfiguration.method( - method: Route, - crossinline handler: (CONTEXT, PATH) -> RESPONSE - ) { + inline fun method(method: Route, crossinline handler: (CONTEXT, PATH) -> RESPONSE) { val path = PATH::class.findAnnotation() ?.path ?: throw IllegalArgumentException("@Path annotation not found") @@ -72,31 +69,14 @@ open class DefaultContextScopeConfiguration< } } - inline fun RoutingDslConfiguration.get(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = - method(Route.GET, handler) - - inline fun RoutingDslConfiguration.post(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = - method(Route.POST, handler) - - inline fun RoutingDslConfiguration.put(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = - method(Route.PUT, handler) - - inline fun RoutingDslConfiguration.delete(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = - method(Route.DELETE, handler) - - inline fun RoutingDslConfiguration.patch(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = - method(Route.PATCH, handler) - - inline fun RoutingDslConfiguration.head(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = - method(Route.HEAD, handler) - - inline fun RoutingDslConfiguration.options(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = - method(Route.OPTIONS, handler) - - inline fun RoutingDslConfiguration.before(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = - method(Route.BEFORE, handler) - - inline fun RoutingDslConfiguration.after(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = - method(Route.AFTER, handler) + inline fun get(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.GET, handler) + inline fun post(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.POST, handler) + inline fun put(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.PUT, handler) + inline fun delete(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.DELETE, handler) + inline fun patch(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.PATCH, handler) + 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 after(crossinline handler: CONTEXT.(PATH) -> RESPONSE) = method(Route.AFTER, 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 759181e..73c6698 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,18 +21,16 @@ class InPlaceRoutingDslTest : TestSpecification() { Javalin.create { config -> config.router.mount(Dsl) { it.before("/before") { header("test", "before") } - after("/after") { header("test", "after") } - - get("/throwing") { throw RuntimeException() } - exception(Exception::class) { header("exception", it::class.java.name) } - - get("/get") { header("test", "get") } - post("/post") { header("test", "post") } - put("/put") { header("test", "put") } - patch("/patch") { header("test", "patch") } - delete("/delete") { header("test", "delete") } - head("/head") { header("test", "head") } - options("/options") { header("test", "options") } + it.after("/after") { header("test", "after") } + it.get("/throwing") { throw RuntimeException() } + it.exception(Exception::class) { header("exception", it::class.java.name) } + it.get("/get") { header("test", "get") } + it.post("/post") { header("test", "post") } + it.put("/put") { header("test", "put") } + it.patch("/patch") { header("test", "patch") } + it.delete("/delete") { header("test", "delete") } + it.head("/head") { header("test", "head") } + it.options("/options") { header("test", "options") } } }, defaultConfig @@ -77,8 +75,8 @@ class InPlaceRoutingDslTest : TestSpecification() { fun `should properly handle class based route`() = JavalinTest.test( // given: a javalin app with routes defined using the dsl Javalin.create { config -> - config.routing { - get { result("Panda") } + config.router.mount(Dsl) { + it.get { result("Panda") } } }, defaultConfig @@ -97,8 +95,8 @@ class InPlaceRoutingDslTest : TestSpecification() { fun `should properly handle class based route with parameter`() = JavalinTest.test( // given: a javalin app with routes defined using the dsl Javalin.create { config -> - config.routing { - get { result(it.name) } + config.router.mount(Dsl) { + it.get { result(it.name) } } }, defaultConfig @@ -117,9 +115,9 @@ class InPlaceRoutingDslTest : TestSpecification() { // given: a javalin app with routes defined using the dsl val app = ThrowingCallable { Javalin.create { - it.routing { + it.router.mount(Dsl) { // when: a route is defined with path without @Path annotation - get { } + it.get { } } } } @@ -138,9 +136,9 @@ class InPlaceRoutingDslTest : TestSpecification() { // given: a javalin app with routes defined using the dsl val app = ThrowingCallable { Javalin.create { - it.routing { + it.router.mount(Dsl) { // when: a route is defined with invalid @Path annotation - get { } + it.get { } } } } @@ -158,16 +156,16 @@ class InPlaceRoutingDslTest : TestSpecification() { fun `should properly map all reified variants`() = JavalinTest.test( // given: a javalin app with routes defined by all available reified methods Javalin.create { config -> - config.routing { - before { result("Before ") } - get { result(result() + "GET") } - put { result(result() + "PUT") } - post { result(result() + "POST") } - patch { result(result() + "PATCH") } - delete { result(result() + "DELETE") } - head { result(result() + "HEAD") } - options { result(result() + "OPTIONS") } - after { + config.router.mount(Dsl) { + it.before { result("Before ") } + it.get { result(result() + "GET") } + it.put { result(result() + "PUT") } + it.post { result(result() + "POST") } + it.patch { result(result() + "PATCH") } + it.delete { result(result() + "DELETE") } + it.head { result(result() + "HEAD") } + it.options { result(result() + "OPTIONS") } + it.after { result(result() + " After") header("test", result()!!) } @@ -176,7 +174,8 @@ class InPlaceRoutingDslTest : TestSpecification() { defaultConfig ) { _, client -> // when: a request is made to the http route - Route.values() + Route.entries + .asSequence() .filter { it.isHttpMethod } .map { it to request(it.name, "${client.origin}/path").asString() } .forEach { (method, response) -> diff --git a/routing-dsl/src/test/kotlin/io/javalin/community/routing/dsl/PropertyRoutingDslTest.kt b/routing-dsl/src/test/kotlin/io/javalin/community/routing/dsl/PropertyRoutingDslTest.kt index d08e884..b39da8c 100644 --- a/routing-dsl/src/test/kotlin/io/javalin/community/routing/dsl/PropertyRoutingDslTest.kt +++ b/routing-dsl/src/test/kotlin/io/javalin/community/routing/dsl/PropertyRoutingDslTest.kt @@ -2,6 +2,7 @@ package io.javalin.community.routing.dsl import io.javalin.Javalin import io.javalin.community.routing.Route.GET +import io.javalin.community.routing.dsl.DslRouting.Companion.Dsl import io.javalin.community.routing.dsl.defaults.DefaultRoutes import io.javalin.community.routing.dsl.specification.TestSpecification import io.javalin.testtools.JavalinTest @@ -27,7 +28,9 @@ class PropertyRoutingDslTest : TestSpecification() { fun `should register valid route in javalin instance`() = JavalinTest.test( Javalin.create { - it.routing(ValidTestEndpoints()) + it.router.mount(Dsl) { + it.routes(ValidTestEndpoints()) + } }, defaultConfig ) { _, client -> diff --git a/routing-dsl/src/test/kotlin/io/javalin/community/routing/dsl/examples/InPlaceExample.kt b/routing-dsl/src/test/kotlin/io/javalin/community/routing/dsl/examples/InPlaceExample.kt index 3c8a578..db3ae8c 100644 --- a/routing-dsl/src/test/kotlin/io/javalin/community/routing/dsl/examples/InPlaceExample.kt +++ b/routing-dsl/src/test/kotlin/io/javalin/community/routing/dsl/examples/InPlaceExample.kt @@ -1,15 +1,15 @@ package io.javalin.community.routing.dsl.examples import io.javalin.Javalin -import io.javalin.community.routing.dsl.defaults.DefaultContextScopeConfiguration -import io.javalin.community.routing.dsl.defaults.DefaultDsl.DefaultScope import io.javalin.community.routing.dsl.DslExceptionHandler import io.javalin.community.routing.dsl.DslRoute -import io.javalin.community.routing.dsl.defaults.Path +import io.javalin.community.routing.dsl.DslRouting import io.javalin.community.routing.dsl.RoutingDslFactory -import io.javalin.community.routing.dsl.examples.CustomDsl.CustomScope +import io.javalin.community.routing.dsl.defaults.DefaultContextScopeConfiguration +import io.javalin.community.routing.dsl.defaults.DefaultDsl.DefaultScope +import io.javalin.community.routing.dsl.defaults.Path import io.javalin.community.routing.dsl.examples.CustomDsl.CustomRoutingConfiguration -import io.javalin.community.routing.dsl.routing +import io.javalin.community.routing.dsl.examples.CustomDsl.CustomScope import io.javalin.http.Context import io.javalin.http.ExceptionHandler import io.javalin.http.Handler @@ -44,20 +44,20 @@ data class PandaPath(val age: Int) fun main() { Javalin.create { config -> - config.routing(CustomDsl) { - before { + config.router.mount(DslRouting(CustomDsl)) { + it.before { // `endpointHandlerPath` comes from Context class result("Called endpoint: ${matchedPath()}") } - get("/") { + it.get("/") { // `helloWorld` comes from CustomScope class result(helloWorld()) } - get { path -> + it.get { path -> // support for type-safe paths result(path.age.toString()) } - exception(Exception::class) { anyException -> + it.exception(Exception::class) { anyException -> // support for exception handlers result(anyException.message ?: "Unknown error") }