From 3d3e550db2e110e7f4fe078c112ef5fe9b95ede7 Mon Sep 17 00:00:00 2001 From: "He-Pin(kerr)" Date: Fri, 27 Nov 2026 07:57:29 +0800 Subject: [PATCH] test: cover a binary WebSocket frame arriving with the upgrade Motivation: The client checks that a text frame survives the HTTP-to-WebSocket handoff when it arrives with the upgrade response. A binary frame uses a different message decoding path. Modification: Run the same successful upgrade scenario for text and binary frames. Verify the exact message and successful upgrade, release every probe in finally, and assert all materialized stages stop. Result: Both inbound message types are covered at the upgrade boundary. Each case stops its streams even when an assertion fails. Production behavior is unchanged. Tests: On JDK 17, three focused cases passed on Scala 2.13.18 and 3.3.9 using the exact test source and current handshake source overlays. Scoped Scalafmt and git diff --check passed. References: RFC 6455 sections 4.1 and 5.6. The binary boundary is inspired by Netty 4.2 WebSocketClientHandshakerTest. --- .../impl/engine/ws/WebSocketClientSpec.scala | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/http-core/src/test/scala/org/apache/pekko/http/impl/engine/ws/WebSocketClientSpec.scala b/http-core/src/test/scala/org/apache/pekko/http/impl/engine/ws/WebSocketClientSpec.scala index 725921305..ef2e808cc 100644 --- a/http-core/src/test/scala/org/apache/pekko/http/impl/engine/ws/WebSocketClientSpec.scala +++ b/http-core/src/test/scala/org/apache/pekko/http/impl/engine/ws/WebSocketClientSpec.scala @@ -669,13 +669,31 @@ class WebSocketClientSpec extends PekkoSpecWithMaterializer("pekko.http.client.w closeNetworkInput() expectNetworkClose() } - "receive first frame in same chunk as HTTP upgrade response" in new TestSetup with ClientProbes { - expectWireData(UpgradeRequestBytes) - - val firstFrame = WSTestUtils.frame(Protocol.Opcode.Text, ByteString("fast"), fin = true, mask = false) - sendWireData(UpgradeResponseBytes ++ firstFrame) - - messagesIn.requestNext(TextMessage("fast")) + val firstBinaryPayload = ByteString(1, 0, 2, 3) + Seq[(String, Protocol.Opcode, ByteString, Message)]( + ("text", Protocol.Opcode.Text, ByteString("fast"), TextMessage("fast")), + ("binary", Protocol.Opcode.Binary, firstBinaryPayload, BinaryMessage(firstBinaryPayload))).foreach { + case (kind, opcode, payload, expected) => + s"receive first frame in same chunk as HTTP upgrade response ($kind)" in { + Utils.assertAllStagesStopped { + new TestSetup with ClientProbes { + try { + expectWireData(UpgradeRequestBytes) + + val firstFrame = WSTestUtils.frame(opcode, payload, fin = true, mask = false) + sendWireData(UpgradeResponseBytes ++ firstFrame) + + messagesIn.requestNext(expected) + response.awaitResult(1.second.dilated) shouldBe a[ValidUpgrade] + } finally { + messagesOut.sendComplete() + messagesIn.cancel() + netOut.cancel() + closeNetworkInput() + } + } + } + } } "manual scenario client sends first" in new EstablishedConnectionSetup with ClientProbes {