dave08
01/27/2025, 10:45 AMdave08
01/27/2025, 10:47 AMval client = createClient {
install(ContentNegotiation) { json() }
defaultRequest {
host = "0.0.0.0"
port = 8080
}
}
for the client.dave08
01/27/2025, 10:48 AMlocalPort
for internal requestsAleksei Tirman [JB]
01/27/2025, 10:49 AMtestApplication
doesn't listen for connections, but instead, it uses its internal client to forward the requests to the test server.dave08
01/27/2025, 10:50 AMdave08
01/27/2025, 10:50 AMdave08
01/27/2025, 10:51 AMAleksei Tirman [JB]
01/27/2025, 10:52 AMdave08
01/27/2025, 10:54 AMfun testApp(
token: String = "",
repository: AccountRepository? = null,
block: suspend TestContext.(HttpClient) -> Unit
) = testApplication {
val deviceStateInRequest = Arb.deviceState()()
val processSyncRequestFake = ProcessSyncRequestFake()
application {
configureSerialization()
repository?.let { internalRoutes(it) }
externalRoutes(processSyncRequestFake, token)
}
val client = createClient {
install(ContentNegotiation) { json() }
defaultRequest {
host = "0.0.0.0"
port = 8080
}
}
TestContext(this, deviceStateInRequest, processSyncRequestFake).block(client)
}
...
it("internally retrieves list of foo") {
testApp(repository = accountRepo) {
val result = it.get("/foo") {
host = "0.0.0.0"
port = 8090
}.body<JsonElement>()
...
dave08
01/27/2025, 10:55 AMCan not resolve request to <http://0.0.0.0:8090>. Main app runs at localhost:80, localhost:443 and external services are
io.ktor.server.testing.client.InvalidTestRequestException: Can not resolve request to <http://0.0.0.0:8090>. Main app runs at localhost:80, localhost:443 and external services are
at io.ktor.server.testing.client.DelegatingTestClientEngine.execute(DelegatingTestClientEngine.kt:62)
at io.ktor.server.testing.client.DelegatingTestClientEngine$execute$1.invokeSuspend(DelegatingTestClientEngine.kt)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:100)
at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:113)
at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:89)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:586)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:820)
dave08
01/27/2025, 10:57 AMdave08
01/27/2025, 10:58 AMAleksei Tirman [JB]
01/27/2025, 11:25 AMdave08
01/27/2025, 11:34 AMimport io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
class KtorTest {
@Test
fun test() = testApplication {
application {
this.routing {
localPort(8080) {
get("/") {
call.respondText("Hello, world!")
}
}
}
}
val client = createClient {
defaultRequest {
host = "0.0.0.0"
port = 8080
}
}
val result = client.get("/")
assertEquals("Hello, world!", result.bodyAsText())
}
}
dave08
01/27/2025, 11:35 AM2025-01-27 13:34:25.217 [Thread-14] INFO org.eclipse.jetty.server.Server - Stopped Server@43e065f2{STOPPING}[11.0.15,sto=1000]
2025-01-27 13:34:25.218 [Thread-14] INFO org.eclipse.jetty.server.Server - Shutdown Server@43e065f2{STOPPING}[11.0.15,sto=1000]
2025-01-27 13:34:25.229 [Thread-14] INFO o.e.jetty.server.AbstractConnector - Stopped NetworkTrafficServerConnector@b835727{HTTP/1.1, (http/1.1, h2c)}{0.0.0.0:45891}
2025-01-27 13:34:25.231 [Thread-14] INFO o.e.j.server.handler.ContextHandler - Stopped o.e.j.s.ServletContextHandler@4b4ee511{/,null,STOPPED}
2025-01-27 13:34:25.231 [Thread-14] INFO o.e.j.server.handler.ContextHandler - Stopped o.e.j.s.ServletContextHandler@58b91d57{/__admin,null,STOPPED}
2025-01-27 13:34:25.310 [DefaultDispatcher-worker-3 @call-context#4] INFO io.ktor.test - No ktor.deployment.watch patterns specified, automatic reload is not active.
2025-01-27 13:34:25.330 [DefaultDispatcher-worker-3 @call-context#4] INFO io.ktor.test - Application started in 0.031 seconds.
2025-01-27 13:34:25.332 [DefaultDispatcher-worker-1 @coroutine#5] INFO io.ktor.test - Responding at <http://localhost:80>
2025-01-27 13:34:25.332 [DefaultDispatcher-worker-1 @coroutine#5] INFO io.ktor.test - Responding at <https://localhost:443>
Can not resolve request to <http://0.0.0.0:8080>. Main app runs at localhost:80, localhost:443 and external services are
io.ktor.server.testing.client.InvalidTestRequestException: Can not resolve request to <http://0.0.0.0:8080>. Main app runs at localhost:80, localhost:443 and external services are
at io.ktor.server.testing.client.DelegatingTestClientEngine.execute(DelegatingTestClientEngine.kt:62)
at io.ktor.server.testing.client.DelegatingTestClientEngine$execute$1.invokeSuspend(DelegatingTestClientEngine.kt)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:100)
at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:113)
at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:89)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:586)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:820)
dave08
01/27/2025, 11:35 AMdave08
01/27/2025, 11:45 AMfun ApplicationEngine.Configuration.envConfig() {
connector {
host = "0.0.0.0"
port = 8080
}
connector {
host = "0.0.0.0"
port = 8090
}
}
in the engine { envConfig() }
block... I'm wondering if it really considers that, taking into account what you said that the test application doesn't actually listen on any ports... I didn't see a word of all this mentioned in the testing docs, it would really be nice if it would be there.dave08
01/27/2025, 11:46 AM