From a68457f48d5075ab708da4aaeb306c00d769c142 Mon Sep 17 00:00:00 2001 From: "He-Pin(kerr)" Date: Fri, 27 Nov 2026 07:57:27 +0800 Subject: [PATCH] test: cover typed response cookie factory validation Motivation: The Scala and Java response cookie factories share validation of names and values, but the rejection causes need direct coverage in both DSLs. Modification: Keep response cookie validation in the Scala header spec and add the Java rejection cases to the Java HttpCookie spec. Assert distinct name and content error fragments without duplicating the Java factory's existing successful construction case. Result: Both DSLs check that each invalid argument reaches its intended validation, without changing production behavior. References: RFC 6265 Section 4.1.1. --- .../javadsl/model/headers/HttpCookieSpec.scala | 10 ++++++++++ .../scaladsl/model/headers/HeaderSpec.scala | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/http-core/src/test/scala/org/apache/pekko/http/javadsl/model/headers/HttpCookieSpec.scala b/http-core/src/test/scala/org/apache/pekko/http/javadsl/model/headers/HttpCookieSpec.scala index 0c28962e0f..02726e75b2 100644 --- a/http-core/src/test/scala/org/apache/pekko/http/javadsl/model/headers/HttpCookieSpec.scala +++ b/http-core/src/test/scala/org/apache/pekko/http/javadsl/model/headers/HttpCookieSpec.scala @@ -46,6 +46,16 @@ class HttpCookieSpec extends AnyFreeSpec with Matchers { Optional.of(SameSite.Lax)).getSameSite must be(Optional.of(SameSite.Lax)) } } + "reject invalid arguments" - { + "non-token cookie name" in { + val error = the[IllegalArgumentException] thrownBy HttpCookie.create(" ", "dark") + error.getMessage must include("not allowed in cookie name") + } + "semicolon in a cookie value" in { + val error = the[IllegalArgumentException] thrownBy HttpCookie.create("theme", ";") + error.getMessage must include("not allowed in cookie content") + } + } "SameSite conversation to Scala" - { "convert Strict" in { SameSite.Strict.asScala() must be(sh.SameSite.Strict) diff --git a/http-core/src/test/scala/org/apache/pekko/http/scaladsl/model/headers/HeaderSpec.scala b/http-core/src/test/scala/org/apache/pekko/http/scaladsl/model/headers/HeaderSpec.scala index c013666281..6a1781f739 100644 --- a/http-core/src/test/scala/org/apache/pekko/http/scaladsl/model/headers/HeaderSpec.scala +++ b/http-core/src/test/scala/org/apache/pekko/http/scaladsl/model/headers/HeaderSpec.scala @@ -328,6 +328,23 @@ class HeaderSpec extends AnyFreeSpec with Matchers { } } } + "HttpCookie factories should" - { + "retain an ordinary name and value" in { + val cookie = HttpCookie("theme", "dark") + cookie.name shouldEqual "theme" + cookie.value shouldEqual "dark" + } + + "reject a non-token cookie name" in { + val error = the[IllegalArgumentException] thrownBy HttpCookie(" ", "dark") + error.getMessage should include("not allowed in cookie name") + } + + "reject a semicolon in a cookie value" in { + val error = the[IllegalArgumentException] thrownBy HttpCookie("theme", ";") + error.getMessage should include("not allowed in cookie content") + } + } "RawHeader should" - { "check for valid arguments" - { "successful parse run" in {