Last active
August 7, 2019 02:45
-
-
Save jeremyrempel/ff5821bd099104ea5358a987f32105f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample | |
import com.github.jeremyrempel.yaba.util.runBlockingTest | |
import io.ktor.client.HttpClient | |
import io.ktor.client.engine.mock.MockEngine | |
import io.ktor.client.engine.mock.respond | |
import io.ktor.client.features.json.JsonFeature | |
import io.ktor.client.features.json.serializer.KotlinxSerializer | |
import io.ktor.client.request.get | |
import kotlinx.serialization.Serializable | |
import org.junit.Test | |
class TestHttp { | |
@Serializable | |
data class UserId( | |
val userId: String, | |
val id: Int, | |
val title: String, | |
val completed: Boolean | |
) | |
@Test | |
fun testFail() { | |
runBlockingTest { | |
// error | |
val client = HttpClient(MockEngine) { | |
install(JsonFeature) { | |
serializer = KotlinxSerializer().apply { | |
register(UserId.serializer()) | |
} | |
} | |
engine { | |
addHandler { | |
respond( | |
""" | |
{ | |
"userId": 1, | |
"id": 3, | |
"title": "fugiat veniam minus", | |
"completed": false | |
} | |
""".trimIndent() | |
) | |
} | |
} | |
} | |
// fails with error: No transformation found: class kotlinx.coroutines.io.ByteBufferChannel | |
val response = client.get<UserId>("/") | |
println(response) | |
} | |
} | |
@Test | |
fun testWorking() { | |
runBlockingTest { | |
val client = HttpClient { | |
install(JsonFeature) { | |
serializer = KotlinxSerializer().apply { | |
register(UserId.serializer()) | |
} | |
} | |
} | |
// correctly parses | |
val response = client.get<UserId>("https://jsonplaceholder.typicode.com/todos/3") | |
println(response) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment