Hi folks. Let’s say I have a string representing a...
# ktor
j
Hi folks. Let’s say I have a string representing a path “/some/path”. I’d like to know if a route has been defined for that path. Is there a way to search and see if a route matching that path has already been defined?
c
a
You can subscribe to an application startup event and there, probe the resolution of a specific request by the routing. In order to run the following example, you need
implementation("io.ktor:ktor-server-test-host:$ktor_version")
dependency:
Copy code
embeddedServer(Netty, port = 9090) {
    environment.monitor.subscribe(ApplicationStarted) { app ->
        val routing = app.feature(Routing)
        val call = TestApplicationCall(app, coroutineContext = EmptyCoroutineContext)
        // The request we want to probe
        call.request.method = HttpMethod.Get
        call.request.uri = "/some"

        val context = RoutingResolveContext(routing, call, emptyList())
        if (context.resolve() is RoutingResolveResult.Success) {
            println("Does exist")
        }
    }

    routing {
        get("/some") {
            call.respondText { "Hello" }
        }
    }
}.start(wait = true)
j
Thanks @crummy and @Aleksei Tirman [JB] for the helpful answers.