Ktor: withTestApplication sendPipeline interceptors do not work

Created on 10 Jan 2020  路  15Comments  路  Source: ktorio/ktor

Ktor Version and Engine Used (client or server and name)
1.2.6, 1.3.0-rc2

Describe the bug
Any sendPipeline interceptor installed does not get called when testing using withTestApplication.

To Reproduce
Steps to reproduce the behavior:
Install a sendPipeline interceptor an try to use it with a handleRequest. The interception lambda will not be called.

Expected behavior
The interception lambda to be called.

You can check out the test code here in this feature of mine where I'm having the issue. A run of the tests is also available here on TravisCI and the corresponding build scan here.

ux

All 15 comments

Just added a test and it works well

@Test
    fun testSendPipeline(): Unit = withTestApplication {
        var processed = false

        application.sendPipeline.intercept(ApplicationSendPipeline.Before) {
            processed = true
        }

        application.intercept(ApplicationCallPipeline.Call) {
            call.respondText("OK")
        }

        assertEquals("OK", handleRequest(HttpMethod.Get, "/").response.content)
        assertTrue(processed)
    }

Test passes

With the after phase works as well

I have no idea why my tests do not pass then. The response is null and the CallLogging says the request is unhandled:

[DefaultDispatcher-worker-1 @request#1] DEBUG ktor.test - Unhandled: GET - /test

Well, then the send pipeline is not executed because no response was sent

Yes, but wouldn't Ktor automatically respond with an empty body and 404 code? Because I need to intercept all 404s.

No, otherwise there will be no "unhandled"

However, it's easy to add a fallback call interceptor that produces 404 if unhandled.

May I ask if you could you show an example?

Also, when running the Apache engine it does respond with 404s, which is why the previous tests that were using a local server were working. Why such difference from the test engine?

This difference makes me wonder if I should handle the unhandled pages in the feature itself or just in the test!

Something like this

application.intercept(ApplicationCallPipeline.Fallback) {
    if (call.response.status() == null) {
        call.respond(HttpStatusCode.NotFound)
        finish()
    }
}

May I ask if you are addressing this inconsistency?

I'm almost ready to make that decision :)

This couldn't be integrated before 1.4.0 because this is breaking change

Oh well... I'll just wait then! Thanks :)

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

Fixed in 1.4.0

Was this page helpful?
0 / 5 - 0 ratings