Skip to content

Commit 8c03c37

Browse files
authored
Merge pull request #725 from kevin1024/make-assert-is-json-less-misleading
assertions.py: Fix mis-leading `assert_is_json`
2 parents 92ca5a1 + f21c8f0 commit 8c03c37

File tree

6 files changed

+17
-16
lines changed

6 files changed

+17
-16
lines changed

tests/assertions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ def assert_cassette_has_one_response(cass):
1111
assert cass.play_count == 1
1212

1313

14-
def assert_is_json(a_string):
14+
def assert_is_json_bytes(b: bytes):
15+
assert isinstance(b, bytes)
1516
try:
16-
json.loads(a_string.decode("utf-8"))
17+
json.loads(b.decode("utf-8"))
1718
except Exception:
1819
assert False
1920
assert True

tests/integration/test_filter.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from urllib.request import Request, urlopen
66

77
import pytest
8-
from assertions import assert_cassette_has_one_response, assert_is_json
8+
from assertions import assert_cassette_has_one_response, assert_is_json_bytes
99

1010
import vcr
1111

@@ -105,7 +105,7 @@ def test_decompress_gzip(tmpdir, httpbin):
105105
with vcr.use_cassette(cass_file) as cass:
106106
decoded_response = urlopen(url).read()
107107
assert_cassette_has_one_response(cass)
108-
assert_is_json(decoded_response)
108+
assert_is_json_bytes(decoded_response)
109109

110110

111111
def test_decomptess_empty_body(tmpdir, httpbin):
@@ -129,7 +129,7 @@ def test_decompress_deflate(tmpdir, httpbin):
129129
with vcr.use_cassette(cass_file) as cass:
130130
decoded_response = urlopen(url).read()
131131
assert_cassette_has_one_response(cass)
132-
assert_is_json(decoded_response)
132+
assert_is_json_bytes(decoded_response)
133133

134134

135135
def test_decompress_regular(tmpdir, httpbin):
@@ -141,7 +141,7 @@ def test_decompress_regular(tmpdir, httpbin):
141141
with vcr.use_cassette(cass_file) as cass:
142142
resp = urlopen(url).read()
143143
assert_cassette_has_one_response(cass)
144-
assert_is_json(resp)
144+
assert_is_json_bytes(resp)
145145

146146

147147
def test_before_record_request_corruption(tmpdir, httpbin):

tests/integration/test_requests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Test requests' interaction with vcr"""
22
import pytest
3-
from assertions import assert_cassette_empty, assert_is_json
3+
from assertions import assert_cassette_empty, assert_is_json_bytes
44

55
import vcr
66

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

181181

182182
def test_gzip__decode_compressed_response_true(tmpdir, httpbin_both):

tests/integration/test_stubs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
import zlib
44

5-
from assertions import assert_is_json
5+
from assertions import assert_is_json_bytes
66

77
import vcr
88

@@ -84,7 +84,7 @@ def test_original_decoded_response_is_not_modified(tmpdir, httpbin):
8484
inside = conn.getresponse()
8585

8686
assert "content-encoding" not in inside.headers
87-
assert_is_json(inside.read())
87+
assert_is_json_bytes(inside.read())
8888

8989

9090
def _make_before_record_response(fields, replacement="[REDACTED]"):

tests/integration/test_tornado.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import json
55

66
import pytest
7-
from assertions import assert_cassette_empty, assert_is_json
7+
from assertions import assert_cassette_empty, assert_is_json_bytes
88

99
import vcr
1010
from vcr.errors import CannotOverwriteExistingCassetteException
@@ -195,11 +195,11 @@ def test_gzip(get_client, tmpdir, scheme):
195195

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

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

205205

tests/integration/test_urllib3.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66
import pytest_httpbin
7-
from assertions import assert_cassette_empty, assert_is_json
7+
from assertions import assert_cassette_empty, assert_is_json_bytes
88

99
import vcr
1010
from vcr.patch import force_reset
@@ -136,10 +136,10 @@ def test_gzip(tmpdir, httpbin_both, verify_pool_mgr):
136136

137137
with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))):
138138
response = verify_pool_mgr.request("GET", url)
139-
assert_is_json(response.data)
139+
assert_is_json_bytes(response.data)
140140

141141
with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))):
142-
assert_is_json(response.data)
142+
assert_is_json_bytes(response.data)
143143

144144

145145
def test_https_with_cert_validation_disabled(tmpdir, httpbin_secure, pool_mgr):

0 commit comments

Comments
 (0)