Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

urllib3.response.HTTPResponse.headers is a dict, but vcr.stubs.VCRHTTPResponse.headers is not #841

Open
johnjameswhitman opened this issue May 28, 2024 · 0 comments

Comments

@johnjameswhitman
Copy link

Problem

I'm dealing with some pydantic models that are deserializing a urrlib3.response.HTTPResponse, and they expect the headers attribute to be a dict. It looks like urllib3 deserializes headers into an HTTPHeaderDict that inherits from dict in the HTTPResponse constructor here:
https://github.com/urllib3/urllib3/blob/b07a669bd970d69847801148286b726f0570b625/src/urllib3/response.py#L327-L330

When I run a test w/ vcr, I'm getting a validation error about the headers not being a dict. After firing up a debugger, it seems like vcr.stubs.VCRHTTPResponse instead sets them to a http.client.HTTPMessage here:

self.headers = self.msg = parse_headers(headers)

Solution

Would it be possible to subclass, patch or otherwise modify how vcr handles response headers for urllib3? It looks like something similar might've been done for tornado, but I'm not super familiar with the available hooks:

response = HTTPResponse(
request,
code=vcr_response["status"]["code"],
reason=vcr_response["status"]["message"],
headers=headers,
buffer=BytesIO(vcr_response["body"]["string"]),
effective_url=vcr_response.get("url"),
request_time=self.io_loop.time() - request.start_time,
)
return callback(response)

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

No branches or pull requests

1 participant