Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KTOR-4875 Fixed respecting connectors port in testApplication #3169

Merged
merged 1 commit into from Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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())
}
}