mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-10 17:45:35 +00:00
Merge pull request #725 from kevin1024/make-assert-is-json-less-misleading
assertions.py: Fix mis-leading `assert_is_json`
This commit is contained in:
@@ -11,9 +11,10 @@ def assert_cassette_has_one_response(cass):
|
|||||||
assert cass.play_count == 1
|
assert cass.play_count == 1
|
||||||
|
|
||||||
|
|
||||||
def assert_is_json(a_string):
|
def assert_is_json_bytes(b: bytes):
|
||||||
|
assert isinstance(b, bytes)
|
||||||
try:
|
try:
|
||||||
json.loads(a_string.decode("utf-8"))
|
json.loads(b.decode("utf-8"))
|
||||||
except Exception:
|
except Exception:
|
||||||
assert False
|
assert False
|
||||||
assert True
|
assert True
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from urllib.parse import urlencode
|
|||||||
from urllib.request import Request, urlopen
|
from urllib.request import Request, urlopen
|
||||||
|
|
||||||
import pytest
|
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
|
import vcr
|
||||||
|
|
||||||
@@ -105,7 +105,7 @@ def test_decompress_gzip(tmpdir, httpbin):
|
|||||||
with vcr.use_cassette(cass_file) as cass:
|
with vcr.use_cassette(cass_file) as cass:
|
||||||
decoded_response = urlopen(url).read()
|
decoded_response = urlopen(url).read()
|
||||||
assert_cassette_has_one_response(cass)
|
assert_cassette_has_one_response(cass)
|
||||||
assert_is_json(decoded_response)
|
assert_is_json_bytes(decoded_response)
|
||||||
|
|
||||||
|
|
||||||
def test_decomptess_empty_body(tmpdir, httpbin):
|
def test_decomptess_empty_body(tmpdir, httpbin):
|
||||||
@@ -129,7 +129,7 @@ def test_decompress_deflate(tmpdir, httpbin):
|
|||||||
with vcr.use_cassette(cass_file) as cass:
|
with vcr.use_cassette(cass_file) as cass:
|
||||||
decoded_response = urlopen(url).read()
|
decoded_response = urlopen(url).read()
|
||||||
assert_cassette_has_one_response(cass)
|
assert_cassette_has_one_response(cass)
|
||||||
assert_is_json(decoded_response)
|
assert_is_json_bytes(decoded_response)
|
||||||
|
|
||||||
|
|
||||||
def test_decompress_regular(tmpdir, httpbin):
|
def test_decompress_regular(tmpdir, httpbin):
|
||||||
@@ -141,7 +141,7 @@ def test_decompress_regular(tmpdir, httpbin):
|
|||||||
with vcr.use_cassette(cass_file) as cass:
|
with vcr.use_cassette(cass_file) as cass:
|
||||||
resp = urlopen(url).read()
|
resp = urlopen(url).read()
|
||||||
assert_cassette_has_one_response(cass)
|
assert_cassette_has_one_response(cass)
|
||||||
assert_is_json(resp)
|
assert_is_json_bytes(resp)
|
||||||
|
|
||||||
|
|
||||||
def test_before_record_request_corruption(tmpdir, httpbin):
|
def test_before_record_request_corruption(tmpdir, httpbin):
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"""Test requests' interaction with vcr"""
|
"""Test requests' interaction with vcr"""
|
||||||
import pytest
|
import pytest
|
||||||
from assertions import assert_cassette_empty, assert_is_json
|
from assertions import assert_cassette_empty, assert_is_json_bytes
|
||||||
|
|
||||||
import vcr
|
import vcr
|
||||||
|
|
||||||
@@ -176,7 +176,7 @@ def test_gzip__decode_compressed_response_false(tmpdir, httpbin_both):
|
|||||||
with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))):
|
with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))):
|
||||||
response = requests.get(httpbin_both + "/gzip")
|
response = requests.get(httpbin_both + "/gzip")
|
||||||
assert response.headers["content-encoding"] == "gzip" # i.e. not removed
|
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):
|
def test_gzip__decode_compressed_response_true(tmpdir, httpbin_both):
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import http.client as httplib
|
|||||||
import json
|
import json
|
||||||
import zlib
|
import zlib
|
||||||
|
|
||||||
from assertions import assert_is_json
|
from assertions import assert_is_json_bytes
|
||||||
|
|
||||||
import vcr
|
import vcr
|
||||||
|
|
||||||
@@ -84,7 +84,7 @@ def test_original_decoded_response_is_not_modified(tmpdir, httpbin):
|
|||||||
inside = conn.getresponse()
|
inside = conn.getresponse()
|
||||||
|
|
||||||
assert "content-encoding" not in inside.headers
|
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]"):
|
def _make_before_record_response(fields, replacement="[REDACTED]"):
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from assertions import assert_cassette_empty, assert_is_json
|
from assertions import assert_cassette_empty, assert_is_json_bytes
|
||||||
|
|
||||||
import vcr
|
import vcr
|
||||||
from vcr.errors import CannotOverwriteExistingCassetteException
|
from vcr.errors import CannotOverwriteExistingCassetteException
|
||||||
@@ -195,11 +195,11 @@ def test_gzip(get_client, tmpdir, scheme):
|
|||||||
|
|
||||||
with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))):
|
with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))):
|
||||||
response = yield get(get_client(), url, **kwargs)
|
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:
|
with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))) as cass:
|
||||||
response = yield get(get_client(), url, **kwargs)
|
response = yield get(get_client(), url, **kwargs)
|
||||||
assert_is_json(response.body)
|
assert_is_json_bytes(response.body)
|
||||||
assert 1 == cass.play_count
|
assert 1 == cass.play_count
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import pytest_httpbin
|
import pytest_httpbin
|
||||||
from assertions import assert_cassette_empty, assert_is_json
|
from assertions import assert_cassette_empty, assert_is_json_bytes
|
||||||
|
|
||||||
import vcr
|
import vcr
|
||||||
from vcr.patch import force_reset
|
from vcr.patch import force_reset
|
||||||
@@ -136,10 +136,10 @@ def test_gzip(tmpdir, httpbin_both, verify_pool_mgr):
|
|||||||
|
|
||||||
with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))):
|
with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))):
|
||||||
response = verify_pool_mgr.request("GET", url)
|
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"))):
|
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):
|
def test_https_with_cert_validation_disabled(tmpdir, httpbin_secure, pool_mgr):
|
||||||
|
|||||||
Reference in New Issue
Block a user