2016-03-07 00:08:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-10-28 22:21:41 +00:00
|
|
|
# Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs
|
2014-01-05 15:50:57 +00:00
|
|
|
# Copyright (C) 2012-2014 Bastian Kleineidam
|
2020-04-12 23:53:45 +00:00
|
|
|
# Copyright (C) 2015-2020 Tobias Gruetzmacher
|
2016-03-07 00:08:57 +00:00
|
|
|
import pytest
|
|
|
|
import re
|
2020-04-12 23:53:45 +00:00
|
|
|
from dosagelib.util import normaliseURL, tagre, get_system_uid
|
2012-06-20 19:58:13 +00:00
|
|
|
|
|
|
|
|
2016-03-07 00:08:57 +00:00
|
|
|
class TestURL(object):
|
2012-06-20 19:58:13 +00:00
|
|
|
"""
|
|
|
|
Tests for URL utility functions.
|
|
|
|
"""
|
2016-03-07 00:08:57 +00:00
|
|
|
|
2012-06-20 19:58:13 +00:00
|
|
|
def test_normalisation(self):
|
|
|
|
# Test URL normalisation.
|
2018-06-29 17:26:17 +00:00
|
|
|
assert (normaliseURL('http://example.com//bar/baz&baz') ==
|
|
|
|
u'http://example.com/bar/baz&baz')
|
2012-06-20 19:58:13 +00:00
|
|
|
|
|
|
|
|
2016-03-07 00:08:57 +00:00
|
|
|
class TestRegex(object):
|
2012-06-20 19:58:13 +00:00
|
|
|
|
|
|
|
ValuePrefix = '/bla/'
|
2016-03-07 00:08:57 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("tag,value,domatch", [
|
2017-05-14 22:54:02 +00:00
|
|
|
('<img src="%s">', ValuePrefix + 'foo', True),
|
2012-06-20 19:58:13 +00:00
|
|
|
('< img src = "%s" >', ValuePrefix, True),
|
2017-05-14 22:54:02 +00:00
|
|
|
('<img class="prev" src="%s">', ValuePrefix + '...', True),
|
2012-06-20 19:58:13 +00:00
|
|
|
('<img origsrc="%s">', ValuePrefix, False),
|
|
|
|
('<Img src="%s">', ValuePrefix, True),
|
|
|
|
('<img SrC="%s">', ValuePrefix, True),
|
|
|
|
('<img src="%s">', ValuePrefix[:-1], False),
|
2012-10-11 19:32:42 +00:00
|
|
|
('<img class="prev" src="%s" a="b">', ValuePrefix, True),
|
2016-03-07 00:08:57 +00:00
|
|
|
])
|
|
|
|
def test_regex(self, tag, value, domatch):
|
|
|
|
matcher = re.compile(tagre("img", "src", '(%s[^"]*)' %
|
|
|
|
self.ValuePrefix))
|
|
|
|
self.match_tag(matcher, tag, value, domatch)
|
2012-06-20 19:58:13 +00:00
|
|
|
|
|
|
|
def match_tag(self, matcher, tag, value, domatch=True):
|
2012-10-11 15:02:40 +00:00
|
|
|
text = tag % value
|
|
|
|
match = matcher.search(text)
|
2012-06-20 19:58:13 +00:00
|
|
|
if domatch:
|
2016-03-07 00:08:57 +00:00
|
|
|
assert match, "%s should match %s" % (matcher.pattern, text)
|
|
|
|
assert match.group(1) == value
|
2012-06-20 19:58:13 +00:00
|
|
|
else:
|
2016-03-07 00:08:57 +00:00
|
|
|
assert not match, "%s should not match %s" % (matcher.pattern,
|
|
|
|
text)
|
2013-04-08 19:20:01 +00:00
|
|
|
|
|
|
|
|
2016-03-07 00:08:57 +00:00
|
|
|
class TestUid(object):
|
2013-04-08 19:20:01 +00:00
|
|
|
"""
|
|
|
|
Tests for unique system IDs.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_system_uid(self):
|
2016-03-07 00:08:57 +00:00
|
|
|
assert get_system_uid()
|