Skip to content

Commit 82eaa1e

Browse files
authored
KTOR-4875 Fixed respecting connectors port in testApplication (#3169)
1 parent 24d59f0 commit 82eaa1e

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

ktor-server/ktor-server-test-host/jvmAndNix/src/io/ktor/server/testing/TestApplicationEngine.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,19 @@ class TestApplicationEngine(
119119
}
120120

121121
override suspend fun resolvedConnectors(): List<EngineConnectorConfig> {
122+
if (environment.connectors.isNotEmpty()) {
123+
return environment.connectors
124+
}
122125
return listOf(
123126
object : EngineConnectorConfig {
124127
override val type: ConnectorType = ConnectorType.HTTP
125-
override val host: String = environment.connectors.firstOrNull()?.host ?: "localhost"
126-
override val port: Int = environment.connectors.firstOrNull()?.port ?: 80
128+
override val host: String = "localhost"
129+
override val port: Int = 80
127130
},
128131
object : EngineConnectorConfig {
129132
override val type: ConnectorType = ConnectorType.HTTPS
130-
override val host: String = environment.connectors.firstOrNull()?.host ?: "localhost"
131-
override val port: Int = environment.connectors.firstOrNull()?.port ?: 443
133+
override val host: String = "localhost"
134+
override val port: Int = 443
132135
}
133136
)
134137
}

ktor-server/ktor-server-test-host/jvmAndNix/src/io/ktor/server/testing/client/TestHttpClientEngine.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class TestHttpClientEngine(override val config: TestHttpClientConfig) : H
5555
}
5656

5757
val testServerCall = with(data) {
58-
runRequest(method, url.fullPath, headers, body, url.protocol)
58+
runRequest(method, url, headers, body, url.protocol)
5959
}
6060

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

6666
private suspend fun runRequest(
6767
method: HttpMethod,
68-
url: String,
68+
url: Url,
6969
headers: Headers,
7070
content: OutgoingContent,
7171
protocol: URLProtocol
7272
): TestApplicationCall {
7373
return app.handleRequestNonBlocking {
74-
this.uri = url
74+
this.uri = url.fullPath
75+
this.port = url.port
7576
this.method = method
7677
appendRequestHeaders(headers, content)
7778
this.protocol = protocol.name

ktor-server/ktor-server-test-host/jvmAndNix/test/TestApplicationTest.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import io.ktor.http.*
1212
import io.ktor.http.content.*
1313
import io.ktor.server.application.*
1414
import io.ktor.server.config.*
15+
import io.ktor.server.engine.*
1516
import io.ktor.server.plugins.*
1617
import io.ktor.server.request.*
1718
import io.ktor.server.response.*
@@ -279,4 +280,43 @@ class TestApplicationTest {
279280
val response = client.get("/boom")
280281
assertEquals(HttpStatusCode.InternalServerError, response.status)
281282
}
283+
284+
@Test
285+
fun testConnectors(): Unit = testApplication {
286+
environment {
287+
connector {
288+
port = 8080
289+
}
290+
connector {
291+
port = 8081
292+
}
293+
294+
module {
295+
routing {
296+
port(8080) {
297+
get {
298+
call.respond("8080")
299+
}
300+
}
301+
port(8081) {
302+
get {
303+
call.respond("8081")
304+
}
305+
}
306+
}
307+
}
308+
}
309+
310+
val response8080 = client.get {
311+
host = "0.0.0.0"
312+
port = 8080
313+
}
314+
assertEquals("8080", response8080.bodyAsText())
315+
316+
val response8081 = client.get {
317+
host = "0.0.0.0"
318+
port = 8081
319+
}
320+
assertEquals("8081", response8081.bodyAsText())
321+
}
282322
}

0 commit comments

Comments
 (0)