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

why

02/08/2021, 10:34 AM
hi.. how can I make a get request throw
ClientRequestException
in my unit tests? is it possible? I’m using the MockEngine like this
Copy code
val httpClientMock = HttpClient(MockEngine) {
            HttpResponseValidator { }
            install(JsonFeature) {
                serializer = GsonSerializer()
            }
            engine {
                addHandler { request ->
                    val page = request.url.parameters["page"]!!.toInt()

                    when {
                        page < 38 -> {
                            val t = ContentType.Application.Json.toString()
                            val headers = headersOf("Content-Type" to listOf(t))
                            TextContent(reposJson, ContentType.Application.Json)
                            respond(reposJson, headers = headers)
                        }
                        else -> throw "I can't ctor the exception"
                    }

                }
            }
        }
r

rsetkus

02/08/2021, 6:49 PM
Use
respondError(HttpStatusCode.BadRequest, reposJson, headers)
. Change status code to whatever you need based on your use case.
💯 1
w

why

02/09/2021, 10:44 AM
thanks for you help 😄
80 Views