Открыть боковую панель
Aurora OS
Kotlin Multiplatform
Libraries
ktor
Коммиты
35392e73
Не подтверждена
Коммит
35392e73
создал
Май 27, 2022
по автору
Rustam
Зафиксировано автором
GitHub
Май 27, 2022
Просмотр файлов
KTOR-4373 External services should use config from environment (#3024)
владелец
e4f151c1
Изменения
3
Скрыть пробелы
Построчно
Рядом
ktor-server/ktor-server-test-host/api/ktor-server-test-host.api
Просмотр файла @
35392e73
...
@@ -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
}
}
...
...
ktor-server/ktor-server-test-host/jvm/test/TestApplicationTestJvm.kt
Просмотр файла @
35392e73
...
@@ -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"
)
}
...
...
ktor-server/ktor-server-test-host/jvmAndNix/src/io/ktor/server/testing/TestApplication.kt
Просмотр файла @
35392e73
...
@@ -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.
Сначала завершите редактирование этого сообщения!
Отмена
Пожалуйста,
зарегистрируйтесь
или
войдите
чтобы прокомментировать