1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-08 16:53:23 +00:00

Move some tests to use mockbin instead httpbin

This commit is contained in:
Jair Henrique
2023-05-11 13:21:25 -03:00
parent 77da67ef0a
commit 14cef83c15
7 changed files with 118 additions and 99 deletions

View File

@@ -13,7 +13,7 @@ interactions:
user-agent:
- python-httpx/0.12.1
method: GET
uri: https://httpbin.org/headers
uri: https://mockbin.org/headers
response:
content: "{\n \"headers\": {\n \"Accept\": \"*/*\", \n \"Accept-Encoding\"\
: \"gzip, deflate, br\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\"\

View File

@@ -5,3 +5,13 @@ import pytest
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"

View File

@@ -34,8 +34,9 @@ def post(url, output="text", **kwargs):
return request("POST", url, output="text", **kwargs)
def test_status(tmpdir, scheme):
url = scheme + "://httpbin.org"
def test_status(tmpdir, mockbin_request_url):
url = mockbin_request_url
with vcr.use_cassette(str(tmpdir.join("status.yaml"))):
response, _ = get(url)
@@ -46,8 +47,8 @@ def test_status(tmpdir, scheme):
@pytest.mark.parametrize("auth", [None, aiohttp.BasicAuth("vcrpy", "test")])
def test_headers(tmpdir, scheme, auth):
url = scheme + "://httpbin.org"
def test_headers(tmpdir, auth, mockbin_request_url):
url = mockbin_request_url
with vcr.use_cassette(str(tmpdir.join("headers.yaml"))):
response, _ = get(url, auth=auth)
@@ -62,8 +63,9 @@ def test_headers(tmpdir, scheme, auth):
assert "yarl.URL" not in cassette.data[0]
def test_case_insensitive_headers(tmpdir, scheme):
url = scheme + "://httpbin.org"
def test_case_insensitive_headers(tmpdir, mockbin_request_url):
url = mockbin_request_url
with vcr.use_cassette(str(tmpdir.join("whatever.yaml"))):
_, _ = get(url)
@@ -74,8 +76,9 @@ def test_case_insensitive_headers(tmpdir, scheme):
assert cassette.play_count == 1
def test_text(tmpdir, scheme):
url = scheme + "://httpbin.org"
def test_text(tmpdir, mockbin_request_url):
url = mockbin_request_url
with vcr.use_cassette(str(tmpdir.join("text.yaml"))):
_, response_text = get(url)
@@ -85,8 +88,8 @@ def test_text(tmpdir, scheme):
assert cassette.play_count == 1
def test_json(tmpdir, scheme):
url = scheme + "://httpbin.org/get"
def test_json(tmpdir, mockbin_request_url):
url = mockbin_request_url
headers = {"Content-Type": "application/json"}
with vcr.use_cassette(str(tmpdir.join("json.yaml"))):
@@ -98,8 +101,8 @@ def test_json(tmpdir, scheme):
assert cassette.play_count == 1
def test_binary(tmpdir, scheme):
url = scheme + "://httpbin.org/image/png"
def test_binary(tmpdir, mockbin_request_url):
url = mockbin_request_url + "/image/png"
with vcr.use_cassette(str(tmpdir.join("binary.yaml"))):
_, response_binary = get(url, output="raw")
@@ -109,23 +112,23 @@ def test_binary(tmpdir, scheme):
assert cassette.play_count == 1
def test_stream(tmpdir, scheme):
url = scheme + "://httpbin.org/get"
def test_stream(tmpdir, mockbin_request_url):
url = mockbin_request_url
with vcr.use_cassette(str(tmpdir.join("stream.yaml"))):
resp, 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
with vcr.use_cassette(str(tmpdir.join("stream.yaml"))) as cassette:
cassette_resp, cassette_body = get(url, output="stream")
_, cassette_body = get(url, output="stream")
assert cassette_body == body
assert cassette.play_count == 1
@pytest.mark.parametrize("body", ["data", "json"])
def test_post(tmpdir, scheme, body, caplog):
def test_post(tmpdir, body, caplog, mockbin_request_url):
caplog.set_level(logging.INFO)
data = {"key1": "value1", "key2": "value2"}
url = scheme + "://httpbin.org/post"
url = mockbin_request_url
with vcr.use_cassette(str(tmpdir.join("post.yaml"))):
_, response_json = post(url, **{body: data})
@@ -146,14 +149,14 @@ def test_post(tmpdir, scheme, body, caplog):
), "Log message not found."
def test_params(tmpdir, scheme):
url = scheme + "://httpbin.org/get?d=d"
def test_params(tmpdir, mockbin_request_url):
url = mockbin_request_url + "?d=d"
headers = {"Content-Type": "application/json"}
params = {"a": 1, "b": 2, "c": "c"}
with vcr.use_cassette(str(tmpdir.join("get.yaml"))) as cassette:
_, response_json = get(url, output="json", params=params, headers=headers)
assert response_json["args"] == {"a": "1", "b": "2", "c": "c", "d": "d"}
assert response_json["queryString"] == {"a": "1", "b": "2", "c": "c", "d": "d"}
with vcr.use_cassette(str(tmpdir.join("get.yaml"))) as cassette:
_, cassette_response_json = get(url, output="json", params=params, headers=headers)
@@ -161,8 +164,8 @@ def test_params(tmpdir, scheme):
assert cassette.play_count == 1
def test_params_same_url_distinct_params(tmpdir, scheme):
url = scheme + "://httpbin.org/get"
def test_params_same_url_distinct_params(tmpdir, mockbin_request_url):
url = mockbin_request_url
headers = {"Content-Type": "application/json"}
params = {"a": 1, "b": 2, "c": "c"}
@@ -180,8 +183,8 @@ def test_params_same_url_distinct_params(tmpdir, scheme):
get(url, output="text", params=other_params)
def test_params_on_url(tmpdir, scheme):
url = scheme + "://httpbin.org/get?a=1&b=foo"
def test_params_on_url(tmpdir, mockbin_request_url):
url = mockbin_request_url + "?a=1&b=foo"
headers = {"Content-Type": "application/json"}
with vcr.use_cassette(str(tmpdir.join("get.yaml"))) as cassette:
@@ -245,8 +248,8 @@ def test_aiohttp_test_client_json(aiohttp_client, tmpdir):
assert cassette.play_count == 1
def test_redirect(aiohttp_client, tmpdir):
url = "https://mockbin.org/redirect/302/2"
def test_redirect(tmpdir, mockbin):
url = mockbin + "/redirect/302/2"
with vcr.use_cassette(str(tmpdir.join("redirect.yaml"))):
response, _ = get(url)
@@ -269,9 +272,9 @@ def test_redirect(aiohttp_client, tmpdir):
assert cassette_response.request_info.real_url == response.request_info.real_url
def test_not_modified(aiohttp_client, tmpdir):
def test_not_modified(tmpdir, mockbin):
"""It doesn't try to redirect on 304"""
url = "https://httpbin.org/status/304"
url = mockbin + "/status/304"
with vcr.use_cassette(str(tmpdir.join("not_modified.yaml"))):
response, _ = get(url)
@@ -286,13 +289,13 @@ def test_not_modified(aiohttp_client, tmpdir):
assert cassette.play_count == 1
def test_double_requests(tmpdir):
def test_double_requests(tmpdir, mockbin_request_url):
"""We should capture, record, and replay all requests and response chains,
even if there are duplicate ones.
We should replay in the order we saw them.
"""
url = "https://httpbin.org/get"
url = mockbin_request_url
with vcr.use_cassette(str(tmpdir.join("text.yaml"))):
_, response_text1 = get(url, output="text")
@@ -397,8 +400,8 @@ def test_cookies_redirect(scheme, tmpdir):
run_in_loop(run)
def test_not_allow_redirects(tmpdir):
url = "https://mockbin.org/redirect/308/5"
def test_not_allow_redirects(tmpdir, mockbin):
url = mockbin + "/redirect/308/5"
path = str(tmpdir.join("redirects.yaml"))
with vcr.use_cassette(path):

View File

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

View File

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

View File

@@ -80,8 +80,6 @@ class DoAsyncRequest(BaseDoRequest):
def pytest_generate_tests(metafunc):
if "do_request" in metafunc.fixturenames:
metafunc.parametrize("do_request", [DoAsyncRequest, DoSyncRequest])
if "scheme" in metafunc.fixturenames:
metafunc.parametrize("scheme", ["http", "https"])
@pytest.fixture
@@ -89,8 +87,9 @@ def yml(tmpdir, request):
return str(tmpdir.join(request.function.__name__ + ".yaml"))
def test_status(tmpdir, scheme, do_request):
url = scheme + "://mockbin.org/request"
def test_status(tmpdir, mockbin, do_request):
url = mockbin
with vcr.use_cassette(str(tmpdir.join("status.yaml"))):
response = do_request()("GET", url)
@@ -100,8 +99,9 @@ def test_status(tmpdir, scheme, do_request):
assert cassette.play_count == 1
def test_case_insensitive_headers(tmpdir, scheme, do_request):
url = scheme + "://mockbin.org/request"
def test_case_insensitive_headers(tmpdir, mockbin, do_request):
url = mockbin
with vcr.use_cassette(str(tmpdir.join("whatever.yaml"))):
do_request()("GET", url)
@@ -112,8 +112,9 @@ def test_case_insensitive_headers(tmpdir, scheme, do_request):
assert cassette.play_count == 1
def test_content(tmpdir, scheme, do_request):
url = scheme + "://httpbin.org"
def test_content(tmpdir, mockbin, do_request):
url = mockbin
with vcr.use_cassette(str(tmpdir.join("cointent.yaml"))):
response = do_request()("GET", url)
@@ -123,9 +124,10 @@ def test_content(tmpdir, scheme, do_request):
assert cassette.play_count == 1
def test_json(tmpdir, scheme, do_request):
url = scheme + "://httpbin.org/get"
headers = {"Content-Type": "application/json"}
def test_json(tmpdir, mockbin, do_request):
url = mockbin + "/request"
headers = {"content-type": "application/json"}
with vcr.use_cassette(str(tmpdir.join("json.yaml"))):
response = do_request(headers=headers)("GET", url)
@@ -136,8 +138,8 @@ def test_json(tmpdir, scheme, do_request):
assert cassette.play_count == 1
def test_params_same_url_distinct_params(tmpdir, scheme, do_request):
url = scheme + "://httpbin.org/get"
def test_params_same_url_distinct_params(tmpdir, mockbin, do_request):
url = mockbin + "/request"
headers = {"Content-Type": "application/json"}
params = {"a": 1, "b": False, "c": "c"}
@@ -156,8 +158,8 @@ def test_params_same_url_distinct_params(tmpdir, scheme, do_request):
do_request()("GET", url, params=params, headers=headers)
def test_redirect(tmpdir, do_request, yml):
url = "https://mockbin.org/redirect/303/2"
def test_redirect(mockbin, yml, do_request):
url = mockbin + "/redirect/303/2"
redirect_kwargs = {HTTPX_REDIRECT_PARAM.name: True}
@@ -182,20 +184,23 @@ def test_redirect(tmpdir, do_request, yml):
}
def test_work_with_gzipped_data(tmpdir, do_request, yml):
def test_work_with_gzipped_data(mockbin, do_request, yml):
url = mockbin + "/gzip?foo=bar"
headers = {"accept-encoding": "deflate, gzip"}
with vcr.use_cassette(yml):
do_request()("GET", "https://httpbin.org/gzip")
do_request(headers=headers)("GET", url)
with vcr.use_cassette(yml) as cassette:
cassette_response = do_request()("GET", "https://httpbin.org/gzip")
cassette_response = do_request(headers=headers)("GET", url)
assert "gzip" in cassette_response.json()["headers"]["Accept-Encoding"]
assert cassette_response.headers["content-encoding"] == "gzip"
assert cassette_response.read()
assert cassette.play_count == 1
@pytest.mark.parametrize("url", ["https://github.com/kevin1024/vcrpy/issues/" + str(i) for i in range(3, 6)])
def test_simple_fetching(tmpdir, do_request, yml, url):
def test_simple_fetching(do_request, yml, url):
with vcr.use_cassette(yml):
do_request()("GET", url)
@@ -210,7 +215,7 @@ def test_behind_proxy(do_request):
yml = (
os.path.dirname(os.path.realpath(__file__)) + "/cassettes/" + "test_httpx_test_test_behind_proxy.yml"
)
url = "https://httpbin.org/headers"
url = "https://mockbin.org/headers"
proxy = "http://localhost:8080"
proxies = {"http://": proxy, "https://": proxy}
@@ -226,46 +231,47 @@ def test_behind_proxy(do_request):
assert cassette_response.request.url == response.request.url
def test_cookies(tmpdir, scheme, do_request):
def test_cookies(tmpdir, mockbin, do_request):
def client_cookies(client):
return [c for c in client.client.cookies]
def response_cookies(response):
return [c for c in response.cookies]
with do_request() as client:
url = mockbin + "/bin/26148652-fe25-4f21-aaf5-689b5b4bf65f"
headers = {"cookie": "k1=v1;k2=v2"}
with do_request(headers=headers) as client:
assert client_cookies(client) == []
redirect_kwargs = {HTTPX_REDIRECT_PARAM.name: True}
url = scheme + "://httpbin.org"
testfile = str(tmpdir.join("cookies.yml"))
with vcr.use_cassette(testfile):
r1 = client("GET", url + "/cookies/set?k1=v1&k2=v2", **redirect_kwargs)
assert response_cookies(r1.history[0]) == ["k1", "k2"]
assert response_cookies(r1) == []
r1 = client("GET", url, **redirect_kwargs)
r2 = client("GET", url + "/cookies", **redirect_kwargs)
assert len(r2.json()["cookies"]) == 2
assert response_cookies(r1) == ["k1", "k2"]
r2 = client("GET", url, **redirect_kwargs)
assert response_cookies(r2) == ["k1", "k2"]
assert client_cookies(client) == ["k1", "k2"]
with do_request() as new_client:
with do_request(headers=headers) as new_client:
assert client_cookies(new_client) == []
with vcr.use_cassette(testfile) as cassette:
cassette_response = new_client("GET", url + "/cookies/set?k1=v1&k2=v2")
assert response_cookies(cassette_response.history[0]) == ["k1", "k2"]
assert response_cookies(cassette_response) == []
cassette_response = new_client("GET", url)
assert cassette.play_count == 2
assert cassette.play_count == 1
assert response_cookies(cassette_response) == ["k1", "k2"]
assert client_cookies(new_client) == ["k1", "k2"]
def test_relative_redirects(tmpdir, scheme, do_request):
def test_relative_redirects(tmpdir, scheme, do_request, mockbin):
redirect_kwargs = {HTTPX_REDIRECT_PARAM.name: True}
url = scheme + "://mockbin.com/redirect/301?to=/redirect/301?to=/request"
url = mockbin + "/redirect/301?to=/redirect/301?to=/request"
testfile = str(tmpdir.join("relative_redirects.yml"))
with vcr.use_cassette(testfile):
response = do_request()("GET", url, **redirect_kwargs)
@@ -280,8 +286,8 @@ def test_relative_redirects(tmpdir, scheme, do_request):
assert cassette.play_count == 3
def test_redirect_wo_allow_redirects(do_request, yml):
url = "https://mockbin.org/redirect/308/5"
def test_redirect_wo_allow_redirects(do_request, mockbin, yml):
url = mockbin + "/redirect/308/5"
redirect_kwargs = {HTTPX_REDIRECT_PARAM.name: False}

View File

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