https://kotlinlang.org logo
#ktor
Title
# ktor
d

dave08

03/30/2023, 9:35 AM
Hi! What could this error mean? I have the connectors set up and everything... (this is in unit tests using testApplication and the environment block to set the connectors...)
Copy code
Can not resolve request to <http://localhost:8080>. Main app runs at 0.0.0.0:8080, 127.0.0.1:8090 and external services are 
io.ktor.server.testing.client.InvalidTestRequestException: Can not resolve request to <http://localhost:8080>. Main app runs at 0.0.0.0:8080, 127.0.0.1:8090 and external services are 
	at app//io.ktor.server.testing.client.DelegatingTestClientEngine.execute(DelegatingTestClientEngine.kt:59)
	at app//io.ktor.client.engine.HttpClientEngine$executeWithinCallContext$2.invokeSuspend(HttpClientEngine.kt:99)
a

Aleksei Tirman [JB]

03/30/2023, 9:39 AM
What request causes this error?
d

dave08

03/30/2023, 9:43 AM
I have this:
Copy code
fun testApp(
    token: String = "",
 ....
) = testApplication {
    environment {
        envConfig {
            configureSerialization()
            // This has a localPort(8090) { ... } in it
            internalRoutes(...)
            // This doesn't have any port()... block in it... 8080 should be the default...? But maybe I should add it... but the error doesn't say the path wasn't found... it returns that funny error
            externalRoutes(...)
        }
    }

    val client = createClient {
        install(ContentNegotiation) { json() }
        defaultRequest {
            port = 8080
        }
    }

    val internalClient = createClient {
        install(ContentNegotiation) { json() }
        defaultRequest {
            port = 8090
        }
    }
...
}
When I use the client in the tests, it gives me that error
a

Aleksei Tirman [JB]

03/30/2023, 9:47 AM
Can you try to explicitly specify the host for the
DefaultRequest
plugin? I guess the problem is that the
127.0.0.1
and
localhost
are different things for the test engine.
d

dave08

03/30/2023, 9:51 AM
Doesn't work for either... here's my connectors:
Copy code
fun ApplicationEngineEnvironmentBuilder.envConfig(module: Application.() -> Unit) {
    module {
        module()
    }
    connector {
        host = "0.0.0.0"
        port = 8080
    }
    connector {
        host = "127.0.0.1"
        port = 8090
    }
}
I tried and host = "localhost" and host = "127.0.0.1" in the client...
Ok.. the external one works with:
host = "0.0.0.0"
... 🤒 maybe Ktor should be a bit smarter when seeing these hosts? I'm sure I'm not the only one facing such things...
Or at least maybe a note in the "Testing" docs about this -- that's the first place I tried looking at when I came across the problem... but there was no mention of ports...
a

Aleksei Tirman [JB]

03/31/2023, 4:00 AM
Can you please file an issue?
64 Views