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

Gus

09/08/2020, 4:33 PM
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

Chris Fillmore

09/08/2020, 6:13 PM
Which engine are you using? If it’s OkHttp, you could take advantage of MockWebServer: https://github.com/square/okhttp/tree/master/mockwebserver
g

Gus

09/08/2020, 6:41 PM
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).