Skip to content

Fix multipart matching with custom boundaries - #249

Open
AryanRG wants to merge 2 commits into
Colin-b:developfrom
AryanRG:fix/custom-multipart-boundaries
Open

AryanRG wants to merge 2 commits into
Colin-b:developfrom
AryanRG:fix/custom-multipart-boundaries

Conversation

@AryanRG

@AryanRG AryanRG commented Oct 17, 2026 •

Copy link
Copy Markdown

Multipart uploads with an explicit boundary such as test-boundary currently fail match_files because the matcher only extracts lowercase hexadecimal boundaries. This also makes get_requests(match_files=...) return no matching requests.

The matcher now reuses a nonempty ASCII boundary from the first body line and retains the full-body equality check. Non-ASCII prefixes remain nonmatches, so arbitrary binary uploads can reach later response matchers without an ASCII-decoding error. Includes sync/async selection and retrieval regressions and an Unreleased changelog entry.

Reproduction (HTTPX 0.28.1):

import httpx

def test_custom_boundary(httpx_mock):
    files = {"name": ("file_name", b"File content")}
    httpx_mock.add_response(match_files=files, status_code=201)
    with httpx.Client() as client:
        response = client.put(
            "https://test_url",
            headers={"Content-Type": "multipart/form-data; boundary=test-boundary"},
            files=files,
        )
    assert response.status_code == 201

Before: httpx.TimeoutException and an unused response at teardown. After: HTTP 201. For retrieval, register an unrestricted response, send the same request, then compare httpx_mock.get_requests(match_files=files) with [response.request]: before [], after the captured request.

Validation on macOS arm64: the full suite passed on Python 3.11.16 and 3.13.12 (390 tests each; 100% statement/branch coverage), 170 additional probes passed, and all eight pre-commit hooks passed. Python 3.12/3.14/3.15, Linux/Windows, and package builds were not run locally; upstream CI is separate. This retains the existing body-based comparison and does not add strict multipart syntax or header/body consistency validation.

Overlap: #239 is currently open and does not include this fix. If that migration lands first, these two new test modules need adaptation to its backend fixture and fresh HTTPX-only, HTTPX2-only, and combined validation; their direct import httpx currently fails collection with only HTTPX2 installed.

AI assistance was used to prepare implementation and regression tests.

@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant