Trying to write tests on Ktor2 with kotlinx-serial...
# ktor
r
Trying to write tests on Ktor2 with kotlinx-serialization. I’m getting this error:
No request transformation found
. Does it sound familiar to someone? Looking a ktor source code, it comes from HttpRequest.kt
Copy code
body as? OutgoingContent ?: error("No request transformation found: $body")
It seems related to the setBody function in my test
Copy code
@Test
    fun testPostRandom() = testApplication {
        val response = <http://client.post|client.post>("/random") {
            contentType(ContentType.Application.Json)
            setBody(PostRandomRequest(listOf("Jet", "Brains")))

        }
        assertEquals("Hello World!", response.bodyAsText())
        assertEquals(HttpStatusCode.Created, response.status)
    }
a
You need to install the
ContentNegotiation
plugin into the client to serialize the request body:
Copy code
val client = createClient {
    install(ContentNegotiation) {
        json()
    }
}
val response = <http://client.post|client.post>("/random") {
    contentType(ContentType.Application.Json)
    setBody(PostRandomRequest(listOf("Jet", "Brains")))

}
assertEquals("Hello World!", response.bodyAsText())
assertEquals(HttpStatusCode.Created, response.status)
r
I’ve tried this approach unsuccessfully 😞
Copy code
val client = createClient {
    this@testApplication.install(ContentNegotiation)
    ...
But since I’m using HOCON config file, according to the doc:
Add modules automatically
If you have the
application.conf
file in the
resources
folder,
testApplication
loads all modules and properties specified in the configuration file automatically.
p
you need to bring in the right
ContentNegotiation
for the client, not the server version
👍 1
Copy code
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
or
Copy code
createClient {    
    install(io.ktor.client.plugins.contentnegotiation.ContentNegotiation) 
}
the HOCON configuration file will configure the server/application for you, but not the client afaik (certainly not in the test application).
👌 1
r
It works, awesome. Thanks @phldavies 👏
🙌 1
r
Hello Everyone, Here is my client:
Copy code
val client by lazy {
    httpClient ?: HttpClient(defaultPlatformEngine) {
        // Configuring HTTP client settings
        expectSuccess = true

        install(ContentNegotiation) {
            // Configuring JSON serialization for content negotiation
            json(json)
        }

        defaultRequest {
            // Setting default base URL for requests
            url(networkConfig.baseUrl)
        }

  if (networkConfig.isLoggingEnabled) {
            // Install logging if enabled in NetworkConfig
            install(Logging) {
                logger = object : Logger {
                    override fun log(message: String) {
                        println("KTOR_LOGGER-->>\n$message")
                    }
                }
                level = LogLevel.ALL
            }
        }
    }
}
when I am trying to call an get api, it works fine. But when I am trying post api, it throws an exception: java.lang.NullPointerException java.lang.NullPointerException 190424.931 W at io.ktor.client.plugins.contentnegotiation.ContentNegotiation$Plugin$install$1.invokeSuspend(ContentNegotiation.kt:115) at io.ktor.client.plugins.contentnegotiation.ContentNegotiation$Plugin$install$1.invoke(Unknown Source:13) at io.ktor.client.plugins.contentnegotiation.ContentNegotiation$Plugin$install$1.invoke(Unknown Source:4) at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:123) Please help.