Hello I am getting an error running a test for web...
# ktor
c
Hello I am getting an error running a test for websocket client. Error in thread.
Copy code
io.ktor.client.call.NoTransformationFoundException: No transformation found: class io.ktor.utils.io.ByteBufferChannel (Kotlin reflection is not available) -> class io.ktor.client.plugins.websocket.DefaultClientWebSocketSession (Kotlin reflection is not available)
Are we to use the same
MockEngine
for websockets?
More errors when I ran the test against ios
kotlinx.serialization.SerializationException: Serializer for class 'DefaultClientWebSocketSession' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
@Aleksei Tirman [JB] I can't mark
DefaultClientWebSocketSession
as Serializable. Is there a workaround?
a
Could you please share the code of your test?
c
Copy code
private val mockEngine = MockEngine { _ ->
        respond(
            content = """{"status":"successful", "message":"success", "data":"Yes"}""",
            status = HttpStatusCode.SwitchingProtocols,
            headers = headersOf(HttpHeaders.ContentType, "application/text")
        )
    }

@Test
    fun connect_success_ForConnection() = runTest {
        data.configure("url","user","path")
        val connectResponse = data.connectToServer().unBox()
        assertEquals("Connected", connectResponse)
    }
a
And what are you trying to test?
c
Test connection
The
connectToServer()
emits a message when a connection is established.
Copy code
while (true) {
                        emit("Connected")
         
                        val frame = incoming.receive()
                        if (frame is Frame.Text) {
                            emit(frame.readText())
                        }
                    }
a
So you want to mock a server that accepts a Websockets connection?
c
Yes.
a
Unfortunately, the
MockEngine
doesn’t have such capabilities and making
DefaultClientWebSocketSession
serializable won’t help. You may try to test your client from the other end by using the testApplication.
c
Okay. 👍