Skip to content

Commit

Permalink
make cassettes human readable
Browse files Browse the repository at this point in the history
  • Loading branch information
parkerhancock authored and jairhenrique committed Dec 12, 2023
1 parent dbf7a33 commit db1e9e7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
26 changes: 26 additions & 0 deletions tests/integration/test_httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,29 @@ def test_stream(tmpdir, httpbin, do_request):
assert cassette_content == response_content
assert len(cassette_content) == 512
assert cassette.play_count == 1

@pytest.mark.online
def test_text_content_type(tmpdir, httpbin, do_request):
url = httpbin.url + "/json"

with vcr.use_cassette(str(tmpdir.join("json_type.yaml"))):
response = do_request()("GET", url)

with vcr.use_cassette(str(tmpdir.join("json_type.yaml"))) as cassette:
cassette_response = do_request()("GET", url)
assert cassette_response.content == response.content
assert cassette.play_count == 1
assert isinstance(cassette.responses[0]['content'], str)

@pytest.mark.online
def test_binary_content_type(tmpdir, httpbin, do_request):
url = httpbin.url + "/bytes/1024"

with vcr.use_cassette(str(tmpdir.join("json_type.yaml"))):
response = do_request()("GET", url)

with vcr.use_cassette(str(tmpdir.join("json_type.yaml"))) as cassette:
cassette_response = do_request()("GET", url)
assert cassette_response.content == response.content
assert cassette.play_count == 1
assert isinstance(cassette.responses[0]['content'], bytes)
10 changes: 9 additions & 1 deletion vcr/stubs/httpx_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@ def _transform_headers(httpx_response):
return out



def _to_serialized_response(httpx_response):
try:
content = httpx_response.content.decode("utf-8")
except UnicodeDecodeError:
content = httpx_response.content

return {
"status_code": httpx_response.status_code,
"http_version": httpx_response.http_version,
"headers": _transform_headers(httpx_response),
"content": httpx_response.content,
"content": content,
}


Expand All @@ -58,6 +64,8 @@ def _from_serialized_headers(headers):
@patch("httpx.Response.read", MagicMock())
def _from_serialized_response(request, serialized_response, history=None):
content = serialized_response.get("content")
if isinstance(content, str):
content = content.encode("utf-8")
response = httpx.Response(
status_code=serialized_response.get("status_code"),
request=request,
Expand Down

0 comments on commit db1e9e7

Please sign in to comment.