Открыть боковую панель
Aurora OS
Kotlin Multiplatform
Libraries
ktor
Коммиты
c2d53b39
Коммит
c2d53b39
создал
Фев 01, 2019
по автору
Leonid Stashevsky
Просмотр файлов
Move HttpBin test to common
владелец
d49fc3f8
Изменения
5
Скрыть пробелы
Построчно
Рядом
ktor-client/ktor-client-curl/posix/test/CurlEnginesTest.kt
удалено
100644 → 0
Просмотр файла @
d49fc3f8
package
io.ktor.client.test
import
io.ktor.client.*
import
io.ktor.client.engine.*
import
io.ktor.client.engine.curl.*
import
io.ktor.client.features.json.*
import
io.ktor.client.features.json.serializer.*
import
io.ktor.client.features.logging.*
import
io.ktor.client.request.*
import
kotlinx.io.core.*
import
kotlinx.serialization.*
import
kotlin.test.*
import
kotlinx.coroutines.*
import
kotlinx.serialization.json.*
@Serializable
data class
HttpBinResponse
(
val
url
:
String
,
val
args
:
Map
<
String
,
String
>,
val
headers
:
Map
<
String
,
String
>
)
class
CurlEnginesTest
{
@Test
fun
getTest
():
Unit
=
clientTest
{
client
->
val
response
=
client
.
get
<
HttpBinResponse
>(
"http://httpbin.org/get"
)
val
expected
=
HttpBinResponse
(
"http://httpbin.org/get"
,
emptyMap
(),
mapOf
(
"Accept"
to
"application/json"
,
"Connection"
to
"close"
,
"Host"
to
"httpbin.org"
)
)
assertEquals
(
expected
,
response
)
}
@Test
fun
postTest
()
=
clientTest
{
client
->
val
response
=
client
.
post
<
HttpBinResponse
>(
"http://httpbin.org/post"
)
{
body
=
"Hello, bin!"
}
val
expected
=
HttpBinResponse
(
"http://httpbin.org/post"
,
emptyMap
(),
mapOf
(
"Content-Type"
to
"text/plain; charset=UTF-8"
,
"Accept"
to
"application/json"
,
"Content-Length"
to
"11"
,
"Connection"
to
"close"
,
"Host"
to
"httpbin.org"
)
)
assertEquals
(
expected
,
response
)
}
private
fun
clientTest
(
block
:
suspend
(
HttpClient
)
->
Unit
):
Unit
=
runBlocking
{
HttpClient
(
Curl
)
{
install
(
JsonFeature
)
{
serializer
=
KotlinxSerializer
(
JSON
.
nonstrict
).
apply
{
register
(
HttpBinResponse
.
serializer
())
}
}
}.
use
{
block
(
it
)
}
}
}
ktor-client/ktor-client-ios/darwin/test/IosEnginesTest.kt
удалено
100644 → 0
Просмотр файла @
d49fc3f8
package
io.ktor.client.test
import
io.ktor.client.*
import
io.ktor.client.engine.*
import
io.ktor.client.engine.ios.*
import
io.ktor.client.features.json.*
import
io.ktor.client.features.json.serializer.*
import
io.ktor.client.request.*
import
kotlinx.io.core.*
import
kotlinx.serialization.*
import
kotlin.test.*
@Serializable
data class
HttpBinResponse
(
val
url
:
String
,
val
args
:
Map
<
String
,
String
>,
val
headers
:
Map
<
String
,
String
>,
val
origin
:
String
)
class
IosEnginesTest
{
@Test
fun
getTest
():
Unit
=
clientTest
{
client
->
val
response
=
client
.
get
<
HttpBinResponse
>(
"http://httpbin.org/get"
)
println
(
response
)
}
@Test
fun
postTest
()
{
}
private
fun
clientTest
(
block
:
suspend
(
HttpClient
)
->
Unit
):
Unit
=
help
{
HttpClient
(
Ios
)
{
install
(
JsonFeature
)
{
serializer
=
KotlinxSerializer
().
apply
{
register
(
HttpBinResponse
.
serializer
())
}
}
}.
use
{
block
(
it
)
}
}
}
ktor-client/ktor-client-ios/darwin/test/utils.kt
удалено
100644 → 0
Просмотр файла @
d49fc3f8
package
io.ktor.client.test
import
kotlinx.coroutines.*
import
platform.Foundation.*
import
kotlin.coroutines.*
internal
class
TestDispatcher
:
CoroutineDispatcher
()
{
private
val
tasks
=
mutableSetOf
<
Runnable
>()
override
fun
dispatch
(
context
:
CoroutineContext
,
block
:
Runnable
)
{
tasks
+=
block
}
fun
schedule
(
block
:
suspend
()
->
Unit
,
onDone
:
(
Throwable
?)
->
Unit
)
{
block
.
startCoroutine
(
TestContinuation
(
this
,
onDone
))
}
fun
isEmpty
()
=
tasks
.
isEmpty
()
fun
runSingleTask
()
{
if
(
tasks
.
isEmpty
())
{
help
()
return
}
val
task
=
tasks
.
iterator
().
next
()
try
{
task
.
run
()
}
catch
(
cause
:
Throwable
)
{
cause
.
printStackTrace
()
}
finally
{
tasks
.
remove
(
task
)
}
}
private
fun
help
()
{
val
date
=
NSDate
().
addTimeInterval
(
1.0
)
as
NSDate
NSRunLoop
.
mainRunLoop
.
runUntilDate
(
date
)
}
}
internal
class
TestContinuation
(
override
val
context
:
CoroutineContext
,
private
val
onDone
:
(
Throwable
?)
->
Unit
)
:
Continuation
<
Unit
>
{
override
fun
resumeWith
(
result
:
Result
<
Unit
>)
{
onDone
(
result
.
exceptionOrNull
())
}
}
internal
fun
help
(
block
:
suspend
CoroutineDispatcher
.()
->
Unit
)
{
val
dispatcher
=
TestDispatcher
()
var
done
=
false
var
cause
:
Throwable
?
=
null
dispatcher
.
schedule
({
dispatcher
.
block
()
})
{
done
=
true
cause
=
it
}
while
(!
done
)
{
dispatcher
.
runSingleTask
()
cause
?.
let
{
it
.
printStackTrace
()
cause
=
null
}
}
while
(!
dispatcher
.
isEmpty
())
{
dispatcher
.
runSingleTask
()
cause
?.
let
{
it
.
printStackTrace
()
cause
=
null
}
}
}
ktor-client/ktor-client-tests/build.gradle
Просмотр файла @
c2d53b39
...
@@ -6,6 +6,7 @@ kotlin.sourceSets {
...
@@ -6,6 +6,7 @@ kotlin.sourceSets {
commonMain
.
dependencies
{
commonMain
.
dependencies
{
api
project
(
':ktor-client:ktor-client-core'
)
api
project
(
':ktor-client:ktor-client-core'
)
api
project
(
':ktor-client:ktor-client-tests:ktor-client-tests-dispatcher'
)
api
project
(
':ktor-client:ktor-client-tests:ktor-client-tests-dispatcher'
)
api
project
(
':ktor-client:ktor-client-features:ktor-client-json'
)
}
}
jvmMain
.
dependencies
{
jvmMain
.
dependencies
{
api
project
(
':ktor-server:ktor-server-jetty'
)
api
project
(
':ktor-server:ktor-server-jetty'
)
...
...
ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/HttpBinTest.kt
0 → 100644
Просмотр файла @
c2d53b39
package
io.ktor.client.tests
import
io.ktor.client.*
import
io.ktor.client.features.json.*
import
io.ktor.client.features.json.serializer.*
import
io.ktor.client.request.*
import
io.ktor.client.tests.utils.*
import
kotlinx.serialization.*
import
kotlinx.serialization.json.*
import
kotlin.test.*
@Serializable
data class
HttpBinResponse
(
val
url
:
String
,
val
args
:
Map
<
String
,
String
>,
val
headers
:
Map
<
String
,
String
>
)
class
HttpBinTest
{
@Test
fun
getTest
()
=
clientsTest
{
config
{
installJson
()
}
test
{
client
->
val
response
=
client
.
get
<
HttpBinResponse
>(
"http://httpbin.org/get"
)
val
expected
=
HttpBinResponse
(
"http://httpbin.org/get"
,
emptyMap
(),
mapOf
(
"Accept"
to
"application/json"
,
"Connection"
to
"close"
,
"Host"
to
"httpbin.org"
)
)
assertEquals
(
expected
,
response
)
}
}
@Test
fun
postTest
()
=
clientsTest
{
config
{
installJson
()
}
test
{
client
->
val
response
=
client
.
post
<
HttpBinResponse
>(
"http://httpbin.org/post"
)
{
body
=
"Hello, bin!"
}
val
expected
=
HttpBinResponse
(
"http://httpbin.org/post"
,
emptyMap
(),
mapOf
(
"Content-Type"
to
"text/plain; charset=UTF-8"
,
"Accept"
to
"application/json"
,
"Content-Length"
to
"11"
,
"Connection"
to
"close"
,
"Host"
to
"httpbin.org"
)
)
assertEquals
(
expected
,
response
)
}
}
private
fun
HttpClientConfig
<*>.
installJson
()
{
install
(
JsonFeature
)
{
serializer
=
KotlinxSerializer
(
Json
.
nonstrict
).
apply
{
register
(
HttpBinResponse
.
serializer
())
}
}
}
}
Редактирование
Предварительный просмотр
Поддерживает Markdown
0%
Попробовать снова
или
прикрепить новый файл
.
Отмена
You are about to add
0
people
to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Отмена
Пожалуйста,
зарегистрируйтесь
или
войдите
чтобы прокомментировать