From 69e431654591f3e9ced4ba5236265de303c6e973 Mon Sep 17 00:00:00 2001 From: Paulo Romeira Date: Wed, 3 Jun 2020 19:06:52 -0300 Subject: [PATCH] aiohttp: Add support for multiple headers with same key --- vcr/stubs/aiohttp_stubs/__init__.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/vcr/stubs/aiohttp_stubs/__init__.py b/vcr/stubs/aiohttp_stubs/__init__.py index be34d42..e1cdad1 100644 --- a/vcr/stubs/aiohttp_stubs/__init__.py +++ b/vcr/stubs/aiohttp_stubs/__init__.py @@ -59,14 +59,14 @@ def build_response(vcr_request, vcr_response, history): request_info = RequestInfo( url=URL(vcr_request.url), method=vcr_request.method, - headers=CIMultiDictProxy(CIMultiDict(vcr_request.headers)), + headers=_deserialize_headers(vcr_request.headers), real_url=URL(vcr_request.url), ) response = MockClientResponse(vcr_request.method, URL(vcr_response.get("url")), request_info=request_info) response.status = vcr_response["status"]["code"] response._body = vcr_response["body"].get("string", b"") response.reason = vcr_response["status"]["message"] - response._headers = CIMultiDictProxy(CIMultiDict(vcr_response["headers"])) + response._headers = _deserialize_headers(vcr_response["headers"]) response._history = tuple(history) response.close() @@ -81,7 +81,23 @@ def _serialize_headers(headers): """ # Mark strings as keys so 'istr' types don't show up in # the cassettes as comments. - return {str(k): v for k, v in headers.items()} + serialized_headers = {} + for k, v in headers.items(): + serialized_headers.setdefault(str(k), []).append(v) + + return serialized_headers + + +def _deserialize_headers(headers): + deserialized_headers = CIMultiDict() + for k, vs in headers.items(): + if isinstance(vs, list): + for v in vs: + deserialized_headers.add(k, v) + else: + deserialized_headers.add(k, vs) + + return CIMultiDictProxy(deserialized_headers) def play_responses(cassette, vcr_request):