Skip to content

Commit bac6cd3

Browse files
authored
adds: url check support for urls with WTI variables (#24)
* fixes: flake * adds: support for 3xx codes * fixes: tests * adds: url check support for urls with WTI variables * chore: bump version
1 parent 2fe1a8c commit bac6cd3

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from setuptools import setup, find_packages
33

44

5-
version = '0.6.0'
5+
version = '0.6.1'
66

77

88
def read(f):

tests/test_check_url.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def test_skip_urls_with_inner_params(self):
2626
actual = list(self.extractor.extract_urls('aaa http://www.{{param}}.com aaa'))
2727
self.assertEqual([], actual)
2828

29+
def test_skip_urls_with_inner_variables(self):
30+
actual = list(self.extractor.extract_urls('aaa http://www.{param}.com aaa'))
31+
self.assertEqual([], actual)
32+
2933

3034
class TestTxt(AsyncTestCase):
3135
def setUp(self):
@@ -83,11 +87,17 @@ def test_skip_parameterized_urls_from_start(self, mock_get):
8387

8488
self.assertFalse(mock_get.called)
8589

90+
@patch('aiohttp.request')
91+
def test_skip_urls_with_variables(self, mock_get):
92+
self._check(mock_get, 'aaa http://domain.com/{ticket.url}, aaa', 200)
93+
94+
self.assertFalse(mock_get.called)
95+
8696
@patch('aiohttp.request')
8797
def test_include_params_in_the_url(self, mock_get):
8898
self._check(mock_get, 'aaa http://domain.com/hello?id=123 aaa', 200)
8999

90-
mock_get.assert_called_with('get', 'http://domain.com/hello?id=123', headers=self.headers)
100+
mock_get.assert_called_with('get', 'http://domain.com/hello?id=123', allow_redirects=True, headers=self.headers)
91101

92102
@patch('aiohttp.request')
93103
def test_skip_empty_urls(self, mock_get):
@@ -111,13 +121,13 @@ def test_skip_commas(self, mock_get):
111121
def test_skip_commas_url(self, mock_get):
112122
self._check(mock_get, 'aaa http://www.google.com, aaa', 200)
113123

114-
mock_get.assert_called_with('get', 'http://www.google.com', headers=self.headers)
124+
mock_get.assert_called_with('get', 'http://www.google.com', allow_redirects=True, headers=self.headers)
115125

116126
@patch('aiohttp.request')
117127
def test_skip_chineese_commas(self, mock_get):
118128
self._check(mock_get, 'aaa http://bit.ly/UpdateKeepSafe。拥有最新版本就能解决大部分问题了。 aaa', 200)
119129

120-
mock_get.assert_called_with('get', 'http://bit.ly/UpdateKeepSafe', headers=self.headers)
130+
mock_get.assert_called_with('get', 'http://bit.ly/UpdateKeepSafe', allow_redirects=True, headers=self.headers)
121131

122132
@patch('aiohttp.request')
123133
def test_skip_keepsafe_urls(self, mock_get):
@@ -127,10 +137,10 @@ def test_skip_keepsafe_urls(self, mock_get):
127137

128138
@patch('aiohttp.request')
129139
def test_check_headers(self, mock_get):
130-
self.check = url.UrlValidator('txt', headers=self.headers)
140+
self.check = url.UrlValidator('txt', allow_redirects=True, headers=self.headers)
131141
self._check(mock_get, 'aaa http://www.google.com, aaa', 200)
132142

133-
mock_get.assert_called_with('get', 'http://www.google.com', headers=self.headers)
143+
mock_get.assert_called_with('get', 'http://www.google.com', allow_redirects=True, headers=self.headers)
134144

135145

136146
class TestHtml(AsyncTestCase):
@@ -154,7 +164,7 @@ def _check(self, mock_get, content, status_code, check=None):
154164
def test_happy_path(self, mock_get):
155165
errors = self._check(mock_get, '<a href="http://www.google.com">link</a>', 200)
156166

157-
mock_get.assert_called_with('get', 'http://www.google.com', headers=self.headers)
167+
mock_get.assert_called_with('get', 'http://www.google.com', allow_redirects=True, headers=self.headers)
158168
self.assertEqual([], errors)
159169

160170
@patch('aiohttp.request')

validator/checks/url.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(self, **kwargs):
3131
r'\];:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))'
3232

3333
def _without_params(self, url):
34-
return not bool(re.search(r'\{\{[a-zA-Z0-9_.]+\}\}', url))
34+
return not bool(re.search(r'\{[a-zA-Z0-9_.]+\}', url))
3535

3636
def _strip_non_ascii_chars(self, url):
3737
return ''.join(filter(lambda c: c in string.printable, url))
@@ -106,7 +106,7 @@ def __init__(self, headers=None):
106106
async def _make_request(self, url):
107107
try:
108108
logging.info('checking {}'.format(url))
109-
async with aiohttp.request('get', url, headers=self._headers) as res:
109+
async with aiohttp.request('get', url, headers=self._headers, allow_redirects=True) as res:
110110
return res.status
111111
except Exception:
112112
logging.error('Error making request to %s', url)
@@ -196,11 +196,11 @@ class UrlOccurenciesValidator(UrlValidator):
196196
def check(self, data, parser, reader):
197197
error = []
198198
for row in data:
199-
base = row.pop(0)
200-
base_urls = self._get_urls([[base]], parser, reader)
201-
for other in row:
202-
other_urls = self._get_urls([[other]], parser, reader)
203-
error.append(UrlOccurencyDiff(base, other, base_urls, other_urls))
199+
base = row.pop(0)
200+
base_urls = self._get_urls([[base]], parser, reader)
201+
for other in row:
202+
other_urls = self._get_urls([[other]], parser, reader)
203+
error.append(UrlOccurencyDiff(base, other, base_urls, other_urls))
204204
return [x for x in error if not x.is_valid()]
205205

206206
def async_check(self, *args):

0 commit comments

Comments
 (0)