Skip to content

Commit

Permalink
KTOR-5383 Fix parsing server host and port when using NettyConnection… (
Browse files Browse the repository at this point in the history
#4014)

* KTOR-5383 Fix parsing server host and port when using NettyConnectionPoint and CIOConnectionPoint
  • Loading branch information
Stexxe authored and bjhham committed Apr 30, 2024
1 parent f8f8fe8 commit f59c32f
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package io.ktor.server.testing.suites
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
Expand Down Expand Up @@ -80,4 +81,50 @@ abstract class ConnectionTestSuite(val engine: ApplicationEngineFactory<*, *>) {

server.stop(50, 100)
}

@OptIn(DelicateCoroutinesApi::class)
@Test
fun testIpv6ServerHostAndPort() = runBlocking {
val serverStarted = CompletableDeferred<Unit>()
val serverPort = withContext(Dispatchers.IO) { ServerSocket(0).use { it.localPort } }
val server = embeddedServer(
engine,
applicationProperties(applicationEnvironment()) {
module {
routing {
get("/") {
val local = call.request.local
call.respondText { "${local.serverHost}:${local.serverPort}" }
}
}
}
}
) {
connector { port = serverPort }
}

server.monitor.subscribe(ServerReady) {
serverStarted.complete(Unit)
}

GlobalScope.launch {
server.start(true)
}

withTimeout(5000) {
serverStarted.join()
val client = HttpClient(CIO)
client.get("http://[::1]:$serverPort/").let { response ->
assertEquals(HttpStatusCode.OK, response.status)
assertEquals("[::1]:$serverPort", response.bodyAsText())
}
client.get("http://[0:0:0:0:0:0:0:1]:$serverPort/").let { response ->
assertEquals(HttpStatusCode.OK, response.status)
assertEquals("[0:0:0:0:0:0:0:1]:$serverPort", response.bodyAsText())
}
client.close()
}

server.stop(50, 100)
}
}

0 comments on commit f59c32f

Please sign in to comment.