Hi all :wave: I'm working on a 3+ year old project...
# ktor
m
Hi all 👋 I'm working on a 3+ year old project. I'm keen to upgrade it to Kotlin v2 and Ktor to v3. But there's some test that I've inherited that are broken due to this change https://ktor.io/docs/migrating-3.html#override-the-default-limit "the
handleRequest
function is replaced with the
client.get
"
. As I understand it the new pattern is pretty much like booting up the app which exposes the endpoint then you call it with a HTTP client as a test. The old pattern allowed you to "peek" into the Ktor application server. The tests I've inherited used this "peeking" ability to check that the cookie + session management is working as expected. This is because our application has used the Ktor Session/Cookie plugins to wire in Pac4j library. The Pac4j code seems to implement a bunch of interfaces. So the tests are proving that everything is wired up correctly. The test assertions all relied on having access to the ApplicationCall object (returned via
handleRequest
) but now we don't have access to it. Is there any "backdoor" that I can peek into the spun up Ktor application and intercept the request to get the ApplicationCall so that I can avoid having to refactor these tests? Long term I plan to refactor them to test the behaviour of the API but it's a huge task and something I can't tackle now.
r
not sure if this would work for you, but I believe when you're using the testApplication helper from ktor server, you can install additional plugins just for testing. You might be able to install a custom plugin that intercepts the call and captures that state in some test global for inspection?
m
Interest idea, I'll try find the relevant docs and give it a whirl.
Thanks
a
You can access the
ApplicationCall
object from the
HttpResponse
. For example:
Copy code
val response = client.get("/")
val call = response.call
m
@Aleksei Tirman [JB] that's a different object, that's
HttpClientCall
@RJ Garcia your idea is looking promising. I've got a bunch of other compile errors but this looks to be workable. I'll confirm in time.
Copy code
var callSpy: ApplicationCall? = null
    testApplication {
        application {
            install(createApplicationPlugin(name="requestSpy") {
                onCall { callSpy = it }
            })
        }
        runBlocking {
            client.get(uri, requestModifier)
            callSpy!!.responseTest()
        }
    }
💪 1
a
Can you please share an example test?