https://kotlinlang.org logo
#ktor
Title
# ktor
r

Riccardo Cardin

11/25/2023, 9:52 AM
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

Sam

11/25/2023, 9:59 AM
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

Riccardo Cardin

11/25/2023, 10:03 AM
Yep, you’re right mind blown. Thanks a lot. I said I’m a really newbie 😅
s

Sam

11/25/2023, 10:03 AM
No problem, glad I could help!
gratitude thank you 1
🎉 1