Skip to content

Instantly share code, notes, and snippets.

@jeremyrempel
Last active August 7, 2019 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyrempel/ff5821bd099104ea5358a987f32105f1 to your computer and use it in GitHub Desktop.
Save jeremyrempel/ff5821bd099104ea5358a987f32105f1 to your computer and use it in GitHub Desktop.
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