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

Archie

06/08/2020, 2:56 PM
Hi guys, I have a Ktor
HttpClient
set like so:
Copy code
HttpClient(engine) {
    defaultRequest {
        header("A Header", headerValue)
    }
}
I wanted to add a test to that ensures that everytime I make a request to the
HttpClient
it would add the stated header. Is there a way to do this test?
h

Hamza

06/08/2020, 3:04 PM
have u tried reading the tests written for the client and see how they test headers?
a

andev

06/08/2020, 3:04 PM
Copy code
assertNotNull(call.request.header("A Header"))
nevermind, didn't see u asked about client
But honestly, you should test only your code. By using the Client, u already trust its developers that they did the good job.
1
☝️ 1
m

marstran

06/08/2020, 3:11 PM
@andev He doesn't really test the HttpClient though. I think he wants to test that his own code adds that header.
❤️ 1
r

russhwolf

06/08/2020, 3:11 PM
Pass
MockEngine
to your client. You can read the headers from the request when you construct your mock response
❤️ 1
a

andev

06/08/2020, 3:16 PM
Yep, just as Russell said
Copy code
@Test
fun test() {
    val client = HttpClient(MockEngine) {
        expectSuccess = false
        engine {
            addHandler { request ->
                assertNotNull(request.headers.get("A Header"))
                respondOk("REPLACE WITH YOUR MOCK RESPONSE")
            }
        }
    }
}
❤️ 1
a

Archie

06/08/2020, 3:24 PM
@andev YES! thats what I need thank you very much!!
m

Matteo Mirk

06/08/2020, 4:49 PM
Still, I’m convinced that if you setup the client like that, you’re already trusting Ktor to do its job, which is already covered by their test suite. I my modest opinion I think testing for the presence of your header in this setting is redundant like testing that an Apache HttpClient initialized such and such stays the same for every request, or that every
@Autowired
usage works in your spring application.
👍 1
👎 1
a

Archie

06/09/2020, 1:39 PM
I honestly disagree.. The test I want to write isn't testing Ktor itself but instead Testing the setup I made. So if someone in the project messed up and accidentally delete some setup, (like the header above) the test would easily tell me what to fix or what was missing. Its not about whether Ktor would add the header that I specified. Its for reflecting the setup of the app. Not Ktor.
👍 1
m

Matteo Mirk

06/09/2020, 2:20 PM
Cool… just maybe a thumb down is a little too harsh for having expressed an opinion? Anyway, regarding this matter, probably I didn’t express myself good enough. The fact that you want to test the header setup for your app, is still indirectly testing Ktor. Quoting your initial message:
I wanted to add a test to that ensures that everytime I make a request to the 
HttpClient
 it would add the stated header.
if this is not exactly testing that Ktor works as designed, I don’t know what else it is. 😄 If someone in the project accidentally deletes the setup then you have a bigger problem than a functional requirement… Maybe your test focus should be shifted, not checking the http client setup but rather the receiving point should test for its required headers. Anyway, I can see your initial point, just wanted to offer a different test design perspective, sorry if I wasn’t clear before.
5 Views