https://kotlinlang.org logo
Title
d

dodalovic

05/10/2022, 5:48 AM
Hi, I'm trying to use
externalServices
mocking with the
2.0.1
ktor version, but it doesn't kick in:
externalServices {
        hosts("<https://api.sendinblue.com>") {
            install(ContentNegotiation) {
                json()
            }
            routing {
                post("v3/smtp/email") {
                    call.response.status(HttpStatusCode.OK)
                }
            }
        }
    }
and then I call the API:
val response = <http://httpClient.post|httpClient.post>("<https://api.sendinblue.com/v3/smtp/email>") {
            accept(ContentType.Application.Json)
            contentType(ContentType.Application.Json)
            header("api-key", apiKey)
But this mocking doesn't kick-in, rather the real thing.
a

Aleksei Tirman [JB]

05/10/2022, 8:38 AM
The following test passes:
@Test
fun test() = testApplication {
    externalServices {
        hosts("<https://api.sendinblue.com>") {
            install(ContentNegotiation) {
                json()
            }
            routing {
                post("v3/smtp/email") {
                    call.response.status(HttpStatusCode.OK)
                }
            }
        }
    }

    val response = <http://client.post|client.post>("<https://api.sendinblue.com/v3/smtp/email>") {
        header("api-key", "123")
    }

    assertEquals(HttpStatusCode.OK, response.status)
}
Could you please share a complete test to reproduce your problem?
d

dodalovic

05/10/2022, 9:06 AM
What is the prerequisite for this magic to work? Do my tests need to use the same HTTP client as my production code or?
I'm calling the
setup
at the beginning of my @Test methods
a

Aleksei Tirman [JB]

05/10/2022, 9:11 AM
Inside the
testApplication
you have the
client
property that you can use for testing. Also, you can create a new client with
createClient
method. My test passes with your
setup
method too.
d

dodalovic

05/10/2022, 9:14 AM
Jesus 🙂
a

Aleksei Tirman [JB]

05/10/2022, 9:17 AM
Is your issue resolved or not?
d

dodalovic

05/10/2022, 9:30 AM
nope
I updated the ticket with more info