From 1bfae22946da6aca54d7bc547fbd41c049300174 Mon Sep 17 00:00:00 2001 From: "He-Pin(kerr)" Date: Fri, 27 Nov 2026 06:35:27 +0800 Subject: [PATCH] test: cover HTTP/1 client EOF before the first response chunk Motivation: A chunked response can reach the client header-first and lose the connection before the application writes any body chunks. Modification: Render a normal typed response with a pending body and deliver its headers through the connection-level test harness. Close the peer input and assert that the exposed entity stream fails without emitting a chunk. Result: The test distinguishes incomplete entities from successful empty responses. Production behavior is unchanged. Tests: - Five focused client cases passed on Scala 2.13.18 and 3.3.9 with JDK 17. - Scoped Scalafmt and git diff --check passed. References: RFC 9112 section 8, Handling Incomplete Messages. --- .../LowLevelOutgoingConnectionSpec.scala | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/http-core/src/test/scala/org/apache/pekko/http/impl/engine/client/LowLevelOutgoingConnectionSpec.scala b/http-core/src/test/scala/org/apache/pekko/http/impl/engine/client/LowLevelOutgoingConnectionSpec.scala index 16986a113..268e90635 100644 --- a/http-core/src/test/scala/org/apache/pekko/http/impl/engine/client/LowLevelOutgoingConnectionSpec.scala +++ b/http-core/src/test/scala/org/apache/pekko/http/impl/engine/client/LowLevelOutgoingConnectionSpec.scala @@ -32,6 +32,12 @@ import pekko.http.scaladsl.model.HttpEntity._ import pekko.http.scaladsl.model.HttpMethods._ import pekko.http.scaladsl.model._ import pekko.http.scaladsl.model.headers._ +import pekko.http.impl.engine.rendering.{ + DateHeaderRendering, + HttpResponseRendererFactory, + ResponseRenderingContext, + ResponseRenderingOutput +} import pekko.http.impl.util._ import pekko.testkit._ @@ -634,6 +640,32 @@ class LowLevelOutgoingConnectionSpec extends PekkoSpecWithMaterializer with Insi netInSub.expectCancellation() } + "fail a chunked response entity when the connection closes before the first chunk" in new TestSetup { + sendStandardRequest() + + // The peer starts a normal response, but its application has not produced a body chunk yet. + val response = HttpResponse(entity = Chunked(ContentTypes.`application/octet-stream`, Source.maybe)) + val ResponseRenderingOutput.HttpData(initialHeaders) = Source.single(ResponseRenderingContext(response)) + .via(new HttpResponseRendererFactory(None, 256, NoLogging, DateHeaderRendering()).renderer) + .runWith(Sink.head) + .awaitResult(3.seconds.dilated) + sendWireData(initialHeaders) + + val received = expectResponse() + received.status shouldBe StatusCodes.OK + val Chunked(_, chunks) = received.entity + val probe = TestSubscriber.manualProbe[ChunkStreamPart]() + chunks.runWith(Sink.fromSubscriber(probe)) + probe.expectSubscription().request(1) + + closeNetworkInput() + + probe.expectError() shouldBe an[EntityStreamException] + responses.expectComplete() + netOut.expectComplete() + requestsSub.expectCancellation() + } + for ((phase, lineEnding) <- List( "before the status line ends" -> "", "after the status line but before the headers end" -> "\r\n"))