2017-02-28 07:24:53 +00:00
|
|
|
# MIT licensed
|
2020-08-17 08:21:02 +00:00
|
|
|
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
|
2017-02-28 07:24:53 +00:00
|
|
|
|
2019-10-15 09:27:34 +00:00
|
|
|
import base64
|
|
|
|
|
2017-07-04 09:04:29 +00:00
|
|
|
import pytest
|
2019-10-15 09:27:34 +00:00
|
|
|
import pytest_httpbin
|
|
|
|
assert pytest_httpbin # for pyflakes
|
|
|
|
|
2023-12-09 11:17:31 +00:00
|
|
|
pytestmark = pytest.mark.asyncio(scope="session")
|
2015-11-04 15:43:35 +00:00
|
|
|
|
2019-10-15 09:27:34 +00:00
|
|
|
def base64_encode(s):
|
|
|
|
return base64.b64encode(s.encode('utf-8')).decode('ascii')
|
|
|
|
|
|
|
|
async def test_regex_httpbin_default_user_agent(get_version, httpbin):
|
2019-07-08 03:40:08 +00:00
|
|
|
ua = await get_version("example", {
|
2020-08-17 08:21:02 +00:00
|
|
|
"source": "regex",
|
2019-10-15 09:27:34 +00:00
|
|
|
"url": httpbin.url + "/get",
|
|
|
|
"regex": r'"User-Agent":\s*"([^"]+)"',
|
2019-07-08 03:40:08 +00:00
|
|
|
})
|
|
|
|
assert ua.startswith("lilydjwg/nvchecker")
|
2019-07-06 06:23:00 +00:00
|
|
|
|
2019-10-15 09:27:34 +00:00
|
|
|
async def test_regex_httpbin_user_agent(get_version, httpbin):
|
2017-07-04 09:04:29 +00:00
|
|
|
assert await get_version("example", {
|
2020-08-17 08:21:02 +00:00
|
|
|
"source": "regex",
|
2019-10-15 09:27:34 +00:00
|
|
|
"url": httpbin.url + "/get",
|
|
|
|
"regex": r'"User-Agent":\s*"(\w+)"',
|
2017-07-04 09:04:29 +00:00
|
|
|
"user_agent": "Meow",
|
|
|
|
}) == "Meow"
|
2018-05-08 11:27:45 +00:00
|
|
|
|
2019-10-15 09:27:34 +00:00
|
|
|
async def test_regex(get_version, httpbin):
|
2018-05-08 11:27:45 +00:00
|
|
|
assert await get_version("example", {
|
2020-08-17 08:21:02 +00:00
|
|
|
"source": "regex",
|
2019-10-15 09:27:34 +00:00
|
|
|
"url": httpbin.url + "/base64/" + base64_encode("version 1.12 released"),
|
|
|
|
"regex": r'version ([0-9.]+)',
|
|
|
|
}) == "1.12"
|
2018-09-21 04:17:21 +00:00
|
|
|
|
2019-10-15 09:27:34 +00:00
|
|
|
async def test_missing_ok(get_version, httpbin):
|
2018-09-21 04:17:21 +00:00
|
|
|
assert await get_version("example", {
|
2020-08-17 08:21:02 +00:00
|
|
|
"source": "regex",
|
2019-10-15 09:27:34 +00:00
|
|
|
"url": httpbin.url + "/base64/" + base64_encode("something not there"),
|
2018-09-21 04:17:21 +00:00
|
|
|
"regex": "foobar",
|
|
|
|
"missing_ok": True,
|
|
|
|
}) is None
|
2021-06-11 08:18:39 +00:00
|
|
|
|
2021-08-31 01:48:07 +00:00
|
|
|
async def test_missing(get_version, httpbin):
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
await get_version("example", {
|
|
|
|
"source": "regex",
|
|
|
|
"url": httpbin.url + "/base64/" + base64_encode("something not there"),
|
|
|
|
"regex": "foobar",
|
|
|
|
})
|
|
|
|
|
2021-11-06 02:44:10 +00:00
|
|
|
async def test_multi_group(get_version, httpbin):
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
await get_version("example", {
|
|
|
|
"source": "regex",
|
|
|
|
"url": httpbin.url + "/base64/" + base64_encode("1.2"),
|
|
|
|
"regex": r"(\d+)\.(\d+)",
|
|
|
|
})
|
|
|
|
|
2021-06-11 08:18:39 +00:00
|
|
|
async def test_regex_with_tokenBasic(get_version, httpbin):
|
|
|
|
assert await get_version("example", {
|
|
|
|
"source": "regex",
|
|
|
|
"url": httpbin.url + "/basic-auth/username/superpassword",
|
|
|
|
"httptoken": "Basic dXNlcm5hbWU6c3VwZXJwYXNzd29yZA==",
|
2023-08-22 10:41:12 +00:00
|
|
|
"regex": r'"user":\s*"([a-w]+)"',
|
2021-06-11 08:18:39 +00:00
|
|
|
}) == "username"
|
|
|
|
|
|
|
|
async def test_regex_with_tokenBearer(get_version, httpbin):
|
|
|
|
assert await get_version("example", {
|
|
|
|
"source": "regex",
|
|
|
|
"url": httpbin.url + "/bearer",
|
|
|
|
"httptoken": "Bearer username:password",
|
2023-08-22 10:41:12 +00:00
|
|
|
"regex": r'"token":\s*"([a-w]+):.*"',
|
2021-06-11 08:18:39 +00:00
|
|
|
}) == "username"
|
2021-06-23 08:00:17 +00:00
|
|
|
|
|
|
|
async def test_regex_no_verify_ssl(get_version, httpbin_secure):
|
|
|
|
assert await get_version("example", {
|
|
|
|
"source": "regex",
|
|
|
|
"url": httpbin_secure.url + "/base64/" + base64_encode("version 1.12 released"),
|
|
|
|
"regex": r'version ([0-9.]+)',
|
|
|
|
"verify_cert": False,
|
|
|
|
}) == "1.12"
|
|
|
|
|
|
|
|
async def test_regex_bad_ssl(get_version, httpbin_secure):
|
|
|
|
try:
|
|
|
|
await get_version("example", {
|
|
|
|
"source": "regex",
|
|
|
|
"url": httpbin_secure.url + "/base64/" + base64_encode("version 1.12 released"),
|
|
|
|
"regex": r'version ([0-9.]+)',
|
|
|
|
})
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
assert False, 'certificate should not be trusted'
|
|
|
|
|
2021-09-05 20:26:30 +00:00
|
|
|
async def test_regex_post(get_version, httpbin):
|
|
|
|
assert await get_version("example", {
|
|
|
|
"source": "regex",
|
|
|
|
"url": httpbin.url + "/post",
|
|
|
|
"regex": r'"ABCDEF":\s*"(\w+)"',
|
|
|
|
"post_data": "ABCDEF=234&CDEFG=xyz"
|
|
|
|
}) == "234"
|
|
|
|
|
|
|
|
async def test_regex_post2(get_version, httpbin):
|
|
|
|
assert await get_version("example", {
|
|
|
|
"source": "regex",
|
|
|
|
"url": httpbin.url + "/post",
|
|
|
|
"regex": r'"CDEFG":\s*"(\w+)"',
|
|
|
|
"post_data": "ABCDEF=234&CDEFG=xyz"
|
|
|
|
}) == "xyz"
|
|
|
|
|
|
|
|
async def test_regex_post_json(get_version, httpbin):
|
|
|
|
assert await get_version("example", {
|
|
|
|
"source": "regex",
|
|
|
|
"url": httpbin.url + "/post",
|
|
|
|
"regex": r'"ABCDEF":\s*(\w+)',
|
|
|
|
"post_data": '{"ABCDEF":234,"CDEFG":"xyz"}',
|
|
|
|
"post_data_type": "application/json"
|
|
|
|
}) == "234"
|
|
|
|
|
|
|
|
async def test_regex_post_json2(get_version, httpbin):
|
|
|
|
assert await get_version("example", {
|
|
|
|
"source": "regex",
|
|
|
|
"url": httpbin.url + "/post",
|
|
|
|
"regex": r'"CDEFG":\s*"(\w+)"',
|
|
|
|
"post_data": '{"ABCDEF":234,"CDEFG":"xyz"}',
|
|
|
|
"post_data_type": "application/json"
|
|
|
|
}) == "xyz"
|