https://kotlinlang.org logo
#ktor
Title
# ktor
c

CLOVIS

05/29/2021, 9:32 AM
Does
withTestApplication
ignore authentication? It accesses an endpoint behind
authenticate()
without any specific setup
r

Rohde Fischer

05/29/2021, 9:34 AM
I'm answering based on vague memories, so I might be wrong if I recall right it shouldn't. I'd check if you get the application injected correctly and if auth even works as intended via curl or similar. I'd find it worrying if tests doesn't include auth
c

CLOVIS

05/29/2021, 9:43 AM
Running tests from clients gets a 401 Unauthorized. The endpoint doesn't have any code at the moment (it is just declared). Testing it with
withTestApplication
gives null, but no error.
r

Rohde Fischer

05/29/2021, 9:51 AM
oh, I can see that I tore part of the bootstrapping apart in ktor, either due to the mess that their gross misuse of extension functions create (I'm borderline concluding that extension methods is an anti pattern due to ktor, it caused us so much pain, because it blocks extensibility and good designs for DI etc) I can't share all, since it's client code, but this might get you started
Copy code
val testEnvironment: () -> ApplicationEngineEnvironment
    fun withTestApplication(
        method: HttpMethod,
        uri: String,
        applicationRequest: (TestApplicationRequest.() -> Unit) = {},
        testSetup: (TestApplicationCall.() -> Unit)
    ) {
        withApplication(testEnvironment()) {
            application.publikum()

            with(
                handleRequest(method, uri, applicationRequest),
                testSetup
            )
        }
    }
I think you can use the raw test environment for your purposes, we override it to simulate various configs from
application.conf
Copy code
testEnvironment = {
    createTestEnvironment {
        config = (config as MapApplicationConfig).let {
      it.put("foo.bar", "value")
            it
        }
    }
}
I hope it can get you started, even though it's bit's and pieces, I'm in the middle of an urgent issue, so can't dive much deeper right now 😞
c

CLOVIS

05/29/2021, 10:43 AM
Thanks a lot, I'll look into that 👍
7 Views