While testing redirects on Ktor client, i got `Sen...
# ktor
c
While testing redirects on Ktor client, i got
SendCountException
. Code in đź§µ
Copy code
@Test
    fun getDownloadUrl_return_validUrl() = runTest {
        val engine = MockEngine{
            respondRedirect(location = "url")
        }
        val client = ApiClient(engine)
        val expected = client.getDownloadUrl("/get-download")
        assertEquals(expected,"url")
    }
a
Do you mean
SendCountExceedException
?
That’s because a “mocked server” responds with a redirect for every request and since the client follows redirects by default it tries to send requests until the limit (20 by default) is reached.
c
Oh okay. How can I avoid that??
a
You can respond with a redirect depending on the request. Here is an example: ``````
Copy code
val client = HttpClient(MockEngine) {
    engine {
        addHandler { request ->
            if (request.url.encodedPath == "/redirect") {
                respondRedirect(location = "/url")
            } else if (request.url.encodedPath == "/url") {
                respond("url")
            } else {
                error("Don't know how to respond")
            }
        }
    }
}

val r = client.get("/redirect")
assertEquals("url", r.bodyAsText())
👍 1