1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-09 17:15:35 +00:00

fix tests

This commit is contained in:
Parker Hancock
2023-12-08 10:49:29 -06:00
committed by Jair Henrique
parent e8e9a4af9f
commit f5fc7aac22
14 changed files with 101 additions and 112 deletions

View File

@@ -4,22 +4,6 @@ import ssl
import pytest import pytest
@pytest.fixture(params=["https", "http"])
def scheme(request):
"""Fixture that returns both http and https."""
return request.param
@pytest.fixture
def mockbin(scheme):
return scheme + "://mockbin.org"
@pytest.fixture
def mockbin_request_url(mockbin):
return mockbin + "/request"
@pytest.fixture @pytest.fixture
def httpbin_ssl_context(): def httpbin_ssl_context():
ssl_ca_location = os.environ["REQUESTS_CA_BUNDLE"] ssl_ca_location = os.environ["REQUESTS_CA_BUNDLE"]

View File

@@ -36,8 +36,8 @@ def post(url, output="text", **kwargs):
@pytest.mark.online @pytest.mark.online
def test_status(tmpdir, mockbin_request_url): def test_status(tmpdir, httpbin):
url = mockbin_request_url url = httpbin.url
with vcr.use_cassette(str(tmpdir.join("status.yaml"))): with vcr.use_cassette(str(tmpdir.join("status.yaml"))):
response, _ = get(url) response, _ = get(url)
@@ -50,8 +50,8 @@ def test_status(tmpdir, mockbin_request_url):
@pytest.mark.online @pytest.mark.online
@pytest.mark.parametrize("auth", [None, aiohttp.BasicAuth("vcrpy", "test")]) @pytest.mark.parametrize("auth", [None, aiohttp.BasicAuth("vcrpy", "test")])
def test_headers(tmpdir, auth, mockbin_request_url): def test_headers(tmpdir, auth, httpbin):
url = mockbin_request_url url = httpbin.url
with vcr.use_cassette(str(tmpdir.join("headers.yaml"))): with vcr.use_cassette(str(tmpdir.join("headers.yaml"))):
response, _ = get(url, auth=auth) response, _ = get(url, auth=auth)
@@ -67,8 +67,8 @@ def test_headers(tmpdir, auth, mockbin_request_url):
@pytest.mark.online @pytest.mark.online
def test_case_insensitive_headers(tmpdir, mockbin_request_url): def test_case_insensitive_headers(tmpdir, httpbin):
url = mockbin_request_url url = httpbin.url
with vcr.use_cassette(str(tmpdir.join("whatever.yaml"))): with vcr.use_cassette(str(tmpdir.join("whatever.yaml"))):
_, _ = get(url) _, _ = get(url)
@@ -81,8 +81,8 @@ def test_case_insensitive_headers(tmpdir, mockbin_request_url):
@pytest.mark.online @pytest.mark.online
def test_text(tmpdir, mockbin_request_url): def test_text(tmpdir, httpbin):
url = mockbin_request_url url = httpbin.url
with vcr.use_cassette(str(tmpdir.join("text.yaml"))): with vcr.use_cassette(str(tmpdir.join("text.yaml"))):
_, response_text = get(url) _, response_text = get(url)
@@ -94,8 +94,8 @@ def test_text(tmpdir, mockbin_request_url):
@pytest.mark.online @pytest.mark.online
def test_json(tmpdir, mockbin_request_url): def test_json(tmpdir, httpbin):
url = mockbin_request_url url = httpbin.url + "/json"
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
with vcr.use_cassette(str(tmpdir.join("json.yaml"))): with vcr.use_cassette(str(tmpdir.join("json.yaml"))):
@@ -108,8 +108,8 @@ def test_json(tmpdir, mockbin_request_url):
@pytest.mark.online @pytest.mark.online
def test_binary(tmpdir, mockbin_request_url): def test_binary(tmpdir, httpbin):
url = mockbin_request_url + "/image/png" url = httpbin.url + "/image/png"
with vcr.use_cassette(str(tmpdir.join("binary.yaml"))): with vcr.use_cassette(str(tmpdir.join("binary.yaml"))):
_, response_binary = get(url, output="raw") _, response_binary = get(url, output="raw")
@@ -120,8 +120,8 @@ def test_binary(tmpdir, mockbin_request_url):
@pytest.mark.online @pytest.mark.online
def test_stream(tmpdir, mockbin_request_url): def test_stream(tmpdir, httpbin):
url = mockbin_request_url url = httpbin.url
with vcr.use_cassette(str(tmpdir.join("stream.yaml"))): with vcr.use_cassette(str(tmpdir.join("stream.yaml"))):
_, body = get(url, output="raw") # Do not use stream here, as the stream is exhausted by vcr _, body = get(url, output="raw") # Do not use stream here, as the stream is exhausted by vcr
@@ -134,10 +134,10 @@ def test_stream(tmpdir, mockbin_request_url):
@pytest.mark.online @pytest.mark.online
@pytest.mark.parametrize("body", ["data", "json"]) @pytest.mark.parametrize("body", ["data", "json"])
def test_post(tmpdir, body, caplog, mockbin_request_url): def test_post(tmpdir, body, caplog, httpbin):
caplog.set_level(logging.INFO) caplog.set_level(logging.INFO)
data = {"key1": "value1", "key2": "value2"} data = {"key1": "value1", "key2": "value2"}
url = mockbin_request_url url = httpbin.url
with vcr.use_cassette(str(tmpdir.join("post.yaml"))): with vcr.use_cassette(str(tmpdir.join("post.yaml"))):
_, response_json = post(url, **{body: data}) _, response_json = post(url, **{body: data})
@@ -159,14 +159,14 @@ def test_post(tmpdir, body, caplog, mockbin_request_url):
@pytest.mark.online @pytest.mark.online
def test_params(tmpdir, mockbin_request_url): def test_params(tmpdir, httpbin):
url = mockbin_request_url + "?d=d" url = httpbin.url + "/get?d=d"
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
params = {"a": 1, "b": 2, "c": "c"} params = {"a": 1, "b": 2, "c": "c"}
with vcr.use_cassette(str(tmpdir.join("get.yaml"))) as cassette: with vcr.use_cassette(str(tmpdir.join("get.yaml"))) as cassette:
_, response_json = get(url, output="json", params=params, headers=headers) _, response_json = get(url, output="json", params=params, headers=headers)
assert response_json["queryString"] == {"a": "1", "b": "2", "c": "c", "d": "d"} assert response_json["args"] == {"a": "1", "b": "2", "c": "c", "d": "d"}
with vcr.use_cassette(str(tmpdir.join("get.yaml"))) as cassette: with vcr.use_cassette(str(tmpdir.join("get.yaml"))) as cassette:
_, cassette_response_json = get(url, output="json", params=params, headers=headers) _, cassette_response_json = get(url, output="json", params=params, headers=headers)
@@ -175,8 +175,8 @@ def test_params(tmpdir, mockbin_request_url):
@pytest.mark.online @pytest.mark.online
def test_params_same_url_distinct_params(tmpdir, mockbin_request_url): def test_params_same_url_distinct_params(tmpdir, httpbin):
url = mockbin_request_url url = httpbin.url + "/json"
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
params = {"a": 1, "b": 2, "c": "c"} params = {"a": 1, "b": 2, "c": "c"}
@@ -195,8 +195,8 @@ def test_params_same_url_distinct_params(tmpdir, mockbin_request_url):
@pytest.mark.online @pytest.mark.online
def test_params_on_url(tmpdir, mockbin_request_url): def test_params_on_url(tmpdir, httpbin):
url = mockbin_request_url + "?a=1&b=foo" url = httpbin.url + "/get?a=1&b=foo"
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
with vcr.use_cassette(str(tmpdir.join("get.yaml"))) as cassette: with vcr.use_cassette(str(tmpdir.join("get.yaml"))) as cassette:
@@ -261,8 +261,8 @@ def test_aiohttp_test_client_json(aiohttp_client, tmpdir):
@pytest.mark.online @pytest.mark.online
def test_redirect(tmpdir, mockbin): def test_redirect(tmpdir, httpbin):
url = mockbin + "/redirect/302/2" url = httpbin.url + "/redirect/2"
with vcr.use_cassette(str(tmpdir.join("redirect.yaml"))): with vcr.use_cassette(str(tmpdir.join("redirect.yaml"))):
response, _ = get(url) response, _ = get(url)
@@ -284,9 +284,9 @@ def test_redirect(tmpdir, mockbin):
@pytest.mark.online @pytest.mark.online
def test_not_modified(tmpdir, mockbin): def test_not_modified(tmpdir, httpbin):
"""It doesn't try to redirect on 304""" """It doesn't try to redirect on 304"""
url = mockbin + "/status/304" url = httpbin.url + "/status/304"
with vcr.use_cassette(str(tmpdir.join("not_modified.yaml"))): with vcr.use_cassette(str(tmpdir.join("not_modified.yaml"))):
response, _ = get(url) response, _ = get(url)
@@ -302,13 +302,13 @@ def test_not_modified(tmpdir, mockbin):
@pytest.mark.online @pytest.mark.online
def test_double_requests(tmpdir, mockbin_request_url): def test_double_requests(tmpdir, httpbin):
"""We should capture, record, and replay all requests and response chains, """We should capture, record, and replay all requests and response chains,
even if there are duplicate ones. even if there are duplicate ones.
We should replay in the order we saw them. We should replay in the order we saw them.
""" """
url = mockbin_request_url url = httpbin.url
with vcr.use_cassette(str(tmpdir.join("text.yaml"))): with vcr.use_cassette(str(tmpdir.join("text.yaml"))):
_, response_text1 = get(url, output="text") _, response_text1 = get(url, output="text")
@@ -426,18 +426,18 @@ def test_cookies_redirect(httpbin_both, httpbin_ssl_context, tmpdir):
@pytest.mark.online @pytest.mark.online
def test_not_allow_redirects(tmpdir, mockbin): def test_not_allow_redirects(tmpdir, httpbin):
url = mockbin + "/redirect/308/5" url = httpbin + "/redirect-to?url=.%2F&status_code=308"
path = str(tmpdir.join("redirects.yaml")) path = str(tmpdir.join("redirects.yaml"))
with vcr.use_cassette(path): with vcr.use_cassette(path):
response, _ = get(url, allow_redirects=False) response, _ = get(url, allow_redirects=False)
assert response.url.path == "/redirect/308/5" assert response.url.path == "/redirect-to"
assert response.status == 308 assert response.status == 308
with vcr.use_cassette(path) as cassette: with vcr.use_cassette(path) as cassette:
response, _ = get(url, allow_redirects=False) response, _ = get(url, allow_redirects=False)
assert response.url.path == "/redirect/308/5" assert response.url.path == "/redirect-to"
assert response.status == 308 assert response.status == 308
assert cassette.play_count == 1 assert cassette.play_count == 1

View File

@@ -39,7 +39,7 @@ def test_basic_json_use(tmpdir, httpbin):
test_fixture = str(tmpdir.join("synopsis.json")) test_fixture = str(tmpdir.join("synopsis.json"))
with vcr.use_cassette(test_fixture, serializer="json"): with vcr.use_cassette(test_fixture, serializer="json"):
response = urlopen(httpbin.url).read() response = urlopen(httpbin.url).read()
assert b"difficult sometimes" in response assert b"A simple HTTP Request & Response Service." in response
def test_patched_content(tmpdir, httpbin): def test_patched_content(tmpdir, httpbin):

View File

@@ -8,12 +8,12 @@ import vcr
@pytest.mark.online @pytest.mark.online
def test_set_serializer_default_config(tmpdir, mockbin_request_url): def test_set_serializer_default_config(tmpdir, httpbin):
my_vcr = vcr.VCR(serializer="json") my_vcr = vcr.VCR(serializer="json")
with my_vcr.use_cassette(str(tmpdir.join("test.json"))): with my_vcr.use_cassette(str(tmpdir.join("test.json"))):
assert my_vcr.serializer == "json" assert my_vcr.serializer == "json"
urlopen(mockbin_request_url) urlopen(httpbin.url)
with open(str(tmpdir.join("test.json"))) as f: with open(str(tmpdir.join("test.json"))) as f:
file_content = f.read() file_content = f.read()
@@ -22,37 +22,37 @@ def test_set_serializer_default_config(tmpdir, mockbin_request_url):
@pytest.mark.online @pytest.mark.online
def test_default_set_cassette_library_dir(tmpdir, mockbin_request_url): def test_default_set_cassette_library_dir(tmpdir, httpbin):
my_vcr = vcr.VCR(cassette_library_dir=str(tmpdir.join("subdir"))) my_vcr = vcr.VCR(cassette_library_dir=str(tmpdir.join("subdir")))
with my_vcr.use_cassette("test.json"): with my_vcr.use_cassette("test.json"):
urlopen(mockbin_request_url) urlopen(httpbin.url)
assert os.path.exists(str(tmpdir.join("subdir").join("test.json"))) assert os.path.exists(str(tmpdir.join("subdir").join("test.json")))
@pytest.mark.online @pytest.mark.online
def test_override_set_cassette_library_dir(tmpdir, mockbin_request_url): def test_override_set_cassette_library_dir(tmpdir, httpbin):
my_vcr = vcr.VCR(cassette_library_dir=str(tmpdir.join("subdir"))) my_vcr = vcr.VCR(cassette_library_dir=str(tmpdir.join("subdir")))
cld = str(tmpdir.join("subdir2")) cld = str(tmpdir.join("subdir2"))
with my_vcr.use_cassette("test.json", cassette_library_dir=cld): with my_vcr.use_cassette("test.json", cassette_library_dir=cld):
urlopen(mockbin_request_url) urlopen(httpbin.url)
assert os.path.exists(str(tmpdir.join("subdir2").join("test.json"))) assert os.path.exists(str(tmpdir.join("subdir2").join("test.json")))
assert not os.path.exists(str(tmpdir.join("subdir").join("test.json"))) assert not os.path.exists(str(tmpdir.join("subdir").join("test.json")))
@pytest.mark.online @pytest.mark.online
def test_override_match_on(tmpdir, mockbin_request_url): def test_override_match_on(tmpdir, httpbin):
my_vcr = vcr.VCR(match_on=["method"]) my_vcr = vcr.VCR(match_on=["method"])
with my_vcr.use_cassette(str(tmpdir.join("test.json"))): with my_vcr.use_cassette(str(tmpdir.join("test.json"))):
urlopen(mockbin_request_url) urlopen(httpbin.url)
with my_vcr.use_cassette(str(tmpdir.join("test.json"))) as cass: with my_vcr.use_cassette(str(tmpdir.join("test.json"))) as cass:
urlopen(mockbin_request_url) urlopen(httpbin.url)
assert len(cass) == 1 assert len(cass) == 1
assert cass.play_count == 1 assert cass.play_count == 1
@@ -67,12 +67,12 @@ def test_missing_matcher():
@pytest.mark.online @pytest.mark.online
def test_dont_record_on_exception(tmpdir, mockbin_request_url): def test_dont_record_on_exception(tmpdir, httpbin):
my_vcr = vcr.VCR(record_on_exception=False) my_vcr = vcr.VCR(record_on_exception=False)
@my_vcr.use_cassette(str(tmpdir.join("dontsave.yml"))) @my_vcr.use_cassette(str(tmpdir.join("dontsave.yml")))
def some_test(): def some_test():
assert b"Not in content" in urlopen(mockbin_request_url) assert b"Not in content" in urlopen(httpbin.url)
with pytest.raises(AssertionError): with pytest.raises(AssertionError):
some_test() some_test()
@@ -82,6 +82,6 @@ def test_dont_record_on_exception(tmpdir, mockbin_request_url):
# Make sure context decorator has the same behavior # Make sure context decorator has the same behavior
with pytest.raises(AssertionError): with pytest.raises(AssertionError):
with my_vcr.use_cassette(str(tmpdir.join("dontsave2.yml"))): with my_vcr.use_cassette(str(tmpdir.join("dontsave2.yml"))):
assert b"Not in content" in urlopen(mockbin_request_url).read() assert b"Not in content" in urlopen(httpbin.url).read()
assert not os.path.exists(str(tmpdir.join("dontsave2.yml"))) assert not os.path.exists(str(tmpdir.join("dontsave2.yml")))

View File

@@ -12,19 +12,19 @@ import vcr
@pytest.mark.online @pytest.mark.online
def test_disk_saver_nowrite(tmpdir, mockbin_request_url): def test_disk_saver_nowrite(tmpdir, httpbin):
""" """
Ensure that when you close a cassette without changing it it doesn't Ensure that when you close a cassette without changing it it doesn't
rewrite the file rewrite the file
""" """
fname = str(tmpdir.join("synopsis.yaml")) fname = str(tmpdir.join("synopsis.yaml"))
with vcr.use_cassette(fname) as cass: with vcr.use_cassette(fname) as cass:
urlopen(mockbin_request_url).read() urlopen(httpbin.url).read()
assert cass.play_count == 0 assert cass.play_count == 0
last_mod = os.path.getmtime(fname) last_mod = os.path.getmtime(fname)
with vcr.use_cassette(fname) as cass: with vcr.use_cassette(fname) as cass:
urlopen(mockbin_request_url).read() urlopen(httpbin.url).read()
assert cass.play_count == 1 assert cass.play_count == 1
assert cass.dirty is False assert cass.dirty is False
last_mod2 = os.path.getmtime(fname) last_mod2 = os.path.getmtime(fname)
@@ -33,14 +33,14 @@ def test_disk_saver_nowrite(tmpdir, mockbin_request_url):
@pytest.mark.online @pytest.mark.online
def test_disk_saver_write(tmpdir, mockbin_request_url): def test_disk_saver_write(tmpdir, httpbin):
""" """
Ensure that when you close a cassette after changing it it does Ensure that when you close a cassette after changing it it does
rewrite the file rewrite the file
""" """
fname = str(tmpdir.join("synopsis.yaml")) fname = str(tmpdir.join("synopsis.yaml"))
with vcr.use_cassette(fname) as cass: with vcr.use_cassette(fname) as cass:
urlopen(mockbin_request_url).read() urlopen(httpbin.url).read()
assert cass.play_count == 0 assert cass.play_count == 0
last_mod = os.path.getmtime(fname) last_mod = os.path.getmtime(fname)
@@ -49,8 +49,8 @@ def test_disk_saver_write(tmpdir, mockbin_request_url):
time.sleep(1) time.sleep(1)
with vcr.use_cassette(fname, record_mode=vcr.mode.ANY) as cass: with vcr.use_cassette(fname, record_mode=vcr.mode.ANY) as cass:
urlopen(mockbin_request_url).read() urlopen(httpbin.url).read()
urlopen(mockbin_request_url + "/get").read() urlopen(httpbin.url + "/get").read()
assert cass.play_count == 1 assert cass.play_count == 1
assert cass.dirty assert cass.dirty
last_mod2 = os.path.getmtime(fname) last_mod2 = os.path.getmtime(fname)

View File

@@ -56,14 +56,14 @@ def test_response_headers(tmpdir, httpbin_both):
@pytest.mark.online @pytest.mark.online
def test_effective_url(tmpdir): def test_effective_url(tmpdir, httpbin):
"""Ensure that the effective_url is captured""" """Ensure that the effective_url is captured"""
url = "http://mockbin.org/redirect/301" url = httpbin.url + "/redirect-to?url=.%2F&status_code=301"
with vcr.use_cassette(str(tmpdir.join("headers.yaml"))): with vcr.use_cassette(str(tmpdir.join("headers.yaml"))):
resp, _ = http().request(url) resp, _ = http().request(url)
effective_url = resp["content-location"] effective_url = resp["content-location"]
assert effective_url == "http://mockbin.org/redirect/301/0" assert effective_url == httpbin.url + "/"
with vcr.use_cassette(str(tmpdir.join("headers.yaml"))): with vcr.use_cassette(str(tmpdir.join("headers.yaml"))):
resp, _ = http().request(url) resp, _ = http().request(url)

View File

@@ -9,14 +9,12 @@ httpx = pytest.importorskip("httpx")
from vcr.stubs.httpx_stubs import HTTPX_REDIRECT_PARAM # noqa: E402 from vcr.stubs.httpx_stubs import HTTPX_REDIRECT_PARAM # noqa: E402
@pytest.fixture(params=["https", "http"]) @pytest.fixture(params=["https", "http"])
def scheme(request): def scheme(request):
"""Fixture that returns both http and https.""" """Fixture that returns both http and https."""
return request.param return request.param
@pytest.fixture
def httpbin(scheme):
return scheme + "://httpbin.org"
class BaseDoRequest: class BaseDoRequest:
_client_class = None _client_class = None
@@ -24,7 +22,7 @@ class BaseDoRequest:
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self._client_args = args self._client_args = args
self._client_kwargs = kwargs self._client_kwargs = kwargs
self._client_kwargs['follow_redirects'] = self._client_kwargs.get('follow_redirects', True) self._client_kwargs["follow_redirects"] = self._client_kwargs.get("follow_redirects", True)
def _make_client(self): def _make_client(self):
return self._client_class(*self._client_args, **self._client_kwargs) return self._client_class(*self._client_args, **self._client_kwargs)
@@ -54,6 +52,7 @@ class DoSyncRequest(BaseDoRequest):
with self.client.stream(*args, **kwargs) as response: with self.client.stream(*args, **kwargs) as response:
return b"".join(response.iter_bytes()) return b"".join(response.iter_bytes())
class DoAsyncRequest(BaseDoRequest): class DoAsyncRequest(BaseDoRequest):
_client_class = httpx.AsyncClient _client_class = httpx.AsyncClient
@@ -104,6 +103,7 @@ class DoAsyncRequest(BaseDoRequest):
with self: with self:
return self._loop.run_until_complete(self._get_stream(*args, **kwargs)) return self._loop.run_until_complete(self._get_stream(*args, **kwargs))
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
if "do_request" in metafunc.fixturenames: if "do_request" in metafunc.fixturenames:
metafunc.parametrize("do_request", [DoAsyncRequest, DoSyncRequest]) metafunc.parametrize("do_request", [DoAsyncRequest, DoSyncRequest])
@@ -116,7 +116,7 @@ def yml(tmpdir, request):
@pytest.mark.online @pytest.mark.online
def test_status(tmpdir, httpbin, do_request): def test_status(tmpdir, httpbin, do_request):
url = httpbin url = httpbin.url
with vcr.use_cassette(str(tmpdir.join("status.yaml"))): with vcr.use_cassette(str(tmpdir.join("status.yaml"))):
response = do_request()("GET", url) response = do_request()("GET", url)
@@ -129,7 +129,7 @@ def test_status(tmpdir, httpbin, do_request):
@pytest.mark.online @pytest.mark.online
def test_case_insensitive_headers(tmpdir, httpbin, do_request): def test_case_insensitive_headers(tmpdir, httpbin, do_request):
url = httpbin url = httpbin.url
with vcr.use_cassette(str(tmpdir.join("whatever.yaml"))): with vcr.use_cassette(str(tmpdir.join("whatever.yaml"))):
do_request()("GET", url) do_request()("GET", url)
@@ -143,7 +143,7 @@ def test_case_insensitive_headers(tmpdir, httpbin, do_request):
@pytest.mark.online @pytest.mark.online
def test_content(tmpdir, httpbin, do_request): def test_content(tmpdir, httpbin, do_request):
url = httpbin url = httpbin.url
with vcr.use_cassette(str(tmpdir.join("cointent.yaml"))): with vcr.use_cassette(str(tmpdir.join("cointent.yaml"))):
response = do_request()("GET", url) response = do_request()("GET", url)
@@ -156,7 +156,7 @@ def test_content(tmpdir, httpbin, do_request):
@pytest.mark.online @pytest.mark.online
def test_json(tmpdir, httpbin, do_request): def test_json(tmpdir, httpbin, do_request):
url = httpbin + "/json" url = httpbin.url + "/json"
with vcr.use_cassette(str(tmpdir.join("json.yaml"))): with vcr.use_cassette(str(tmpdir.join("json.yaml"))):
response = do_request()("GET", url) response = do_request()("GET", url)
@@ -169,7 +169,7 @@ def test_json(tmpdir, httpbin, do_request):
@pytest.mark.online @pytest.mark.online
def test_params_same_url_distinct_params(tmpdir, httpbin, do_request): def test_params_same_url_distinct_params(tmpdir, httpbin, do_request):
url = httpbin + "/get" url = httpbin.url + "/get"
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
params = {"a": 1, "b": False, "c": "c"} params = {"a": 1, "b": False, "c": "c"}
@@ -190,7 +190,7 @@ def test_params_same_url_distinct_params(tmpdir, httpbin, do_request):
@pytest.mark.online @pytest.mark.online
def test_redirect(httpbin, yml, do_request): def test_redirect(httpbin, yml, do_request):
url = httpbin + "/redirect-to" url = httpbin.url + "/redirect-to"
response = do_request()("GET", url) response = do_request()("GET", url)
with vcr.use_cassette(yml): with vcr.use_cassette(yml):
@@ -213,7 +213,7 @@ def test_redirect(httpbin, yml, do_request):
@pytest.mark.online @pytest.mark.online
def test_work_with_gzipped_data(httpbin, do_request, yml): def test_work_with_gzipped_data(httpbin, do_request, yml):
url = httpbin + "/gzip?foo=bar" url = httpbin.url + "/gzip?foo=bar"
headers = {"accept-encoding": "deflate, gzip"} headers = {"accept-encoding": "deflate, gzip"}
with vcr.use_cassette(yml): with vcr.use_cassette(yml):
@@ -238,6 +238,7 @@ def test_simple_fetching(do_request, yml, url):
assert str(cassette_response.request.url) == url assert str(cassette_response.request.url) == url
assert cassette.play_count == 1 assert cassette.play_count == 1
@pytest.mark.online @pytest.mark.online
def test_cookies(tmpdir, httpbin, do_request): def test_cookies(tmpdir, httpbin, do_request):
def client_cookies(client): def client_cookies(client):
@@ -246,7 +247,7 @@ def test_cookies(tmpdir, httpbin, do_request):
def response_cookies(response): def response_cookies(response):
return list(response.cookies) return list(response.cookies)
url = httpbin + "/cookies/set" url = httpbin.url + "/cookies/set"
params = {"k1": "v1", "k2": "v2"} params = {"k1": "v1", "k2": "v2"}
with do_request(params=params, follow_redirects=False) as client: with do_request(params=params, follow_redirects=False) as client:
@@ -273,19 +274,18 @@ def test_cookies(tmpdir, httpbin, do_request):
assert response_cookies(cassette_response) == ["k1", "k2"] assert response_cookies(cassette_response) == ["k1", "k2"]
assert client_cookies(new_client) == ["k1", "k2"] assert client_cookies(new_client) == ["k1", "k2"]
@pytest.mark.online @pytest.mark.online
def test_stream(tmpdir, httpbin, do_request): def test_stream(tmpdir, httpbin, do_request):
url = httpbin + "/stream-bytes/512" url = httpbin.url + "/stream-bytes/512"
testfile = str(tmpdir.join("stream.yml")) testfile = str(tmpdir.join("stream.yml"))
with vcr.use_cassette(testfile): with vcr.use_cassette(testfile):
response_content = do_request().stream("GET", url) response_content = do_request().stream("GET", url)
assert len(response_content) == 512 assert len(response_content) == 512
with vcr.use_cassette(testfile) as cassette: with vcr.use_cassette(testfile) as cassette:
cassette_content = do_request().stream("GET", url) cassette_content = do_request().stream("GET", url)
assert cassette_content == response_content assert cassette_content == response_content
assert len(cassette_content) == 512 assert len(cassette_content) == 512
assert cassette.play_count == 1 assert cassette.play_count == 1

View File

@@ -2,6 +2,7 @@
import http.server import http.server
import multiprocessing import multiprocessing
import threading
import socketserver import socketserver
from urllib.request import urlopen from urllib.request import urlopen
@@ -29,7 +30,8 @@ class Proxy(http.server.SimpleHTTPRequestHandler):
# In Python 2 the response is an addinfourl instance. # In Python 2 the response is an addinfourl instance.
status = upstream_response.code status = upstream_response.code
headers = upstream_response.info().items() headers = upstream_response.info().items()
self.send_response(status, upstream_response.msg) self.log_request(status)
self.send_response_only(status, upstream_response.msg)
for header in headers: for header in headers:
self.send_header(*header) self.send_header(*header)
self.end_headers() self.end_headers()
@@ -39,10 +41,11 @@ class Proxy(http.server.SimpleHTTPRequestHandler):
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def proxy_server(): def proxy_server():
httpd = socketserver.ThreadingTCPServer(("", 0), Proxy) httpd = socketserver.ThreadingTCPServer(("", 0), Proxy)
proxy_process = multiprocessing.Process(target=httpd.serve_forever) proxy_process = threading.Thread(target=httpd.serve_forever)
proxy_process.start() proxy_process.start()
yield "http://{}:{}".format(*httpd.server_address) yield "http://{}:{}".format(*httpd.server_address)
proxy_process.terminate() httpd.shutdown()
proxy_process.join()
def test_use_proxy(tmpdir, httpbin, proxy_server): def test_use_proxy(tmpdir, httpbin, proxy_server):
@@ -50,8 +53,10 @@ def test_use_proxy(tmpdir, httpbin, proxy_server):
with vcr.use_cassette(str(tmpdir.join("proxy.yaml"))): with vcr.use_cassette(str(tmpdir.join("proxy.yaml"))):
response = requests.get(httpbin.url, proxies={"http": proxy_server}) response = requests.get(httpbin.url, proxies={"http": proxy_server})
with vcr.use_cassette(str(tmpdir.join("proxy.yaml"))) as cassette: with vcr.use_cassette(str(tmpdir.join("proxy.yaml")), mode="once") as cassette:
cassette_response = requests.get(httpbin.url, proxies={"http": proxy_server}) cassette_response = requests.get(httpbin.url, proxies={"http": proxy_server})
for key in set(cassette_response.headers.keys()) & set(response.headers.keys()):
assert cassette_response.headers[key] == response.headers[key]
assert cassette_response.headers == response.headers assert cassette_response.headers == response.headers
assert cassette.play_count == 1 assert cassette.play_count == 1

View File

@@ -14,28 +14,28 @@ def false_matcher(r1, r2):
@pytest.mark.online @pytest.mark.online
def test_registered_true_matcher(tmpdir, mockbin_request_url): def test_registered_true_matcher(tmpdir, httpbin):
my_vcr = vcr.VCR() my_vcr = vcr.VCR()
my_vcr.register_matcher("true", true_matcher) my_vcr.register_matcher("true", true_matcher)
testfile = str(tmpdir.join("test.yml")) testfile = str(tmpdir.join("test.yml"))
with my_vcr.use_cassette(testfile, match_on=["true"]): with my_vcr.use_cassette(testfile, match_on=["true"]):
# These 2 different urls are stored as the same request # These 2 different urls are stored as the same request
urlopen(mockbin_request_url) urlopen(httpbin.url)
urlopen(mockbin_request_url + "/get") urlopen(httpbin.url + "/get")
with my_vcr.use_cassette(testfile, match_on=["true"]): with my_vcr.use_cassette(testfile, match_on=["true"]):
# I can get the response twice even though I only asked for it once # I can get the response twice even though I only asked for it once
urlopen(mockbin_request_url) urlopen(httpbin.url)
urlopen(mockbin_request_url) urlopen(httpbin.url)
@pytest.mark.online @pytest.mark.online
def test_registered_false_matcher(tmpdir, mockbin_request_url): def test_registered_false_matcher(tmpdir, httpbin):
my_vcr = vcr.VCR() my_vcr = vcr.VCR()
my_vcr.register_matcher("false", false_matcher) my_vcr.register_matcher("false", false_matcher)
testfile = str(tmpdir.join("test.yml")) testfile = str(tmpdir.join("test.yml"))
with my_vcr.use_cassette(testfile, match_on=["false"]) as cass: with my_vcr.use_cassette(testfile, match_on=["false"]) as cass:
# These 2 different urls are stored as different requests # These 2 different urls are stored as different requests
urlopen(mockbin_request_url) urlopen(httpbin.url)
urlopen(mockbin_request_url + "/get") urlopen(httpbin.url + "/get")
assert len(cass) == 2 assert len(cass) == 2

View File

@@ -66,7 +66,7 @@ def test_load_cassette_with_custom_persister(tmpdir, httpbin):
with my_vcr.use_cassette(test_fixture, serializer="json"): with my_vcr.use_cassette(test_fixture, serializer="json"):
response = urlopen(httpbin.url).read() response = urlopen(httpbin.url).read()
assert b"difficult sometimes" in response assert b"A simple HTTP Request & Response Service." in response
def test_load_cassette_persister_exception_handling(tmpdir, httpbin): def test_load_cassette_persister_exception_handling(tmpdir, httpbin):

View File

@@ -81,12 +81,12 @@ def test_body(get_client, tmpdir, scheme):
@pytest.mark.gen_test @pytest.mark.gen_test
def test_effective_url(get_client, scheme, tmpdir): def test_effective_url(get_client, tmpdir, httpbin):
"""Ensure that the effective_url is captured""" """Ensure that the effective_url is captured"""
url = scheme + "://mockbin.org/redirect/301?url=/html" url = httpbin.url + "/redirect/1"
with vcr.use_cassette(str(tmpdir.join("url.yaml"))): with vcr.use_cassette(str(tmpdir.join("url.yaml"))):
effective_url = (yield get(get_client(), url)).effective_url effective_url = (yield get(get_client(), url)).effective_url
assert effective_url == scheme + "://mockbin.org/redirect/301/0" assert effective_url == httpbin.url + "/get"
with vcr.use_cassette(str(tmpdir.join("url.yaml"))) as cass: with vcr.use_cassette(str(tmpdir.join("url.yaml"))) as cass:
assert effective_url == (yield get(get_client(), url)).effective_url assert effective_url == (yield get(get_client(), url)).effective_url

View File

@@ -57,13 +57,13 @@ def test_response_headers(httpbin_both, tmpdir):
@mark.online @mark.online
def test_effective_url(tmpdir): def test_effective_url(tmpdir, httpbin):
"""Ensure that the effective_url is captured""" """Ensure that the effective_url is captured"""
url = "http://mockbin.org/redirect/301" url = httpbin.url + "/redirect-to?url=.%2F&status_code=301"
with vcr.use_cassette(str(tmpdir.join("headers.yaml"))): with vcr.use_cassette(str(tmpdir.join("headers.yaml"))):
effective_url = urlopen_with_cafile(url).geturl() effective_url = urlopen_with_cafile(url).geturl()
assert effective_url == "http://mockbin.org/redirect/301/0" assert effective_url == httpbin.url + "/"
with vcr.use_cassette(str(tmpdir.join("headers.yaml"))): with vcr.use_cassette(str(tmpdir.join("headers.yaml"))):
assert effective_url == urlopen_with_cafile(url).geturl() assert effective_url == urlopen_with_cafile(url).geturl()

View File

@@ -99,9 +99,9 @@ def test_post(tmpdir, httpbin_both, verify_pool_mgr):
@pytest.mark.online @pytest.mark.online
def test_redirects(tmpdir, verify_pool_mgr): def test_redirects(tmpdir, verify_pool_mgr, httpbin):
"""Ensure that we can handle redirects""" """Ensure that we can handle redirects"""
url = "http://mockbin.org/redirect/301" url = httpbin.url + "/redirect/1"
with vcr.use_cassette(str(tmpdir.join("verify_pool_mgr.yaml"))): with vcr.use_cassette(str(tmpdir.join("verify_pool_mgr.yaml"))):
content = verify_pool_mgr.request("GET", url).data content = verify_pool_mgr.request("GET", url).data

View File

@@ -6,7 +6,7 @@ envlist =
{py38,py39,py310,py311,py312}-{requests-urllib3-1,httplib2,urllib3-1,tornado4,boto3,aiohttp,httpx}, {py38,py39,py310,py311,py312}-{requests-urllib3-1,httplib2,urllib3-1,tornado4,boto3,aiohttp,httpx},
{py310,py311,py312}-{requests-urllib3-2,urllib3-2}, {py310,py311,py312}-{requests-urllib3-2,urllib3-2},
{pypy3}-{requests-urllib3-1,httplib2,urllib3-1,tornado4,boto3}, {pypy3}-{requests-urllib3-1,httplib2,urllib3-1,tornado4,boto3},
{py310}-httpx019, #{py310}-httpx019,
cov-report cov-report