Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HTTPX support #784

Merged
merged 5 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@
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
def httpbin_ssl_context():
ssl_ca_location = os.environ["REQUESTS_CA_BUNDLE"]
Expand Down
66 changes: 33 additions & 33 deletions tests/integration/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def post(url, output="text", **kwargs):


@pytest.mark.online
def test_status(tmpdir, mockbin_request_url):
url = mockbin_request_url
def test_status(tmpdir, httpbin):
url = httpbin.url

with vcr.use_cassette(str(tmpdir.join("status.yaml"))):
response, _ = get(url)
Expand All @@ -50,8 +50,8 @@ def test_status(tmpdir, mockbin_request_url):

@pytest.mark.online
@pytest.mark.parametrize("auth", [None, aiohttp.BasicAuth("vcrpy", "test")])
def test_headers(tmpdir, auth, mockbin_request_url):
url = mockbin_request_url
def test_headers(tmpdir, auth, httpbin):
url = httpbin.url
with vcr.use_cassette(str(tmpdir.join("headers.yaml"))):
response, _ = get(url, auth=auth)

Expand All @@ -67,8 +67,8 @@ def test_headers(tmpdir, auth, mockbin_request_url):


@pytest.mark.online
def test_case_insensitive_headers(tmpdir, mockbin_request_url):
url = mockbin_request_url
def test_case_insensitive_headers(tmpdir, httpbin):
url = httpbin.url

with vcr.use_cassette(str(tmpdir.join("whatever.yaml"))):
_, _ = get(url)
Expand All @@ -81,8 +81,8 @@ def test_case_insensitive_headers(tmpdir, mockbin_request_url):


@pytest.mark.online
def test_text(tmpdir, mockbin_request_url):
url = mockbin_request_url
def test_text(tmpdir, httpbin):
url = httpbin.url

with vcr.use_cassette(str(tmpdir.join("text.yaml"))):
_, response_text = get(url)
Expand All @@ -94,8 +94,8 @@ def test_text(tmpdir, mockbin_request_url):


@pytest.mark.online
def test_json(tmpdir, mockbin_request_url):
url = mockbin_request_url
def test_json(tmpdir, httpbin):
url = httpbin.url + "/json"
headers = {"Content-Type": "application/json"}

with vcr.use_cassette(str(tmpdir.join("json.yaml"))):
Expand All @@ -108,8 +108,8 @@ def test_json(tmpdir, mockbin_request_url):


@pytest.mark.online
def test_binary(tmpdir, mockbin_request_url):
url = mockbin_request_url + "/image/png"
def test_binary(tmpdir, httpbin):
url = httpbin.url + "/image/png"
with vcr.use_cassette(str(tmpdir.join("binary.yaml"))):
_, response_binary = get(url, output="raw")

Expand All @@ -120,8 +120,8 @@ def test_binary(tmpdir, mockbin_request_url):


@pytest.mark.online
def test_stream(tmpdir, mockbin_request_url):
url = mockbin_request_url
def test_stream(tmpdir, httpbin):
url = httpbin.url

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
Expand All @@ -134,10 +134,10 @@ def test_stream(tmpdir, mockbin_request_url):

@pytest.mark.online
@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)
data = {"key1": "value1", "key2": "value2"}
url = mockbin_request_url
url = httpbin.url
with vcr.use_cassette(str(tmpdir.join("post.yaml"))):
_, response_json = post(url, **{body: data})

Expand All @@ -159,14 +159,14 @@ def test_post(tmpdir, body, caplog, mockbin_request_url):


@pytest.mark.online
def test_params(tmpdir, mockbin_request_url):
url = mockbin_request_url + "?d=d"
def test_params(tmpdir, httpbin):
url = httpbin.url + "/get?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["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:
_, cassette_response_json = get(url, output="json", params=params, headers=headers)
Expand All @@ -175,8 +175,8 @@ def test_params(tmpdir, mockbin_request_url):


@pytest.mark.online
def test_params_same_url_distinct_params(tmpdir, mockbin_request_url):
url = mockbin_request_url
def test_params_same_url_distinct_params(tmpdir, httpbin):
url = httpbin.url + "/json"
headers = {"Content-Type": "application/json"}
params = {"a": 1, "b": 2, "c": "c"}

Expand All @@ -195,8 +195,8 @@ def test_params_same_url_distinct_params(tmpdir, mockbin_request_url):


@pytest.mark.online
def test_params_on_url(tmpdir, mockbin_request_url):
url = mockbin_request_url + "?a=1&b=foo"
def test_params_on_url(tmpdir, httpbin):
url = httpbin.url + "/get?a=1&b=foo"
headers = {"Content-Type": "application/json"}

with vcr.use_cassette(str(tmpdir.join("get.yaml"))) as cassette:
Expand Down Expand Up @@ -261,8 +261,8 @@ def test_aiohttp_test_client_json(aiohttp_client, tmpdir):


@pytest.mark.online
def test_redirect(tmpdir, mockbin):
url = mockbin + "/redirect/302/2"
def test_redirect(tmpdir, httpbin):
url = httpbin.url + "/redirect/2"

with vcr.use_cassette(str(tmpdir.join("redirect.yaml"))):
response, _ = get(url)
Expand All @@ -284,9 +284,9 @@ def test_redirect(tmpdir, mockbin):


@pytest.mark.online
def test_not_modified(tmpdir, mockbin):
def test_not_modified(tmpdir, httpbin):
"""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"))):
response, _ = get(url)
Expand All @@ -302,13 +302,13 @@ def test_not_modified(tmpdir, mockbin):


@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,
even if there are duplicate ones.

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"))):
_, response_text1 = get(url, output="text")
Expand Down Expand Up @@ -426,18 +426,18 @@ async def run(loop):


@pytest.mark.online
def test_not_allow_redirects(tmpdir, mockbin):
url = mockbin + "/redirect/308/5"
def test_not_allow_redirects(tmpdir, httpbin):
url = httpbin + "/redirect-to?url=.%2F&status_code=308"
path = str(tmpdir.join("redirects.yaml"))

with vcr.use_cassette(path):
response, _ = get(url, allow_redirects=False)
assert response.url.path == "/redirect/308/5"
assert response.url.path == "/redirect-to"
assert response.status == 308

with vcr.use_cassette(path) as cassette:
response, _ = get(url, allow_redirects=False)
assert response.url.path == "/redirect/308/5"
assert response.url.path == "/redirect-to"
assert response.status == 308
assert cassette.play_count == 1

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_basic_json_use(tmpdir, httpbin):
test_fixture = str(tmpdir.join("synopsis.json"))
with vcr.use_cassette(test_fixture, serializer="json"):
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):
Expand Down
24 changes: 12 additions & 12 deletions tests/integration/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@


@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")

with my_vcr.use_cassette(str(tmpdir.join("test.json"))):
assert my_vcr.serializer == "json"
urlopen(mockbin_request_url)
urlopen(httpbin.url)

with open(str(tmpdir.join("test.json"))) as f:
file_content = f.read()
Expand All @@ -22,37 +22,37 @@ def test_set_serializer_default_config(tmpdir, mockbin_request_url):


@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")))

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")))


@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")))

cld = str(tmpdir.join("subdir2"))

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 not os.path.exists(str(tmpdir.join("subdir").join("test.json")))


@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"])

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:
urlopen(mockbin_request_url)
urlopen(httpbin.url)

assert len(cass) == 1
assert cass.play_count == 1
Expand All @@ -67,12 +67,12 @@ def test_missing_matcher():


@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.use_cassette(str(tmpdir.join("dontsave.yml")))
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):
some_test()
Expand All @@ -82,6 +82,6 @@ def some_test():
# 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(mockbin_request_url).read()
assert b"Not in content" in urlopen(httpbin.url).read()

assert not os.path.exists(str(tmpdir.join("dontsave2.yml")))
14 changes: 7 additions & 7 deletions tests/integration/test_disksaver.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@


@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
rewrite the file
"""
fname = str(tmpdir.join("synopsis.yaml"))
with vcr.use_cassette(fname) as cass:
urlopen(mockbin_request_url).read()
urlopen(httpbin.url).read()
assert cass.play_count == 0
last_mod = os.path.getmtime(fname)

with vcr.use_cassette(fname) as cass:
urlopen(mockbin_request_url).read()
urlopen(httpbin.url).read()
assert cass.play_count == 1
assert cass.dirty is False
last_mod2 = os.path.getmtime(fname)
Expand All @@ -33,14 +33,14 @@ def test_disk_saver_nowrite(tmpdir, mockbin_request_url):


@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
rewrite the file
"""
fname = str(tmpdir.join("synopsis.yaml"))
with vcr.use_cassette(fname) as cass:
urlopen(mockbin_request_url).read()
urlopen(httpbin.url).read()
assert cass.play_count == 0
last_mod = os.path.getmtime(fname)

Expand All @@ -49,8 +49,8 @@ def test_disk_saver_write(tmpdir, mockbin_request_url):
time.sleep(1)

with vcr.use_cassette(fname, record_mode=vcr.mode.ANY) as cass:
urlopen(mockbin_request_url).read()
urlopen(mockbin_request_url + "/get").read()
urlopen(httpbin.url).read()
urlopen(httpbin.url + "/get").read()
assert cass.play_count == 1
assert cass.dirty
last_mod2 = os.path.getmtime(fname)
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_httplib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ def test_response_headers(tmpdir, httpbin_both):


@pytest.mark.online
def test_effective_url(tmpdir):
def test_effective_url(tmpdir, httpbin):
"""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"))):
resp, _ = http().request(url)
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"))):
resp, _ = http().request(url)
Expand Down
Loading
Loading