Hi, I'm trying to use `externalServices` mocking w...
# ktor
d
Hi, I'm trying to use
externalServices
mocking with the
2.0.1
ktor version, but it doesn't kick in:
Copy code
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:
Copy code
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
The following test passes:
Copy code
@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
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
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
Jesus 🙂
a
Is your issue resolved or not?
d
nope
I updated the ticket with more info
142 Views