Próbuję sobie ogarnąć Ktor, wraz z testami i trochę trace pomysły:
mam tak postawiony serwer:
fun main() {
embeddedServer(Netty, port = 8080, host = "localhost") {
install(ContentNegotiation) {
json()
}
rootModule()
personsCrud()
}.start(wait = true)
}
fun Application.rootModule() {
// Starting point for a Ktor app:
routing {
get("/") {
call.respondText("Hello World!")
}
}
routing {
}
}
fun Application.personsCrud() {
routing {
post("/person") {
val person = call.receive<Person>()
println("${person.firstName} ${person.lastName}")
call.respond(PostResponse(1))
}
}
}
I fajnie, działa jak sobie uderzę takim zapytaniem:
POST http://localhost:8080/person
Content-Type: application/json
{"firstName":"John", "lastName":"Doe"}
HTTP/1.1 200 OK
Content-Length: 8
Content-Type: application/json
Connection: keep-alive
{
"id": 1
}
Ale jeżeli próbuję zrobić to z poziomu testu:
@Test
fun testPostPerson() = testApplication {
application {
personsCrud()
}
val client = createClient {
install(ContentNegotiation) {
json(Json {
prettyPrint = true
isLenient = true
})
}
}
client.post("/person") {
contentType(ContentType.Application.Json)
setBody(Person("John", "Doe"))
}
.apply {
assertEquals(HttpStatusCode.OK, this.status)
}
}
expected:<200 OK> but was:<415 Unsupported Media Type>
Ma ktoś jakiś pomysł?