Skip to content

Commit

Permalink
Merge pull request #725 from kevin1024/make-assert-is-json-less-misle…
Browse files Browse the repository at this point in the history
…ading

assertions.py: Fix mis-leading `assert_is_json`
  • Loading branch information
hartwork authored Jun 26, 2023
2 parents 92ca5a1 + f21c8f0 commit 8c03c37
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 16 deletions.
5 changes: 3 additions & 2 deletions tests/assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ def assert_cassette_has_one_response(cass):
assert cass.play_count == 1


def assert_is_json(a_string):
def assert_is_json_bytes(b: bytes):
assert isinstance(b, bytes)
try:
json.loads(a_string.decode("utf-8"))
json.loads(b.decode("utf-8"))
except Exception:
assert False
assert True
8 changes: 4 additions & 4 deletions tests/integration/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from urllib.request import Request, urlopen

import pytest
from assertions import assert_cassette_has_one_response, assert_is_json
from assertions import assert_cassette_has_one_response, assert_is_json_bytes

import vcr

Expand Down Expand Up @@ -105,7 +105,7 @@ def test_decompress_gzip(tmpdir, httpbin):
with vcr.use_cassette(cass_file) as cass:
decoded_response = urlopen(url).read()
assert_cassette_has_one_response(cass)
assert_is_json(decoded_response)
assert_is_json_bytes(decoded_response)


def test_decomptess_empty_body(tmpdir, httpbin):
Expand All @@ -129,7 +129,7 @@ def test_decompress_deflate(tmpdir, httpbin):
with vcr.use_cassette(cass_file) as cass:
decoded_response = urlopen(url).read()
assert_cassette_has_one_response(cass)
assert_is_json(decoded_response)
assert_is_json_bytes(decoded_response)


def test_decompress_regular(tmpdir, httpbin):
Expand All @@ -141,7 +141,7 @@ def test_decompress_regular(tmpdir, httpbin):
with vcr.use_cassette(cass_file) as cass:
resp = urlopen(url).read()
assert_cassette_has_one_response(cass)
assert_is_json(resp)
assert_is_json_bytes(resp)


def test_before_record_request_corruption(tmpdir, httpbin):
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_requests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Test requests' interaction with vcr"""
import pytest
from assertions import assert_cassette_empty, assert_is_json
from assertions import assert_cassette_empty, assert_is_json_bytes

import vcr

Expand Down Expand Up @@ -176,7 +176,7 @@ def test_gzip__decode_compressed_response_false(tmpdir, httpbin_both):
with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))):
response = requests.get(httpbin_both + "/gzip")
assert response.headers["content-encoding"] == "gzip" # i.e. not removed
assert_is_json(response.content) # i.e. uncompressed bytes
assert_is_json_bytes(response.content) # i.e. uncompressed bytes


def test_gzip__decode_compressed_response_true(tmpdir, httpbin_both):
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import zlib

from assertions import assert_is_json
from assertions import assert_is_json_bytes

import vcr

Expand Down Expand Up @@ -84,7 +84,7 @@ def test_original_decoded_response_is_not_modified(tmpdir, httpbin):
inside = conn.getresponse()

assert "content-encoding" not in inside.headers
assert_is_json(inside.read())
assert_is_json_bytes(inside.read())


def _make_before_record_response(fields, replacement="[REDACTED]"):
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json

import pytest
from assertions import assert_cassette_empty, assert_is_json
from assertions import assert_cassette_empty, assert_is_json_bytes

import vcr
from vcr.errors import CannotOverwriteExistingCassetteException
Expand Down Expand Up @@ -195,11 +195,11 @@ def test_gzip(get_client, tmpdir, scheme):

with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))):
response = yield get(get_client(), url, **kwargs)
assert_is_json(response.body)
assert_is_json_bytes(response.body)

with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))) as cass:
response = yield get(get_client(), url, **kwargs)
assert_is_json(response.body)
assert_is_json_bytes(response.body)
assert 1 == cass.play_count


Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_urllib3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest
import pytest_httpbin
from assertions import assert_cassette_empty, assert_is_json
from assertions import assert_cassette_empty, assert_is_json_bytes

import vcr
from vcr.patch import force_reset
Expand Down Expand Up @@ -136,10 +136,10 @@ def test_gzip(tmpdir, httpbin_both, verify_pool_mgr):

with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))):
response = verify_pool_mgr.request("GET", url)
assert_is_json(response.data)
assert_is_json_bytes(response.data)

with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))):
assert_is_json(response.data)
assert_is_json_bytes(response.data)


def test_https_with_cert_validation_disabled(tmpdir, httpbin_secure, pool_mgr):
Expand Down

0 comments on commit 8c03c37

Please sign in to comment.