Jonathan Hollingsworth
10/04/2021, 7:14 PMJonathan Hollingsworth
10/04/2021, 7:15 PMwithTestApplication({ configureRouting() }) {
with(handleRequest(<http://HttpMethod.Post|HttpMethod.Post>, "/preview", ) {
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(Json.encodeToString(Entity(name="test", description="A test description")))
}) {
assertEquals(HttpStatusCode.OK, response.status())
}
}
And the Route definition even more so
post("/preview") {
val config = call.receive<Entity>()
call.respondText(call.receiveText())
}
It fails Cannot transform this request's content to <package>.Entity
Jonathan Hollingsworth
10/04/2021, 8:34 PMcall.receive<Entity>()
with Json.decodeFromString<Entity>(call.receiveText())
it seems to work. I’d have thought they were functionally similar.Landry Norris
10/10/2021, 11:53 PMAleksei Tirman [JB]
10/11/2021, 5:09 PMimport io.ktor.application.*
import io.ktor.features.*
import io.ktor.http.*
import io.ktor.request.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.serialization.*
import io.ktor.server.testing.*
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.junit.Test
import kotlin.test.assertEquals
class SomeTest {
@OptIn(ExperimentalSerializationApi::class)
@Test
fun test(): Unit = withTestApplication({ configureRouting() }) {
application.install(ContentNegotiation) {
json()
}
with(handleRequest(<http://HttpMethod.Post|HttpMethod.Post>, "/preview", ) {
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(Json.encodeToString(Entity(name="test", description="A test description")))
}) {
assertEquals(HttpStatusCode.OK, response.status())
assertEquals("A test description", response.content)
}
}
}
fun Application.configureRouting() {
routing {
post("/preview") {
val config = call.receive<Entity>()
call.respondText(config.description)
}
}
}
@Serializable
data class Entity(val name: String, val description: String)
Jonathan Hollingsworth
10/14/2021, 2:41 AMContentNegotiation
in the test.