Коммит c2d53b39 создал по автору Leonid Stashevsky's avatar Leonid Stashevsky
Просмотр файлов

Move HttpBin test to common

владелец 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) }
}
}
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) }
}
}
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
}
}
}
...@@ -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')
......
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.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать