Can anyone explain why the test below throws an ex...
# ktor
l
Can anyone explain why the test below throws an exception? The code works when consuming the endpoint using the browser.
Copy code
Exception in thread "ForkJoinPool.commonPool-worker-2 @coroutine#4" java.lang.IllegalStateException: Unsupported opcode b
	at io.ktor.websocket.FrameParser.getFrameType(FrameParser.kt:26)
	at io.ktor.websocket.FrameParser.parseHeader1(FrameParser.kt:69)
	at io.ktor.websocket.FrameParser.handleStep(FrameParser.kt:56)
	at io.ktor.websocket.FrameParser.frame(FrameParser.kt:52)
	at io.ktor.websocket.WebSocketReader.parseLoop(WebSocketReader.kt:55)
	at io.ktor.websocket.WebSocketReader.readLoop(WebSocketReader.kt:46)
	at io.ktor.websocket.WebSocketReader$readerJob$1.doResume(WebSocketReader.kt:21)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:54)
	at kotlinx.coroutines.experimental.DispatchTask.run(CoroutineDispatcher.kt:123)
	at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402)
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
	at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Copy code
@Test
fun `It can get a car using cars ws endpoint`() = withTestApplication(Application::main) {
    val webSocketRequest = handleWebSocket("/cars", {
        body = """{"model":"Ford Mustang","releaseYear":1976}"""
    })

    webSocketRequest.let { call ->
        call.response.awaitWebSocket(Duration.ofSeconds(20))

        val (model, releaseYear) = jacksonObjectMapper().readValue<Car>(call.response.content!!)
        assertEquals(model, "Ford Mustang")
        assertEquals(releaseYear, 1789)
    }
}
Copy code
routing {
        val messages = ConflatedBroadcastChannel<Car>()
        webSocket("/cars") {
            messages.openSubscription().use { subscription ->
                subscription.consumeEach {
                    println("Sending $it")
                    outgoing.send(Frame.Text(jacksonObjectMapper().writeValueAsString(it)))
                }
            }
        }

        launch {
            while (isActive) {
                delay(1000L)
                messages.offer(Car(model = "Ford Mustang", releaseYear = 1976))
            }
        }
    }
c
@Lucas this is because you are sending JSON content instead of websocket frames. Unfortunately test host still doesn't support streaming so you need to encode all the frames at once
(opcode
b
is caused by character
{
with ascii code 0x7b)
l
Thank you for the clarification, @cy.