Archie
06/08/2020, 2:56 PMHttpClient
set like so:
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?Hamza
06/08/2020, 3:04 PMandev
06/08/2020, 3:04 PMassertNotNull(call.request.header("A Header"))
marstran
06/08/2020, 3:11 PMrusshwolf
06/08/2020, 3:11 PMMockEngine
to your client. You can read the headers from the request when you construct your mock responseandev
06/08/2020, 3:16 PM@Test
fun test() {
val client = HttpClient(MockEngine) {
expectSuccess = false
engine {
addHandler { request ->
assertNotNull(request.headers.get("A Header"))
respondOk("REPLACE WITH YOUR MOCK RESPONSE")
}
}
}
}
Archie
06/08/2020, 3:24 PMMatteo Mirk
06/08/2020, 4:49 PM@Autowired
usage works in your spring application.Archie
06/09/2020, 1:39 PMMatteo Mirk
06/09/2020, 2:20 PMI wanted to add a test to that ensures that everytime I make a request to theif 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.it would add the stated header.HttpClient