def test_incorrect_urlencoded_path(self): 'Incorrect url encoded path' t = UrlTemplate('/<name>') value = quote(u'/имя'.encode('utf-8')) + '%r1' self.assertEqual(t.match(value), (True, { 'name': u'\u0438\u043c\u044f%r1' }))
def test_incorrect_value(self): 'Incorrect url encoded value' t = UrlTemplate('/<name>') value = quote(u'/имя'.encode('utf-8'))[:-1] self.assertEqual(t.match(value), (True, { 'name': u'\u0438\u043c\ufffd%8' }))
def test_match_from_begining_without_params(self): 'UrlTemplate match method without params (from begining of str)' ut = UrlTemplate('simple', match_whole_str=False) self.assertEqual(ut.match('simple'), (True, {})) self.assertEqual(ut.match('simple/sdffds'), (True, {})) self.assertEqual(ut.match('/simple'), (False, {})) self.assertEqual(ut.match('/simple/'), (False, {}))
def test_multiple_converters(self): 'Multiple converters' t = UrlTemplate('/<name>/text/<action>') self.assertEqual(t.match(quote('/this/text/edit')), (True, { 'name': 'this', 'action': 'edit' }))
def test_unicode(self): 'Unicode values of converters' t = UrlTemplate('/<name>/text/<action>') self.assertEqual(t.match(quote(u'/имя/text/действие'.encode('utf-8'))), (True, { 'name': u'имя', 'action': u'действие' }))
def test_multiple_converters_postfix(self): 'Multiple converters with postfix data' t = UrlTemplate('/<name>/text/<action>/post') self.assertEqual(t.match(quote(u'/this/text/edit/post')), (True, { 'name': 'this', 'action': 'edit' })) self.assertEqual(t.match(quote(u'/this/text/edit')), (False, {}))
def test_converter_with_args(self): 'Converter with args' class Conv(Converter): def __init__(self, *items): self.items = items def to_python(self, value, **kw): if value not in self.items: raise ConvertError(self.name, value) return value t = UrlTemplate('/<conv(u"text", u"test"):name>', converters=[Conv]) value = quote(u'/имя'.encode('utf-8')) self.assertEqual(t.match(value), (False, {})) value = quote(u'/text'.encode('utf-8')) self.assertEqual(t.match(value), (True, {'name': u'text'})) value = quote(u'/test'.encode('utf-8')) self.assertEqual(t.match(value), (True, {'name': u'test'}))
def test_match_from_begining_with_params(self): 'UrlTemplate match method with params (from begining of str)' ut = UrlTemplate('/simple/<int:id>', match_whole_str=False) self.assertEqual(ut.match('/simple/2'), (True, {'id':2})) self.assertEqual(ut.match('/simple/2/sdfsf'), (True, {'id':2})) self.assertEqual(ut.match('/simple'), (False, {})) self.assertEqual(ut.match('/simple/d'), (False, {})) self.assertEqual(ut.match('/simple/d/sdfsdf'), (False, {}))
def test_match_with_params(self): 'UrlTemplate match method with params' ut = UrlTemplate('/simple/<int:id>') self.assertEqual(ut.match('/simple/2'), (True, {'id':2})) self.assertEqual(ut.match('/simple'), (False, {})) self.assertEqual(ut.match('/simple/d'), (False, {}))
def test_incorrect_value(self): 'Incorrect url encoded value' t = UrlTemplate('/<name>') value = quote(u'/имя'.encode('utf-8'))[:-1] self.assertEqual(t.match(value), (True, {'name': u'\u0438\u043c\ufffd%8'}))
def test_multiple_converters_postfix(self): 'Multiple converters with postfix data' t = UrlTemplate('/<name>/text/<action>/post') self.assertEqual(t.match(quote(u'/this/text/edit/post')), (True, {'name': 'this', 'action': 'edit'})) self.assertEqual(t.match(quote(u'/this/text/edit')), (False, {}))
def test_default_converter(self): 'Default converter test' t = UrlTemplate('/<name>') self.assertEqual(t.match('/somevalue'), (True, {'name': 'somevalue'}))
def test_static_text(self): 'Simple text match' t = UrlTemplate('/test/url') self.assertEqual(t.match('/test/url'), (True, {}))
def test_unknown_converter(self): 'Unknown converter' self.assertRaises(KeyError, lambda: UrlTemplate('/<baba:name>/')) self.assertRaises(KeyError, lambda: UrlTemplate('/<baba:name></'))
def test_incorrect_url_template1(self): 'Incorrect url template 1' self.assertRaises(ValueError, lambda: UrlTemplate('/<:name>/'))
def test_match(self): 'Simple match' t = UrlTemplate('/') self.assertEqual(t.match('/'), (True, {}))
def test_converter(self): 'Simple text match' t = UrlTemplate('/<string:name>') self.assertEqual(t.match('/somevalue'), (True, {'name': 'somevalue'}))
def test_multiple_converters(self): 'Multiple converters' t = UrlTemplate('/<name>/text/<action>') self.assertEqual(t.match(quote('/this/text/edit')), (True, {'name': 'this', 'action': 'edit'}))
def test_unicode(self): 'Unicode values of converters' t = UrlTemplate('/<name>/text/<action>') self.assertEqual(t.match(quote(u'/имя/text/действие'.encode('utf-8'))), (True, {'name': u'имя', 'action': u'действие'}))
def test_incorrect_urlencoded_path(self): 'Incorrect url encoded path' t = UrlTemplate('/<name>') value = quote(u'/имя'.encode('utf-8'))+'%r1' self.assertEqual(t.match(value), (True, {'name': u'\u0438\u043c\u044f%r1'}))
def test_match_without_params(self): 'UrlTemplate match method without params' ut = UrlTemplate('simple') self.assertEqual(ut.match('simple'), (True, {})) self.assertEqual(ut.match('/simple'), (False, {}))