tests: fix dep on httpbin

This commit is contained in:
lilydjwg 2024-05-11 13:19:14 +08:00
parent 2a63ddef7a
commit 9f6706e0e7
2 changed files with 125 additions and 110 deletions

View file

@ -1,24 +1,31 @@
# MIT licensed # MIT licensed
# Copyright (c) 2021 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2021,2024 lilydjwg <lilydjwg@gmail.com>, et al.
import pytest import pytest
import pytest_httpbin
assert pytest_httpbin # for pyflakes
pytestmark = [pytest.mark.asyncio(scope="session"), pytest.mark.needs_net] httpbin_available = True
try:
import pytest_httpbin
assert pytest_httpbin # for pyflakes
except ImportError:
httpbin_available = False
pytestmark = pytest.mark.asyncio(scope="session")
@pytest.mark.needs_net
async def test_redirection(get_version): async def test_redirection(get_version):
assert await get_version("unifiedremote", { assert await get_version("unifiedremote", {
"source": "httpheader", "source": "httpheader",
"url": "https://www.unifiedremote.com/download/linux-x64-deb", "url": "https://www.unifiedremote.com/download/linux-x64-deb",
"regex": r'urserver-([\d.]+).deb', "regex": r'urserver-([\d.]+).deb',
}) is not None }) is not None
@pytest.mark.skipif(not httpbin_available, reason="needs pytest_httpbin")
async def test_get_version_withtoken(get_version, httpbin): async def test_get_version_withtoken(get_version, httpbin):
assert await get_version("unifiedremote", { assert await get_version("unifiedremote", {
"source": "httpheader", "source": "httpheader",
"url": httpbin.url + "/basic-auth/username/superpassword", "url": httpbin.url + "/basic-auth/username/superpassword",
"httptoken": "Basic dXNlcm5hbWU6c3VwZXJwYXNzd29yZA==", "httptoken": "Basic dXNlcm5hbWU6c3VwZXJwYXNzd29yZA==",
"header": "server", "header": "server",
"regex": r'([0-9.]+)*', "regex": r'([0-9.]+)*',
}) is not None }) is not None

View file

@ -1,130 +1,138 @@
# MIT licensed # MIT licensed
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2020,2024 lilydjwg <lilydjwg@gmail.com>, et al.
import base64 import base64
import pytest import pytest
import pytest_httpbin
assert pytest_httpbin # for pyflakes
pytestmark = pytest.mark.asyncio(scope="session") httpbin_available = True
try:
import pytest_httpbin
assert pytest_httpbin # for pyflakes
except ImportError:
httpbin_available = False
pytestmark = [
pytest.mark.asyncio(scope="session"),
pytest.mark.skipif(not httpbin_available, reason="needs pytest_httpbin"),
]
def base64_encode(s): def base64_encode(s):
return base64.b64encode(s.encode('utf-8')).decode('ascii') return base64.b64encode(s.encode('utf-8')).decode('ascii')
async def test_regex_httpbin_default_user_agent(get_version, httpbin): async def test_regex_httpbin_default_user_agent(get_version, httpbin):
ua = await get_version("example", { ua = await get_version("example", {
"source": "regex", "source": "regex",
"url": httpbin.url + "/get", "url": httpbin.url + "/get",
"regex": r'"User-Agent":\s*"([^"]+)"', "regex": r'"User-Agent":\s*"([^"]+)"',
}) })
assert ua.startswith("lilydjwg/nvchecker") assert ua.startswith("lilydjwg/nvchecker")
async def test_regex_httpbin_user_agent(get_version, httpbin): async def test_regex_httpbin_user_agent(get_version, httpbin):
assert await get_version("example", { assert await get_version("example", {
"source": "regex", "source": "regex",
"url": httpbin.url + "/get", "url": httpbin.url + "/get",
"regex": r'"User-Agent":\s*"(\w+)"', "regex": r'"User-Agent":\s*"(\w+)"',
"user_agent": "Meow", "user_agent": "Meow",
}) == "Meow" }) == "Meow"
async def test_regex(get_version, httpbin): async def test_regex(get_version, httpbin):
assert await get_version("example", { assert await get_version("example", {
"source": "regex", "source": "regex",
"url": httpbin.url + "/base64/" + base64_encode("version 1.12 released"), "url": httpbin.url + "/base64/" + base64_encode("version 1.12 released"),
"regex": r'version ([0-9.]+)', "regex": r'version ([0-9.]+)',
}) == "1.12" }) == "1.12"
async def test_missing_ok(get_version, httpbin): async def test_missing_ok(get_version, httpbin):
assert await get_version("example", { assert await get_version("example", {
"source": "regex", "source": "regex",
"url": httpbin.url + "/base64/" + base64_encode("something not there"), "url": httpbin.url + "/base64/" + base64_encode("something not there"),
"regex": "foobar", "regex": "foobar",
"missing_ok": True, "missing_ok": True,
}) is None }) is None
async def test_missing(get_version, httpbin): async def test_missing(get_version, httpbin):
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError):
await get_version("example", { await get_version("example", {
"source": "regex", "source": "regex",
"url": httpbin.url + "/base64/" + base64_encode("something not there"), "url": httpbin.url + "/base64/" + base64_encode("something not there"),
"regex": "foobar", "regex": "foobar",
}) })
async def test_multi_group(get_version, httpbin): async def test_multi_group(get_version, httpbin):
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError):
await get_version("example", { await get_version("example", {
"source": "regex", "source": "regex",
"url": httpbin.url + "/base64/" + base64_encode("1.2"), "url": httpbin.url + "/base64/" + base64_encode("1.2"),
"regex": r"(\d+)\.(\d+)", "regex": r"(\d+)\.(\d+)",
}) })
async def test_regex_with_tokenBasic(get_version, httpbin): async def test_regex_with_tokenBasic(get_version, httpbin):
assert await get_version("example", { assert await get_version("example", {
"source": "regex", "source": "regex",
"url": httpbin.url + "/basic-auth/username/superpassword", "url": httpbin.url + "/basic-auth/username/superpassword",
"httptoken": "Basic dXNlcm5hbWU6c3VwZXJwYXNzd29yZA==", "httptoken": "Basic dXNlcm5hbWU6c3VwZXJwYXNzd29yZA==",
"regex": r'"user":\s*"([a-w]+)"', "regex": r'"user":\s*"([a-w]+)"',
}) == "username" }) == "username"
async def test_regex_with_tokenBearer(get_version, httpbin): async def test_regex_with_tokenBearer(get_version, httpbin):
assert await get_version("example", { assert await get_version("example", {
"source": "regex", "source": "regex",
"url": httpbin.url + "/bearer", "url": httpbin.url + "/bearer",
"httptoken": "Bearer username:password", "httptoken": "Bearer username:password",
"regex": r'"token":\s*"([a-w]+):.*"', "regex": r'"token":\s*"([a-w]+):.*"',
}) == "username" }) == "username"
async def test_regex_no_verify_ssl(get_version, httpbin_secure): async def test_regex_no_verify_ssl(get_version, httpbin_secure):
assert await get_version("example", { assert await get_version("example", {
"source": "regex", "source": "regex",
"url": httpbin_secure.url + "/base64/" + base64_encode("version 1.12 released"), "url": httpbin_secure.url + "/base64/" + base64_encode("version 1.12 released"),
"regex": r'version ([0-9.]+)', "regex": r'version ([0-9.]+)',
"verify_cert": False, "verify_cert": False,
}) == "1.12" }) == "1.12"
async def test_regex_bad_ssl(get_version, httpbin_secure): async def test_regex_bad_ssl(get_version, httpbin_secure):
try: try:
await get_version("example", { await get_version("example", {
"source": "regex", "source": "regex",
"url": httpbin_secure.url + "/base64/" + base64_encode("version 1.12 released"), "url": httpbin_secure.url + "/base64/" + base64_encode("version 1.12 released"),
"regex": r'version ([0-9.]+)', "regex": r'version ([0-9.]+)',
}) })
except Exception: except Exception:
pass pass
else: else:
assert False, 'certificate should not be trusted' assert False, 'certificate should not be trusted'
async def test_regex_post(get_version, httpbin): async def test_regex_post(get_version, httpbin):
assert await get_version("example", { assert await get_version("example", {
"source": "regex", "source": "regex",
"url": httpbin.url + "/post", "url": httpbin.url + "/post",
"regex": r'"ABCDEF":\s*"(\w+)"', "regex": r'"ABCDEF":\s*"(\w+)"',
"post_data": "ABCDEF=234&CDEFG=xyz" "post_data": "ABCDEF=234&CDEFG=xyz"
}) == "234" }) == "234"
async def test_regex_post2(get_version, httpbin): async def test_regex_post2(get_version, httpbin):
assert await get_version("example", { assert await get_version("example", {
"source": "regex", "source": "regex",
"url": httpbin.url + "/post", "url": httpbin.url + "/post",
"regex": r'"CDEFG":\s*"(\w+)"', "regex": r'"CDEFG":\s*"(\w+)"',
"post_data": "ABCDEF=234&CDEFG=xyz" "post_data": "ABCDEF=234&CDEFG=xyz"
}) == "xyz" }) == "xyz"
async def test_regex_post_json(get_version, httpbin): async def test_regex_post_json(get_version, httpbin):
assert await get_version("example", { assert await get_version("example", {
"source": "regex", "source": "regex",
"url": httpbin.url + "/post", "url": httpbin.url + "/post",
"regex": r'"ABCDEF":\s*(\w+)', "regex": r'"ABCDEF":\s*(\w+)',
"post_data": '{"ABCDEF":234,"CDEFG":"xyz"}', "post_data": '{"ABCDEF":234,"CDEFG":"xyz"}',
"post_data_type": "application/json" "post_data_type": "application/json"
}) == "234" }) == "234"
async def test_regex_post_json2(get_version, httpbin): async def test_regex_post_json2(get_version, httpbin):
assert await get_version("example", { assert await get_version("example", {
"source": "regex", "source": "regex",
"url": httpbin.url + "/post", "url": httpbin.url + "/post",
"regex": r'"CDEFG":\s*"(\w+)"', "regex": r'"CDEFG":\s*"(\w+)"',
"post_data": '{"ABCDEF":234,"CDEFG":"xyz"}', "post_data": '{"ABCDEF":234,"CDEFG":"xyz"}',
"post_data_type": "application/json" "post_data_type": "application/json"
}) == "xyz" }) == "xyz"