Skip to content

Commit

Permalink
KTOR-4875 Fixed respecting connectors port in testApplication (#3169)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsinukov authored Sep 27, 2022
1 parent 24d59f0 commit 82eaa1e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,19 @@ class TestApplicationEngine(
}

override suspend fun resolvedConnectors(): List<EngineConnectorConfig> {
if (environment.connectors.isNotEmpty()) {
return environment.connectors
}
return listOf(
object : EngineConnectorConfig {
override val type: ConnectorType = ConnectorType.HTTP
override val host: String = environment.connectors.firstOrNull()?.host ?: "localhost"
override val port: Int = environment.connectors.firstOrNull()?.port ?: 80
override val host: String = "localhost"
override val port: Int = 80
},
object : EngineConnectorConfig {
override val type: ConnectorType = ConnectorType.HTTPS
override val host: String = environment.connectors.firstOrNull()?.host ?: "localhost"
override val port: Int = environment.connectors.firstOrNull()?.port ?: 443
override val host: String = "localhost"
override val port: Int = 443
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class TestHttpClientEngine(override val config: TestHttpClientConfig) : H
}

val testServerCall = with(data) {
runRequest(method, url.fullPath, headers, body, url.protocol)
runRequest(method, url, headers, body, url.protocol)
}

return with(testServerCall.response) {
Expand All @@ -65,13 +65,14 @@ public class TestHttpClientEngine(override val config: TestHttpClientConfig) : H

private suspend fun runRequest(
method: HttpMethod,
url: String,
url: Url,
headers: Headers,
content: OutgoingContent,
protocol: URLProtocol
): TestApplicationCall {
return app.handleRequestNonBlocking {
this.uri = url
this.uri = url.fullPath
this.port = url.port
this.method = method
appendRequestHeaders(headers, content)
this.protocol = protocol.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.server.application.*
import io.ktor.server.config.*
import io.ktor.server.engine.*
import io.ktor.server.plugins.*
import io.ktor.server.request.*
import io.ktor.server.response.*
Expand Down Expand Up @@ -279,4 +280,43 @@ class TestApplicationTest {
val response = client.get("/boom")
assertEquals(HttpStatusCode.InternalServerError, response.status)
}

@Test
fun testConnectors(): Unit = testApplication {
environment {
connector {
port = 8080
}
connector {
port = 8081
}

module {
routing {
port(8080) {
get {
call.respond("8080")
}
}
port(8081) {
get {
call.respond("8081")
}
}
}
}
}

val response8080 = client.get {
host = "0.0.0.0"
port = 8080
}
assertEquals("8080", response8080.bodyAsText())

val response8081 = client.get {
host = "0.0.0.0"
port = 8081
}
assertEquals("8081", response8081.bodyAsText())
}
}

0 comments on commit 82eaa1e

Please sign in to comment.