ktorio / ktor-init-tools

ktor project generator web page and IDEA plugin
https://start.ktor.io
Other
40 stars 20 forks source link

fix for #12, not tested tough (via web) #13

Closed robstoll closed 5 years ago

robstoll commented 5 years ago

I have no idea in which version the change was introduced, maybe I also made a mistake in translating the code into the generation (only have not tested it, inserted it via web form). In the end the testClientMock function should look as follows (that one is tested)

@Test
fun testClientMock() {
    runBlocking {
        val client = HttpClient(MockEngine) {
            engine {
                addHandler { request ->
                    @UseExperimental(InternalAPI::class)
                    when (request.url.encodedPath) {
                        "/" -> respond(
                            ByteReadChannel(byteArrayOf(1, 2, 3)),
                            headers = headersOf("X-MyHeader", "MyValue")
                        )
                        else -> respond("Not Found ${request.url.encodedPath}", HttpStatusCode.NotFound)
                    }
                }
            }
            expectSuccess = false
        }
        assertEquals(byteArrayOf(1, 2, 3).toList(), client.get<ByteArray>("/").toList())
        assertEquals("MyValue", client.call("/").response.headers["X-MyHeader"])
        assertEquals("Not Found other/path", client.get<String>("/other/path"))
    }
}

Notice the @UseExperimental(InternalAPI::class) is this really the only way?

cy6erGn0m commented 5 years ago

Nice catch, thanks!

robstoll commented 5 years ago

Actually, looking at the code I wrote, it is not ideal either, using an experimental feature was a mistake I made (can be changed to that once it isn't experimental anymore). I create another pull request #16