From 0ea60681c639f8f3da67f3b2f439cd70c0eb0bca Mon Sep 17 00:00:00 2001 From: "He-Pin(kerr)" Date: Fri, 27 Nov 2026 03:44:03 +0800 Subject: [PATCH] test: cover HTTP/1 response trailers without body data Motivation: Keep a regression for a chunked HTTP/1.1 response that carries trailer fields without data chunks. Modification: Add the trailer-only exchange to the existing HTTP/1.1 suite alongside the body-bearing trailer test. Result: The focused client/server tests assert the final trailer fields with and without body data. References: apache/pekko-http#1623; RFC 9110 Section 6.5; RFC 9112 Section 7.1.2. --- .../http/scaladsl/ClientServerSpec.scala | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/http-core/src/test/scala/org/apache/pekko/http/scaladsl/ClientServerSpec.scala b/http-core/src/test/scala/org/apache/pekko/http/scaladsl/ClientServerSpec.scala index fd9c77df8..38609b278 100644 --- a/http-core/src/test/scala/org/apache/pekko/http/scaladsl/ClientServerSpec.scala +++ b/http-core/src/test/scala/org/apache/pekko/http/scaladsl/ClientServerSpec.scala @@ -86,6 +86,31 @@ class ClientServerSpec extends ClientServerSpecBase(http2 = false) { (StatusCodes.OK, Some(after.length.toLong), after)) } finally binding.terminate(1.second.dilated).awaitResult(timeout) } + + "preserve a declared trailer in an HTTP/1.1 response without body data" in Utils.assertAllStagesStopped { + // This application test field permits a report result in the trailer section. + val terminal = LastChunk(trailer = List(headers.RawHeader("X-Report-Result", "ready"))) + val response = HttpResponse( + headers = List(headers.Trailer(Seq("X-Report-Result"))), + entity = Chunked(ContentTypes.`application/octet-stream`, Source.single(terminal))) + val host = "127.0.0.1" + val timeout = 3.seconds.dilated + val binding = Http().newServerAt(host, 0).bindSync(_ => response).awaitResult(timeout) + try { + val received = Source.single(HttpRequest(uri = "/report")) + .via(Http().connectionTo(host).toPort(binding.localAddress.getPort).http()) + .mapAsync(1) { result => + result.status shouldBe StatusCodes.OK + result.protocol shouldBe HttpProtocols.`HTTP/1.1` + result.entity match { + case chunked: Chunked => chunked.chunks.runWith(Sink.seq) + case other => fail(s"Expected a chunked response entity, got $other") + } + } + .runWith(Sink.head) + received.awaitResult(timeout) shouldEqual Seq(terminal) + } finally binding.terminate(1.second.dilated).awaitResult(timeout) + } } } // Runs the same tests but with http2 enabled which makes sure the alternative server pipeline from Http2.scala