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
|
from unittest import TestCase
from openid.yadis import xri
class XriDiscoveryTestCase(TestCase):
def test_isXRI(self):
i = xri.identifierScheme
self.assertEqual(i('=john.smith'), 'XRI')
self.assertEqual(i('@smiths/john'), 'XRI')
self.assertEqual(i('smoker.myopenid.com'), 'URI')
self.assertEqual(i('xri://=john'), 'XRI')
self.assertEqual(i(''), 'URI')
class XriEscapingTestCase(TestCase):
def test_escaping_percents(self):
self.assertEqual(xri.escapeForIRI('@example/abc%2Fd/ef'),
'@example/abc%252Fd/ef')
def test_escaping_xref(self):
# no escapes
esc = xri.escapeForIRI
self.assertEqual('@example/foo/(@bar)', esc('@example/foo/(@bar)'))
# escape slashes
self.assertEqual('@example/foo/(@bar%2Fbaz)',
esc('@example/foo/(@bar/baz)'))
self.assertEqual('@example/foo/(@bar%2Fbaz)/(+a%2Fb)',
esc('@example/foo/(@bar/baz)/(+a/b)'))
# escape query ? and fragment #
self.assertEqual('@example/foo/(@baz%3Fp=q%23r)?i=j#k',
esc('@example/foo/(@baz?p=q#r)?i=j#k'))
class XriTransformationTestCase(TestCase):
def test_to_iri_normal(self):
self.assertEqual(xri.toIRINormal('@example'), 'xri://@example')
try:
chr(0x10000)
except ValueError:
# bleh narrow python build
def test_iri_to_url(self):
s = 'l\xa1m'
expected = 'l%C2%A1m'
self.assertEqual(xri.iriToURI(s), expected)
else:
def test_iri_to_url(self):
s = 'l\xa1m\U00101010n'
expected = 'l%C2%A1m%F4%81%80%90n'
self.assertEqual(xri.iriToURI(s), expected)
class CanonicalIDTest(TestCase):
def mkTest(providerID, canonicalID, isAuthoritative):
def test(self):
result = xri.providerIsAuthoritative(providerID, canonicalID)
format = "%s providing %s, expected %s"
message = format % (providerID, canonicalID, isAuthoritative)
self.assertEqual(isAuthoritative, result, message)
return test
test_equals = mkTest('=', '=!698.74D1.A1F2.86C7', True)
test_atOne = mkTest('@!1234', '@!1234!ABCD', True)
test_atTwo = mkTest('@!1234!5678', '@!1234!5678!ABCD', True)
test_atEqualsFails = mkTest('@!1234', '=!1234!ABCD', False)
test_tooDeepFails = mkTest('@!1234', '@!1234!ABCD!9765', False)
test_atEqualsAndTooDeepFails = mkTest('@!1234!ABCD', '=!1234', False)
test_differentBeginningFails = mkTest('=!BABE', '=!D00D', False)
class TestGetRootAuthority(TestCase):
def mkTest(the_xri, expected_root):
def test(self):
actual_root = xri.rootAuthority(the_xri)
self.assertEqual(actual_root, xri.XRI(expected_root))
return test
test_at = mkTest("@foo", "@")
test_atStar = mkTest("@foo*bar", "@")
test_atStarStar = mkTest("@*foo*bar", "@")
test_atWithPath = mkTest("@foo/bar", "@")
test_bangBang = mkTest("!!990!991", "!")
test_bang = mkTest("!1001!02", "!")
test_equalsStar = mkTest("=foo*bar", "=")
test_xrefPath = mkTest("(example.com)/foo", "(example.com)")
test_xrefStar = mkTest("(example.com)*bar/foo", "(example.com)")
test_uriAuth = mkTest("baz.example.com/foo", "baz.example.com")
test_uriAuthPort = mkTest("baz.example.com:8080/foo",
"baz.example.com:8080")
# Looking at the ABNF in XRI Syntax 2.0, I don't think you can
# have example.com*bar. You can do (example.com)*bar, but that
# would mean something else.
##("example.com*bar/(=baz)", "example.com*bar"),
##("baz.example.com!01/foo", "baz.example.com!01"),
if __name__ == '__main__':
import unittest
unittest.main()
|