File: test_regex.py

package info (click to toggle)
python-nvchecker 2.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 736 kB
  • sloc: python: 4,801; makefile: 25
file content (138 lines) | stat: -rw-r--r-- 4,213 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# MIT licensed
# Copyright (c) 2013-2020,2024 lilydjwg <lilydjwg@gmail.com>, et al.

import base64

import pytest

httpbin_available = True
try:
  import pytest_httpbin
  assert pytest_httpbin # for pyflakes
except ImportError:
  httpbin_available = False

pytestmark = [
  pytest.mark.asyncio,
  pytest.mark.skipif(not httpbin_available, reason="needs pytest_httpbin"),
]

def base64_encode(s):
  return base64.b64encode(s.encode('utf-8')).decode('ascii')

async def test_regex_httpbin_default_user_agent(get_version, httpbin):
  ua = await get_version("example", {
    "source": "regex",
    "url": httpbin.url + "/get",
    "regex": r'"User-Agent":\s*"([^"]+)"',
  })
  assert ua.startswith("lilydjwg/nvchecker")

async def test_regex_httpbin_user_agent(get_version, httpbin):
  assert await get_version("example", {
    "source": "regex",
    "url": httpbin.url + "/get",
    "regex": r'"User-Agent":\s*"(\w+)"',
    "user_agent": "Meow",
  }) == "Meow"

async def test_regex(get_version, httpbin):
  assert await get_version("example", {
    "source": "regex",
    "url": httpbin.url + "/base64/" + base64_encode("version 1.12 released"),
    "regex": r'version ([0-9.]+)',
  }) == "1.12"

async def test_missing_ok(get_version, httpbin):
  assert await get_version("example", {
    "source": "regex",
    "url": httpbin.url + "/base64/" + base64_encode("something not there"),
    "regex": "foobar",
    "missing_ok": True,
  }) is None

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

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+)",
    })

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==",
    "regex": r'"user":\s*"([a-w]+)"',
  }) == "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",
    "regex": r'"token":\s*"([a-w]+):.*"',
  }) == "username"

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'

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"