Hey. I’m a newbie to Ktor (experience with Spring ...
# ktor
r
Hey. I’m a newbie to Ktor (experience with Spring Boot). I’m having some problems creating a
post
route. The definition is the following:
Copy code
fun Application.configureSerialization() {
  install(ContentNegotiation) { json() }
}
fun Application.configureRouting() {
  routing {
    post("/portfolios") {
      val dto = call.receive<CreatePortfolioDTO>()
      call.response.header("Location", "/portfolios/1")
      call.respond(HttpStatusCode.Created)
    }
  }
}

@Serializable data class CreatePortfolioDTO(val userId: String, val amount: Double)
The test I try to develop is the following:
Copy code
class ApplicationTest {
  @Test
  fun testRoot() = testApplication {
    application { configureRouting() }
    val client = createClient { install(ContentNegotiation) { json() } }
    val response =
        client
            .post {
              url("/portfolios")
              contentType(ContentType.Application.Json)
              setBody(CreatePortfolioDTO("rcardin", 100.0))
            }
            .apply { assertEquals(status, HttpStatusCode.Created) }
  }
}
I can’t understand why the test fails with the following message:
Copy code
java.lang.AssertionError: expected:<415 Unsupported Media Type> but was:<201 Created>
Is there somebody that can help me understand what’s going on? Thanks a lot 🙂
s
To start with, I think your error message is misleading because of the order of arguments in
assertEquals
. I suspect it should really be "Expected 201 but was 415".
In your test, you called
configureRouting()
but you didn't call
configureSerialization()
. Maybe that's the problem?
r
Yep, you’re right mind blown. Thanks a lot. I said I’m a really newbie 😅
s
No problem, glad I could help!
gratitude thank you 1
🎉 1