Hello. Ktor/Kotlin newbie here. I'm writing a clie...
# ktor
g
Hello. Ktor/Kotlin newbie here. I'm writing a client for an API and my client is wrapping Ktor in such a way that I can replace its engine with a
MockEngine
for testing. However, I'm finding myself duplicating the config (e.g.,
install(...)
) across the two clients because I couldn't find a way to define it once. Any pointers on how one may do that?
I also tried something like this but it doesn't seem to have made any difference:
Copy code
val mockClient = HttpClient(MockEngine) {
        engine {
            addHandler(...)
        }
        install(originalClient)
    }
c
Which engine are you using? If it’s OkHttp, you could take advantage of MockWebServer: https://github.com/square/okhttp/tree/master/mockwebserver
g
Thanks Chris! I'm using OkHTTP but I already have a test suite written with
MockEngine
so I wouldn't like to rewrite it to use MockWebServer from OkHTTP. Anyway, I've manage to achieve what I wanted by making the engine in
HttpClient(...)
variable (OkHTTP in prod, MockEngine in the unit tests).