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

Cherrio LLC

01/24/2023, 10:46 AM
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

Aleksei Tirman [JB]

01/24/2023, 11:01 AM
Could you please share the code of your test?
c

Cherrio LLC

01/24/2023, 11:01 AM
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

Aleksei Tirman [JB]

01/24/2023, 11:02 AM
And what are you trying to test?
c

Cherrio LLC

01/24/2023, 11:03 AM
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

Aleksei Tirman [JB]

01/24/2023, 11:06 AM
So you want to mock a server that accepts a Websockets connection?
c

Cherrio LLC

01/24/2023, 11:06 AM
Yes.
a

Aleksei Tirman [JB]

01/24/2023, 11:11 AM
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

Cherrio LLC

01/24/2023, 11:14 AM
Okay. 👍
3 Views