Не подтверждена Коммит 35392e73 создал по автору Rustam's avatar Rustam Зафиксировано автором GitHub
Просмотр файлов

KTOR-4373 External services should use config from environment (#3024)

владелец e4f151c1
...@@ -75,7 +75,7 @@ protected abstract interface annotation class io/ktor/server/testing/EngineTestB ...@@ -75,7 +75,7 @@ protected abstract interface annotation class io/ktor/server/testing/EngineTestB
} }
public final class io/ktor/server/testing/ExternalServicesBuilder { public final class io/ktor/server/testing/ExternalServicesBuilder {
public fun <init> ()V public synthetic fun <init> ()V
public final fun hosts ([Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V public final fun hosts ([Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V
} }
......
...@@ -98,6 +98,27 @@ class TestApplicationTestJvm { ...@@ -98,6 +98,27 @@ class TestApplicationTestJvm {
assertEquals("OK FROM MODULE", response.bodyAsText()) assertEquals("OK FROM MODULE", response.bodyAsText())
} }
@Test
fun testExternalServicesCustomConfig() = testApplication {
environment {
config = ApplicationConfig("application-custom.conf")
}
externalServices {
hosts("http://www.google.com") {
val config = environment.config
routing {
get {
val configValue = config.propertyOrNull("ktor.test")?.getString() ?: "no value"
call.respond(configValue)
}
}
}
}
val external = client.get("http://www.google.com")
assertEquals("another_test_value", external.bodyAsText())
}
public fun Application.module() { public fun Application.module() {
routing { routing {
get { call.respond("OK FROM MODULE") } get { call.respond("OK FROM MODULE") }
......
...@@ -82,8 +82,15 @@ public fun TestApplication( ...@@ -82,8 +82,15 @@ public fun TestApplication(
* Registers mocks for external services. * Registers mocks for external services.
*/ */
@KtorDsl @KtorDsl
public class ExternalServicesBuilder { public class ExternalServicesBuilder internal constructor(private val testApplicationBuilder: TestApplicationBuilder) {
internal val externalApplications = mutableMapOf<String, TestApplication>()
@Deprecated(message = "This constructor will be removed from public API", level = DeprecationLevel.HIDDEN)
public constructor() : this(TestApplicationBuilder())
private val externalApplicationBuilders = mutableMapOf<String, () -> TestApplication>()
internal val externalApplications: Map<String, TestApplication> by lazy {
externalApplicationBuilders.mapValues { it.value.invoke() }
}
/** /**
* Registers a mock for an external service specified by [hosts] and configured with [block]. * Registers a mock for an external service specified by [hosts] and configured with [block].
...@@ -93,10 +100,14 @@ public class ExternalServicesBuilder { ...@@ -93,10 +100,14 @@ public class ExternalServicesBuilder {
public fun hosts(vararg hosts: String, block: Application.() -> Unit) { public fun hosts(vararg hosts: String, block: Application.() -> Unit) {
check(hosts.isNotEmpty()) { "hosts can not be empty" } check(hosts.isNotEmpty()) { "hosts can not be empty" }
val application = TestApplication { applicationModules.add(block) }
hosts.forEach { hosts.forEach {
val protocolWithAuthority = Url(it).protocolWithAuthority val protocolWithAuthority = Url(it).protocolWithAuthority
externalApplications[protocolWithAuthority] = application externalApplicationBuilders[protocolWithAuthority] = {
TestApplication {
environment(this@ExternalServicesBuilder.testApplicationBuilder.environmentBuilder)
applicationModules.add(block)
}
}
} }
} }
} }
...@@ -109,7 +120,7 @@ public open class TestApplicationBuilder { ...@@ -109,7 +120,7 @@ public open class TestApplicationBuilder {
private var built = false private var built = false
internal val externalServices = ExternalServicesBuilder() internal val externalServices = ExternalServicesBuilder(this)
internal val applicationModules = mutableListOf<Application.() -> Unit>() internal val applicationModules = mutableListOf<Application.() -> Unit>()
internal var environmentBuilder: ApplicationEngineEnvironmentBuilder.() -> Unit = {} internal var environmentBuilder: ApplicationEngineEnvironmentBuilder.() -> Unit = {}
internal val job = Job() internal val job = Job()
......
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать