Skip to content

Commit

Permalink
Run linters.
Browse files Browse the repository at this point in the history
  • Loading branch information
Allan Crooks authored and jairhenrique committed Jan 23, 2024
1 parent c548738 commit 54bc646
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
7 changes: 5 additions & 2 deletions tests/integration/test_httpx.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import pytest
import os

import pytest

import vcr

from ..assertions import assert_is_json_bytes

asyncio = pytest.importorskip("asyncio")
httpx = pytest.importorskip("httpx")

from ..assertions import assert_is_json_bytes

@pytest.fixture(params=["https", "http"])
def scheme(request):
Expand Down
21 changes: 12 additions & 9 deletions vcr/stubs/httpx_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import httpx

from vcr.errors import CannotOverwriteExistingCassetteException
from vcr.request import Request as VcrRequest
from vcr.filters import decode_response
from vcr.request import Request as VcrRequest
from vcr.serializers.compat import convert_body_to_bytes

_httpx_signature = inspect.signature(httpx.Client.request)
Expand Down Expand Up @@ -37,7 +37,6 @@ def _transform_headers(httpx_response):


async def _to_serialized_response(resp, aread):

# The content shouldn't already have been read in by HTTPX.
assert not hasattr(resp, "_decoder")

Expand All @@ -49,7 +48,7 @@ async def _to_serialized_response(resp, aread):
resp.read()

result = {
"status": dict(code=resp.status_code, message=resp.reason_phrase),
"status": {"code": resp.status_code, "message": resp.reason_phrase},
"headers": _transform_headers(resp),
"body": {"string": resp.content},
}
Expand All @@ -60,6 +59,7 @@ async def _to_serialized_response(resp, aread):
resp._content = resp._get_content_decoder().decode(resp.content)
return result


def _from_serialized_headers(headers):
"""
httpx accepts headers as list of tuples of header key and value.
Expand All @@ -75,16 +75,19 @@ def _from_serialized_headers(headers):
@patch("httpx.Response.close", MagicMock())
@patch("httpx.Response.read", MagicMock())
def _from_serialized_response(request, serialized_response, history=None):

# Cassette format generated for HTTPX requests by older versions of
# vcrpy. We restructure the content to resemble what a regular
# cassette looks like.
if "status_code" in serialized_response:
serialized_response = decode_response(convert_body_to_bytes({
'headers': serialized_response['headers'],
'body': {'string': serialized_response['content']},
'status': {'code': serialized_response['status_code']},
}))
serialized_response = decode_response(
convert_body_to_bytes(
{
"headers": serialized_response["headers"],
"body": {"string": serialized_response["content"]},
"status": {"code": serialized_response["status_code"]},
},
),
)
extensions = None
else:
extensions = {"reason_phrase": serialized_response["status"]["message"].encode()}
Expand Down

0 comments on commit 54bc646

Please sign in to comment.