Hi! I am testing the <https://ktor.io/docs/server-...
# ktor
m
Hi! I am testing the https://ktor.io/docs/server-testing.html for spinning up a testapplication. When i try to make a restassured test against my api, restassured need a port. How can I fetch the port from
TestApplication
? My restassured test:
Copy code
val response =
    RestAssured.given()
        .filter(validationFilter)
        .header(HttpHeaders.ContentType, APPLICATION_JSON)
        .header(HttpHeaders.Authorization, "Bearer ${mockOAuth2Server.token()}")
        .body(OppdragsInfoRequest(gjelderId = "12345678901", fagGruppeKode = "ABC"))
        .port(PORT)
        .post("$OPPDRAGSINFO_BASE_API_PATH/oppdragsinfo")
        .then()
        .assertThat()
        .statusCode(HttpStatusCode.OK.value)
        .extract()
        .response()
Any my
TestApplicationListener
(using kotest):
Copy code
object EmbeddedTestServerListener : TestListener {
    val oppdragsInfoService = mockk<OppdragsInfoService>()
    val integrationService = mockk<IntegrationService>()

    var authorization by Delegates.notNull<Boolean>()
    val mockOAuth2Server = MockOAuth2Server()

    lateinit var testApplication: TestApplication

    operator fun invoke(authorization: Boolean): TestListener {
        this.authorization = authorization
        return this
    }

    override suspend fun beforeSpec(spec: Spec) {
        mockOAuth2Server.start()
        this.testApplication =
            TestApplication {
                application {
                    myApplicationModule()
                }
            }
        testApplication.start()
    }

    override suspend fun afterSpec(spec: Spec) {
        mockOAuth2Server.shutdown()
    }
}

private fun Application.myApplicationModule() {
    commonConfig()
    if (authorization) {
        securityConfig(true, mockOAuth2Server.mockAuthConfig())
    }
    routing {
        authenticate(authorization, AUTHENTICATION_NAME) {
            integrationApi(integrationService)
            oppdragsInfoApi(oppdragsInfoService)
        }
    }
}
When running test I get following output:
Copy code
15:56:40 [] [ker-1] INFO ktor.test
                : Responding at <http://localhost:80>
15:56:40 [] [ker-1] INFO ktor.test
                : Responding at <https://localhost:443>

Connection refused
java.net.ConnectException: Connection refused
The
PORT
is a constant variable set to
80
just to check if I can response but I get connection refused. Or I cant use TestApplication in my case? do i need to spin up a EmbeddedServer when doing Restassured tests ?
a
The
testApplication
doesn't use a network for testing, so the test server doesn't listen for connections, and the test client doesn't establish TCP/IP connections. As far as I understand, REST Assured requires an HTTP server listening for connections to test it so you can't use the
testApplication
with REST Assured.
👍 1