From 32887a7dd036c53837a22b173925191555ab1f19 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Wed, 28 Oct 2026 18:42:21 +0000 Subject: [PATCH 1/4] =?UTF-8?q?test:=20cover=20repeated=20headers=20per=20?= =?UTF-8?q?RFC=209110=20=C2=A75.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- .../interaction/test_http_repeated_headers.py | 233 ++++++++++++++++++ 1 file changed, 233 insertions(+) create mode 100644 tests/interaction/test_http_repeated_headers.py diff --git a/tests/interaction/test_http_repeated_headers.py b/tests/interaction/test_http_repeated_headers.py new file mode 100644 index 000000000..da5267807 --- /dev/null +++ b/tests/interaction/test_http_repeated_headers.py @@ -0,0 +1,233 @@ +""" +Repeated request header matching by the mock server. + +[RFC 9110 §5.3](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.3) +allows a header which is repeated over several field lines to be combined into +a single comma-separated line. The mock server must therefore treat the +following requests as equivalent, unless the header's values cannot be joined +(as with `Content-Type`): + +```http +X-Request: a +X-Request: b +``` + +```http +X-Request: a, b +``` + +HTTP clients merge, reorder or reject repeated header lines, so these tests +write the request to a raw socket to control exactly which field lines reach +the mock server. +""" + +from __future__ import annotations + +import logging +import socket +from typing import TYPE_CHECKING + +import pytest + +from pact import Pact, match + +if TYPE_CHECKING: + from collections.abc import Sequence + + from pact.match.matcher import AbstractMatcher + from pact.pact import PactServer + +RFC_9110_WARNING = "RFC 9110 §5.3" +""" +Marker in the warning logged when field lines only match one at a time. +""" + +LINK_A = '; rel="a"' +LINK_B = '; rel="b"' + + +def first_n(example: str) -> AbstractMatcher[str]: + """ + Regex matcher for values of the form `first-`. + """ + return match.regex(example, regex=r"^first-\d+$") + + +def send_raw_request(srv: PactServer, header: str, lines: Sequence[str]) -> int: + """ + Send `GET /headers` with each value in `lines` as its own field line. + + Args: + srv: + Running mock server. + + header: + Name of the repeated header. + + lines: + Values of the header, one per field line, in order. + + Returns: + The response status code. + """ + assert srv.port is not None + request = ( + "GET /headers HTTP/1.1\r\n" + f"Host: {srv.host}:{srv.port}\r\n" + "Connection: close\r\n" + + "".join(f"{header}: {line}\r\n" for line in lines) + + "\r\n" + ) + with socket.create_connection((srv.host, srv.port)) as conn: + conn.sendall(request.encode()) + with conn.makefile("rb") as response: + status_line = response.readline().decode() + return int(status_line.split(" ")[1]) + + +def rfc_9110_warnings(caplog: pytest.LogCaptureFixture) -> list[str]: + """ + Messages of the RFC 9110 §5.3 warnings forwarded from the FFI. + """ + return [ + record.getMessage() + for record in caplog.records + if record.name.startswith("pact.ffi") + and record.levelno == logging.WARNING + and RFC_9110_WARNING in record.getMessage() + ] + + +@pytest.mark.parametrize( + ("header", "expected", "lines", "matches"), + [ + pytest.param( + "X-Request", + ["a", "b"], + ["a", "b"], + True, + id="H1-separate-lines", + ), + pytest.param( + "X-Request", + ["a", "b"], + ["a, b"], + True, + id="H2-combined-line", + ), + pytest.param( + "X-Sunset", + ["Sat, 01 Jan 2028 00:00:00 GMT", "Sun, 02 Jan 2028 00:00:00 GMT"], + ["Sat, 01 Jan 2028 00:00:00 GMT", "Sun, 02 Jan 2028 00:00:00 GMT"], + True, + id="H3-values-with-commas-separate-lines", + ), + pytest.param( + "Content-Type", + ["text/plain", "text/html"], + ["text/plain, text/html"], + False, + id="H4-content-type-combined-line", + ), + pytest.param( + "Cache-Control", + ["no-cache", "no-store"], + ["no-cache, no-store"], + True, + id="H5-cache-control-combined-line", + ), + pytest.param( + "Link", + [LINK_A, LINK_B], + [LINK_A, LINK_B], + True, + id="H8a-link-separate-lines", + ), + pytest.param( + "Link", + [LINK_A, LINK_B], + [f"{LINK_A}, {LINK_B}"], + True, + id="H8b-link-combined-line", + ), + ], +) +def test_repeated_request_header( + header: str, + expected: Sequence[str], + lines: Sequence[str], + matches: bool, # noqa: FBT001 +) -> None: + pact = Pact("consumer", "provider") + interaction = pact.upon_receiving(f"a request with a repeated {header} header") + interaction.with_request("GET", "/headers") + for value in expected: + interaction.with_header(header, value) + interaction.will_respond_with(200) + + with pact.serve(raises=False) as srv: + status = send_raw_request(srv, header, lines) + mismatches = srv.mismatches + + if matches: + assert status == 200 + assert mismatches == [] + else: + assert status != 200 + assert mismatches + + +def test_repeated_request_header_matcher_per_value( + caplog: pytest.LogCaptureFixture, +) -> None: + """ + H6a: each field line is matched by the matcher at its own index. + """ + pact = Pact("consumer", "provider") + ( + pact + .upon_receiving("a request with a matcher per repeated header value") + .with_request("GET", "/headers") + .with_header("X-Request", first_n("first-1")) + .with_header("X-Request", first_n("first-2")) + .will_respond_with(200) + ) + + with pact.serve(raises=False) as srv: + status = send_raw_request(srv, "X-Request", ["first-42", "first-99"]) + mismatches = srv.mismatches + + assert status == 200 + assert mismatches == [] + assert rfc_9110_warnings(caplog) == [] + + +def test_repeated_request_header_per_line_fallback_warns( + caplog: pytest.LogCaptureFixture, +) -> None: + """ + H6b: a match only found by comparing field lines one at a time is logged. + + The expected `"x , y"` differs from the sent `"x, y"` only in whitespace + inside a quoted string, so the values only match when the field lines are + compared one at a time rather than joined. + """ + pact = Pact("consumer", "provider") + ( + pact + .upon_receiving("a request with a quoted repeated header value") + .with_request("GET", "/headers") + .with_header("X-Request", '"x , y"') + .with_header("X-Request", "z") + .will_respond_with(200) + ) + + with pact.serve(raises=False) as srv: + status = send_raw_request(srv, "X-Request", ['"x, y"', "z"]) + mismatches = srv.mismatches + + assert status == 200 + assert mismatches == [] + warnings = rfc_9110_warnings(caplog) + assert len(warnings) == 1 + assert "compared one at a time" in warnings[0] From 6527e4477004638a2415eb983c2a3d291b665b36 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Mon, 9 Nov 2026 17:25:21 +0000 Subject: [PATCH 2/4] test: use pact_ffi 0.5.13-rc.1 from YOU54F fork (revert before merge) Points the FFI build hook at the YOU54F/pact-reference prerelease, and lets test_version accept the -rc.1 suffix in pactffi_version(). Co-Authored-By: Claude Opus 5.5 --- pact-python-ffi/hatch_build.py | 7 ++++++- pact-python-ffi/tests/test_init.py | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pact-python-ffi/hatch_build.py b/pact-python-ffi/hatch_build.py index fdd3c4284..6ba4a06d3 100644 --- a/pact-python-ffi/hatch_build.py +++ b/pact-python-ffi/hatch_build.py @@ -28,7 +28,11 @@ from collections.abc import Mapping, MutableMapping, Sequence PKG_DIR = Path(__file__).parent.resolve() / "src" / "pact_ffi" -PACT_RELEASE_URL = "https://github.com/pact-foundation/pact-reference/releases/download/pact_ffi-v{version}" +# TEMP: fork prerelease, revert before merge. +PACT_RELEASE_URL = ( + "https://github.com/YOU54F/pact-reference/releases/download/pact_ffi-v{version}" +) +PACT_FFI_VERSION_OVERRIDE = "0.5.13-rc.1" def _stable_abi() -> str: @@ -133,6 +137,7 @@ def initialize( incompatible platform). """ ffi_version = ".".join(self.metadata.version.split(".")[:3]) + ffi_version = PACT_FFI_VERSION_OVERRIDE # TEMP: revert before merge if not ffi_version: msg = "Failed to determine Pact FFI version." self.app.display_error(msg) diff --git a/pact-python-ffi/tests/test_init.py b/pact-python-ffi/tests/test_init.py index 05a9d7140..8ddd4c4f1 100644 --- a/pact-python-ffi/tests/test_init.py +++ b/pact-python-ffi/tests/test_init.py @@ -28,7 +28,8 @@ def test_version() -> None: assert isinstance(pact_ffi.version(), str) assert len(pact_ffi.version()) > 0 - assert pact_ffi.version().count(".") == 2 + # TEMP: allow the 0.5.13-rc.1 prerelease suffix, revert before merge. + assert pact_ffi.version().split("-")[0].count(".") == 2 def test_string_result_ok() -> None: From 722e0eb1f87f009fd2995868a6dd44988f83461c Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Mon, 9 Nov 2026 17:25:21 +0000 Subject: [PATCH 3/4] chore(ci): run tests on draft PRs (revert before merge) Every job in test.yml is skipped on draft PRs, so the fork FFI prerelease would never be exercised on this draft PR. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/test.yml | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c001f3b2c..5950fa4d0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -56,7 +56,8 @@ jobs: Test Python ${{ matrix.python-version }} on ${{ startsWith(matrix.os, 'macos-') && 'macOS' || startsWith(matrix.os, 'windows-') && 'Windows' || 'Linux' }} - if: github.event_name != 'pull_request' || !github.event.pull_request.draft + # TEMP: run on draft PRs to test the fork FFI prerelease, revert before merge. + if: true runs-on: ${{ matrix.os }} strategy: @@ -135,7 +136,8 @@ jobs: Test Python Example ${{ matrix.python-version }} on ${{ startsWith(matrix.os, 'macos-') && 'macOS' || startsWith(matrix.os, 'windows-') && 'Windows' || 'Linux' }} - if: github.event_name != 'pull_request' || !github.event.pull_request.draft + # TEMP: run on draft PRs to test the fork FFI prerelease, revert before merge. + if: true runs-on: ${{ matrix.os }} strategy: @@ -193,7 +195,8 @@ jobs: format: name: Format - if: github.event_name != 'pull_request' || !github.event.pull_request.draft + # TEMP: run on draft PRs to test the fork FFI prerelease, revert before merge. + if: true runs-on: ubuntu-latest steps: @@ -221,7 +224,8 @@ jobs: run: hatch run format --check --output-format github lint: name: Lint - if: github.event_name != 'pull_request' || !github.event.pull_request.draft + # TEMP: run on draft PRs to test the fork FFI prerelease, revert before merge. + if: true runs-on: ubuntu-latest steps: @@ -250,7 +254,8 @@ jobs: typecheck: name: Typecheck - if: github.event_name != 'pull_request' || !github.event.pull_request.draft + # TEMP: run on draft PRs to test the fork FFI prerelease, revert before merge. + if: true runs-on: ubuntu-latest steps: @@ -275,7 +280,8 @@ jobs: prek: name: Prek - if: github.event_name != 'pull_request' || !github.event.pull_request.draft + # TEMP: run on draft PRs to test the fork FFI prerelease, revert before merge. + if: true runs-on: ubuntu-latest steps: From 654ca88097a70d6a884ddd390ede0654af3b6289 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Mon, 9 Nov 2026 19:02:11 +0000 Subject: [PATCH 4/4] test: quoted-string whitespace in repeated headers is literal (H6b no longer matches) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per RFC 9110 §5.6.4 quoted-string content is literal, so `"x , y"` no longer matches `"x, y"` and no RFC 9110 or deprecation warning is logged. Co-Authored-By: Claude Opus 5.5 --- .../interaction/test_http_repeated_headers.py | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/tests/interaction/test_http_repeated_headers.py b/tests/interaction/test_http_repeated_headers.py index da5267807..8367b1475 100644 --- a/tests/interaction/test_http_repeated_headers.py +++ b/tests/interaction/test_http_repeated_headers.py @@ -202,15 +202,17 @@ def test_repeated_request_header_matcher_per_value( assert rfc_9110_warnings(caplog) == [] -def test_repeated_request_header_per_line_fallback_warns( +def test_repeated_request_header_quoted_whitespace_is_literal( caplog: pytest.LogCaptureFixture, ) -> None: """ - H6b: a match only found by comparing field lines one at a time is logged. + H6b: whitespace inside a quoted string is significant. - The expected `"x , y"` differs from the sent `"x, y"` only in whitespace - inside a quoted string, so the values only match when the field lines are - compared one at a time rather than joined. + Per [RFC 9110 + §5.6.4](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.4), the + content of a quoted string is literal. The expected `"x , y"` therefore + does not match the sent `"x, y"`, whether the field lines are joined or + compared one at a time, and no RFC 9110 or deprecation warning is logged. """ pact = Pact("consumer", "provider") ( @@ -226,8 +228,13 @@ def test_repeated_request_header_per_line_fallback_warns( status = send_raw_request(srv, "X-Request", ['"x, y"', "z"]) mismatches = srv.mismatches - assert status == 200 - assert mismatches == [] - warnings = rfc_9110_warnings(caplog) - assert len(warnings) == 1 - assert "compared one at a time" in warnings[0] + assert status != 200 + assert mismatches + assert rfc_9110_warnings(caplog) == [] + assert not [ + record.getMessage() + for record in caplog.records + if record.name.startswith("pact.ffi") + and record.levelno == logging.WARNING + and "deprecat" in record.getMessage().lower() + ]