Skip to content

aiohttp stubs aren't setting raw_headers #970

@jamesbraza

Description

@jamesbraza

karpetrosyan/httpx-aiohttp#29 moving to use raw_headers exposes that vcrpy's aiohttp stubs aren't setting raw_headers, and they should be: https://github.com/kevin1024/vcrpy/blob/v8.1.0/vcr/stubs/aiohttp_stubs.py#L70-L75

Here is a minimal reproducer with Python 3.13, aiohttp==3.13.2, vcrpy==8.1.0, and pytest==9:

import aiohttp
import pytest
from vcr.request import Request
from vcr.stubs.aiohttp_stubs import build_response


@pytest.mark.asyncio
async def test_minimal_repro_raw_headers() -> None:
    async with (
        aiohttp.ClientSession() as session,
        session.get("https://httpbingo.org/gzip") as real_response,
    ):
        real_response.raise_for_status()
        body = await real_response.read()
    assert real_response.raw_headers is not None, "Response should have raw_headers"

    vcr_request = Request(
        real_response.request_info.method,
        str(real_response.request_info.url),
        None,
        {k: [v] for k, v in real_response.request_info.headers.items()},
    )
    vcr_response = {
        "status": {"code": real_response.status, "message": real_response.reason},
        "headers": {k: [v] for k, v in real_response.headers.items()},
        "body": {"string": body},
    }
    vcr_mock_response = build_response(vcr_request, vcr_response, [])
    # This assertion will fail
    assert (
        vcr_mock_response.raw_headers is not None
    ), "Mock response should have raw_headers"

Here's a starting point for the fix:

response._raw_headers = tuple(
    (k.encode("utf-8"), v.encode("utf-8"))
    for k, v in response._headers.items()
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions