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

hruan

05/04/2022, 9:53 AM
Hi, I’ve run an issue with testing a Ktor app where the test client receives a response with status
415
when
201
is expected. Running the app and calling the same endpoint via curl works as expected, leading me to believe that I’ve done something wrong with the test client setup. Any pointers would be appreciated. Details in thread.
Test set up is as follows:
Copy code
testApplication {
  // ... test application setup ...
  
  val testClient = createClient {
      install(ContentNegotiation) {
          json(Json { DefaultJson })
      }

      install(Auth) {
          bearer {
              loadTokens {
                  BearerTokens(jwt, "nope")
              }
          }
      }
  }

  <http://testClient.post|testClient.post>("/some-post-endpoint") {
      accept(ContentType.Application.Json)
      contentType(ContentType.Application.Json)
      setBody(CreationRequest("test", listOf("test"), "testOrg", "testId"))
  }.apply {
      assertEquals(HttpStatusCode.Created, status)
  }
}
ContentNegotiation
plugin config match the server.
Removing the
createClient
bit and doing the post manually
Copy code
<http://client.post|client.post>("/some-post-endpoint") {
    accept(ContentType.Application.Json)
    contentType(ContentType.Application.Json)
    header(HttpHeaders.Authorization, "Bearer $jwt")
    setBody(
        """
        {
          ... some raw json ...
        }
    """.trimIndent()
    )
  }.apply {
    assertEquals(HttpStatusCode.Created, status)
  }
Still returns
415 Unsupported Media Type
. What am I missing here?
Aha, found it! Completely my fault. I had missed one of the extension methods with plugins when building
testApplication
🤦