2016-03-07 00:08:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-20 20:41:04 +00:00
|
|
|
# Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs
|
2014-01-05 15:50:57 +00:00
|
|
|
# Copyright (C) 2012-2014 Bastian Kleineidam
|
2016-03-07 00:08:57 +00:00
|
|
|
# Copyright (C) 2016 Tobias Gruetzmacher
|
2012-06-20 19:58:13 +00:00
|
|
|
|
2016-03-07 00:08:57 +00:00
|
|
|
import pytest
|
|
|
|
import re
|
2013-04-08 19:20:01 +00:00
|
|
|
from dosagelib.util import normaliseURL, unescape, 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_unescape(self):
|
|
|
|
# Test HTML replacement.
|
2016-03-07 00:08:57 +00:00
|
|
|
assert unescape(u'foo&bar') == u'foo&bar'
|
|
|
|
assert unescape(u'foo bar') == u'foo\xa0bar'
|
|
|
|
assert unescape(u'"foo"') == u'"foo"'
|
2012-06-20 19:58:13 +00:00
|
|
|
|
|
|
|
def test_normalisation(self):
|
|
|
|
# Test URL normalisation.
|
2016-03-07 00:08:57 +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", [
|
2012-06-20 19:58:13 +00:00
|
|
|
('<img src="%s">', ValuePrefix+'foo', True),
|
|
|
|
('< img src = "%s" >', ValuePrefix, True),
|
|
|
|
('<img class="prev" src="%s">', ValuePrefix+'...', True),
|
|
|
|
('<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()
|