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

pytest.mark.online tests that need internet

This commit is contained in:
Jochen Sprickerhof
2022-12-16 18:55:56 +01:00
committed by Jochen Sprickerhof
parent e3aae34ef7
commit 7007e944ae
12 changed files with 57 additions and 0 deletions

View File

@@ -13,3 +13,8 @@ skip = '.git,*.pdf,*.svg,.tox'
ignore-regex = "\\\\[fnrstv]"
#
# ignore-words-list = ''
[tool.pytest.ini_options]
markers = [
"online",
]

View File

@@ -34,6 +34,7 @@ def post(url, output="text", **kwargs):
return request("POST", url, output="text", **kwargs)
@pytest.mark.online
def test_status(tmpdir, mockbin_request_url):
url = mockbin_request_url
@@ -46,6 +47,7 @@ def test_status(tmpdir, mockbin_request_url):
assert cassette.play_count == 1
@pytest.mark.online
@pytest.mark.parametrize("auth", [None, aiohttp.BasicAuth("vcrpy", "test")])
def test_headers(tmpdir, auth, mockbin_request_url):
url = mockbin_request_url
@@ -63,6 +65,7 @@ def test_headers(tmpdir, auth, mockbin_request_url):
assert "yarl.URL" not in cassette.data[0]
@pytest.mark.online
def test_case_insensitive_headers(tmpdir, mockbin_request_url):
url = mockbin_request_url
@@ -76,6 +79,7 @@ def test_case_insensitive_headers(tmpdir, mockbin_request_url):
assert cassette.play_count == 1
@pytest.mark.online
def test_text(tmpdir, mockbin_request_url):
url = mockbin_request_url
@@ -88,6 +92,7 @@ def test_text(tmpdir, mockbin_request_url):
assert cassette.play_count == 1
@pytest.mark.online
def test_json(tmpdir, mockbin_request_url):
url = mockbin_request_url
headers = {"Content-Type": "application/json"}
@@ -101,6 +106,7 @@ def test_json(tmpdir, mockbin_request_url):
assert cassette.play_count == 1
@pytest.mark.online
def test_binary(tmpdir, mockbin_request_url):
url = mockbin_request_url + "/image/png"
with vcr.use_cassette(str(tmpdir.join("binary.yaml"))):
@@ -112,6 +118,7 @@ def test_binary(tmpdir, mockbin_request_url):
assert cassette.play_count == 1
@pytest.mark.online
def test_stream(tmpdir, mockbin_request_url):
url = mockbin_request_url
@@ -124,6 +131,7 @@ def test_stream(tmpdir, mockbin_request_url):
assert cassette.play_count == 1
@pytest.mark.online
@pytest.mark.parametrize("body", ["data", "json"])
def test_post(tmpdir, body, caplog, mockbin_request_url):
caplog.set_level(logging.INFO)
@@ -149,6 +157,7 @@ def test_post(tmpdir, body, caplog, mockbin_request_url):
), "Log message not found."
@pytest.mark.online
def test_params(tmpdir, mockbin_request_url):
url = mockbin_request_url + "?d=d"
headers = {"Content-Type": "application/json"}
@@ -164,6 +173,7 @@ def test_params(tmpdir, mockbin_request_url):
assert cassette.play_count == 1
@pytest.mark.online
def test_params_same_url_distinct_params(tmpdir, mockbin_request_url):
url = mockbin_request_url
headers = {"Content-Type": "application/json"}
@@ -183,6 +193,7 @@ def test_params_same_url_distinct_params(tmpdir, mockbin_request_url):
get(url, output="text", params=other_params)
@pytest.mark.online
def test_params_on_url(tmpdir, mockbin_request_url):
url = mockbin_request_url + "?a=1&b=foo"
headers = {"Content-Type": "application/json"}
@@ -248,6 +259,7 @@ def test_aiohttp_test_client_json(aiohttp_client, tmpdir):
assert cassette.play_count == 1
@pytest.mark.online
def test_redirect(tmpdir, mockbin):
url = mockbin + "/redirect/302/2"
@@ -272,6 +284,7 @@ def test_redirect(tmpdir, mockbin):
assert cassette_response.request_info.real_url == response.request_info.real_url
@pytest.mark.online
def test_not_modified(tmpdir, mockbin):
"""It doesn't try to redirect on 304"""
url = mockbin + "/status/304"
@@ -289,6 +302,7 @@ def test_not_modified(tmpdir, mockbin):
assert cassette.play_count == 1
@pytest.mark.online
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.
@@ -404,6 +418,7 @@ def test_cookies_redirect(httpbin_both, httpbin_ssl_context, tmpdir):
run_in_loop(run)
@pytest.mark.online
def test_not_allow_redirects(tmpdir, mockbin):
url = mockbin + "/redirect/308/5"
path = str(tmpdir.join("redirects.yaml"))

View File

@@ -7,6 +7,7 @@ import pytest
import vcr
@pytest.mark.online
def test_set_serializer_default_config(tmpdir, mockbin_request_url):
my_vcr = vcr.VCR(serializer="json")
@@ -20,6 +21,7 @@ def test_set_serializer_default_config(tmpdir, mockbin_request_url):
assert json.loads(file_content)
@pytest.mark.online
def test_default_set_cassette_library_dir(tmpdir, mockbin_request_url):
my_vcr = vcr.VCR(cassette_library_dir=str(tmpdir.join("subdir")))
@@ -29,6 +31,7 @@ def test_default_set_cassette_library_dir(tmpdir, mockbin_request_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):
my_vcr = vcr.VCR(cassette_library_dir=str(tmpdir.join("subdir")))
@@ -41,6 +44,7 @@ def test_override_set_cassette_library_dir(tmpdir, mockbin_request_url):
assert not os.path.exists(str(tmpdir.join("subdir").join("test.json")))
@pytest.mark.online
def test_override_match_on(tmpdir, mockbin_request_url):
my_vcr = vcr.VCR(match_on=["method"])
@@ -62,6 +66,7 @@ def test_missing_matcher():
pass
@pytest.mark.online
def test_dont_record_on_exception(tmpdir, mockbin_request_url):
my_vcr = vcr.VCR(record_on_exception=False)

View File

@@ -6,10 +6,13 @@ import os
import time
from urllib.request import urlopen
import pytest
# Internal imports
import vcr
@pytest.mark.online
def test_disk_saver_nowrite(tmpdir, mockbin_request_url):
"""
Ensure that when you close a cassette without changing it it doesn't
@@ -30,6 +33,7 @@ def test_disk_saver_nowrite(tmpdir, mockbin_request_url):
assert last_mod == last_mod2
@pytest.mark.online
def test_disk_saver_write(tmpdir, mockbin_request_url):
"""
Ensure that when you close a cassette after changing it it does

View File

@@ -56,6 +56,7 @@ def test_response_headers(tmpdir, httpbin_both):
assert set(headers) == set(resp.items())
@pytest.mark.online
def test_effective_url(tmpdir):
"""Ensure that the effective_url is captured"""
url = "http://mockbin.org/redirect/301"

View File

@@ -87,6 +87,7 @@ def yml(tmpdir, request):
return str(tmpdir.join(request.function.__name__ + ".yaml"))
@pytest.mark.online
def test_status(tmpdir, mockbin, do_request):
url = mockbin
@@ -99,6 +100,7 @@ def test_status(tmpdir, mockbin, do_request):
assert cassette.play_count == 1
@pytest.mark.online
def test_case_insensitive_headers(tmpdir, mockbin, do_request):
url = mockbin
@@ -112,6 +114,7 @@ def test_case_insensitive_headers(tmpdir, mockbin, do_request):
assert cassette.play_count == 1
@pytest.mark.online
def test_content(tmpdir, mockbin, do_request):
url = mockbin
@@ -124,6 +127,7 @@ def test_content(tmpdir, mockbin, do_request):
assert cassette.play_count == 1
@pytest.mark.online
def test_json(tmpdir, mockbin, do_request):
url = mockbin + "/request"
@@ -138,6 +142,7 @@ def test_json(tmpdir, mockbin, do_request):
assert cassette.play_count == 1
@pytest.mark.online
def test_params_same_url_distinct_params(tmpdir, mockbin, do_request):
url = mockbin + "/request"
headers = {"Content-Type": "application/json"}
@@ -158,6 +163,7 @@ def test_params_same_url_distinct_params(tmpdir, mockbin, do_request):
do_request()("GET", url, params=params, headers=headers)
@pytest.mark.online
def test_redirect(mockbin, yml, do_request):
url = mockbin + "/redirect/303/2"
@@ -184,6 +190,7 @@ def test_redirect(mockbin, yml, do_request):
}
@pytest.mark.online
def test_work_with_gzipped_data(mockbin, do_request, yml):
url = mockbin + "/gzip?foo=bar"
headers = {"accept-encoding": "deflate, gzip"}
@@ -199,6 +206,7 @@ def test_work_with_gzipped_data(mockbin, do_request, yml):
assert cassette.play_count == 1
@pytest.mark.online
@pytest.mark.parametrize("url", ["https://github.com/kevin1024/vcrpy/issues/" + str(i) for i in range(3, 6)])
def test_simple_fetching(do_request, yml, url):
with vcr.use_cassette(yml):
@@ -231,6 +239,7 @@ def test_behind_proxy(do_request):
assert cassette_response.request.url == response.request.url
@pytest.mark.online
def test_cookies(tmpdir, mockbin, do_request):
def client_cookies(client):
return [c for c in client.client.cookies]
@@ -268,6 +277,7 @@ def test_cookies(tmpdir, mockbin, do_request):
assert client_cookies(new_client) == ["k1", "k2"]
@pytest.mark.online
def test_relative_redirects(tmpdir, scheme, do_request, mockbin):
redirect_kwargs = {HTTPX_REDIRECT_PARAM.name: True}
@@ -286,6 +296,7 @@ def test_relative_redirects(tmpdir, scheme, do_request, mockbin):
assert cassette.play_count == 3
@pytest.mark.online
def test_redirect_wo_allow_redirects(do_request, mockbin, yml):
url = mockbin + "/redirect/308/5"

View File

@@ -1,5 +1,7 @@
from urllib.request import urlopen
import pytest
import vcr
@@ -11,6 +13,7 @@ def false_matcher(r1, r2):
return False
@pytest.mark.online
def test_registered_true_matcher(tmpdir, mockbin_request_url):
my_vcr = vcr.VCR()
my_vcr.register_matcher("true", true_matcher)
@@ -26,6 +29,7 @@ def test_registered_true_matcher(tmpdir, mockbin_request_url):
urlopen(mockbin_request_url)
@pytest.mark.online
def test_registered_false_matcher(tmpdir, mockbin_request_url):
my_vcr = vcr.VCR()
my_vcr.register_matcher("false", false_matcher)

View File

@@ -7,6 +7,7 @@ from urllib.request import urlopen
import pytest_httpbin.certs
from assertions import assert_cassette_has_one_response
from pytest import mark
# Internal imports
import vcr
@@ -56,6 +57,7 @@ def test_response_headers(httpbin_both, tmpdir):
assert sorted(open1) == sorted(open2)
@mark.online
def test_effective_url(tmpdir):
"""Ensure that the effective_url is captured"""
url = "http://mockbin.org/redirect/301"

View File

@@ -97,6 +97,7 @@ def test_post(tmpdir, httpbin_both, verify_pool_mgr):
assert req1 == req2
@pytest.mark.online
def test_redirects(tmpdir, verify_pool_mgr):
"""Ensure that we can handle redirects"""
url = "http://mockbin.org/redirect/301"

View File

@@ -52,6 +52,7 @@ def test_flickr_multipart_upload(httpbin, tmpdir):
assert cass.play_count == 1
@pytest.mark.online
def test_flickr_should_respond_with_200(tmpdir):
testfile = str(tmpdir.join("flickr.yml"))
with vcr.use_cassette(testfile):
@@ -70,6 +71,7 @@ def test_cookies(tmpdir, httpbin):
assert sorted(r2.json()["cookies"].keys()) == ["k1", "k2"]
@pytest.mark.online
def test_amazon_doctype(tmpdir):
# amazon gzips its homepage. For some reason, in requests 2.7, it's not
# getting gunzipped.

View File

@@ -1,5 +1,7 @@
from unittest import mock
from pytest import mark
from vcr import mode
from vcr.cassette import Cassette
from vcr.stubs import VCRHTTPSConnection
@@ -11,6 +13,7 @@ class TestVCRConnection:
vcr_connection.ssl_version = "example_ssl_version"
assert vcr_connection.real_connection.ssl_version == "example_ssl_version"
@mark.online
@mock.patch("vcr.cassette.Cassette.can_play_response_for", return_value=False)
def testing_connect(*args):
vcr_connection = VCRHTTPSConnection("www.google.com")

View File

@@ -3,6 +3,8 @@ from unittest import TextTestRunner, defaultTestLoader
from unittest.mock import MagicMock
from urllib.request import urlopen
import pytest
from vcr.unittest import VCRTestCase
@@ -126,6 +128,7 @@ def test_vcr_kwargs_cassette_dir():
assert test._get_cassette_library_dir.call_count == 0
@pytest.mark.online
def test_get_vcr_with_matcher(tmpdir):
cassette_dir = tmpdir.mkdir("cassettes")
assert len(cassette_dir.listdir()) == 0
@@ -160,6 +163,7 @@ def test_get_vcr_with_matcher(tmpdir):
)
@pytest.mark.online
def test_testcase_playback(tmpdir):
cassette_dir = tmpdir.mkdir("cassettes")
assert len(cassette_dir.listdir()) == 0