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
|
import unittest
from uritools import urijoin
class JoinTest(unittest.TestCase):
RFC3986_BASE = "http://a/b/c/d;p?q"
def check(self, base, ref, expected, strict=False):
self.assertEqual(expected, urijoin(base, ref, strict))
# base as bytes, ref as str
self.assertEqual(expected, urijoin(base.encode(), ref, strict))
# base as str, ref as bytes
self.assertEqual(expected, urijoin(base, ref.encode(), strict))
# both base and ref as bytes
self.assertEqual(
expected.encode(), urijoin(base.encode(), ref.encode(), strict)
)
def test_rfc3986_normal(self):
"""urijoin test cases from RFC 3986 5.4.1. Normal Examples"""
self.check(self.RFC3986_BASE, "g:h", "g:h")
self.check(self.RFC3986_BASE, "g", "http://a/b/c/g")
self.check(self.RFC3986_BASE, "./g", "http://a/b/c/g")
self.check(self.RFC3986_BASE, "g/", "http://a/b/c/g/")
self.check(self.RFC3986_BASE, "/g", "http://a/g")
self.check(self.RFC3986_BASE, "//g", "http://g")
self.check(self.RFC3986_BASE, "?y", "http://a/b/c/d;p?y")
self.check(self.RFC3986_BASE, "g?y", "http://a/b/c/g?y")
self.check(self.RFC3986_BASE, "#s", "http://a/b/c/d;p?q#s")
self.check(self.RFC3986_BASE, "g#s", "http://a/b/c/g#s")
self.check(self.RFC3986_BASE, "g?y#s", "http://a/b/c/g?y#s")
self.check(self.RFC3986_BASE, ";x", "http://a/b/c/;x")
self.check(self.RFC3986_BASE, "g;x", "http://a/b/c/g;x")
self.check(self.RFC3986_BASE, "g;x?y#s", "http://a/b/c/g;x?y#s")
self.check(self.RFC3986_BASE, "", "http://a/b/c/d;p?q")
self.check(self.RFC3986_BASE, ".", "http://a/b/c/")
self.check(self.RFC3986_BASE, "./", "http://a/b/c/")
self.check(self.RFC3986_BASE, "..", "http://a/b/")
self.check(self.RFC3986_BASE, "../", "http://a/b/")
self.check(self.RFC3986_BASE, "../g", "http://a/b/g")
self.check(self.RFC3986_BASE, "../..", "http://a/")
self.check(self.RFC3986_BASE, "../../", "http://a/")
self.check(self.RFC3986_BASE, "../../g", "http://a/g")
def test_rfc3986_abnormal(self):
"""urijoin test cases from RFC 3986 5.4.2. Abnormal Examples"""
self.check(self.RFC3986_BASE, "../../../g", "http://a/g")
self.check(self.RFC3986_BASE, "../../../../g", "http://a/g")
self.check(self.RFC3986_BASE, "/./g", "http://a/g")
self.check(self.RFC3986_BASE, "/../g", "http://a/g")
self.check(self.RFC3986_BASE, "g.", "http://a/b/c/g.")
self.check(self.RFC3986_BASE, ".g", "http://a/b/c/.g")
self.check(self.RFC3986_BASE, "g..", "http://a/b/c/g..")
self.check(self.RFC3986_BASE, "..g", "http://a/b/c/..g")
self.check(self.RFC3986_BASE, "./../g", "http://a/b/g")
self.check(self.RFC3986_BASE, "./g/.", "http://a/b/c/g/")
self.check(self.RFC3986_BASE, "g/./h", "http://a/b/c/g/h")
self.check(self.RFC3986_BASE, "g/../h", "http://a/b/c/h")
self.check(self.RFC3986_BASE, "g;x=1/./y", "http://a/b/c/g;x=1/y")
self.check(self.RFC3986_BASE, "g;x=1/../y", "http://a/b/c/y")
self.check(self.RFC3986_BASE, "g?y/./x", "http://a/b/c/g?y/./x")
self.check(self.RFC3986_BASE, "g?y/../x", "http://a/b/c/g?y/../x")
self.check(self.RFC3986_BASE, "g#s/./x", "http://a/b/c/g#s/./x")
self.check(self.RFC3986_BASE, "g#s/../x", "http://a/b/c/g#s/../x")
self.check(self.RFC3986_BASE, "http:g", "http:g", True)
self.check(self.RFC3986_BASE, "http:g", "http://a/b/c/g", False)
def test_rfc3986_merge(self):
"""urijoin test cases for RFC 3986 5.2.3. Merge Paths"""
self.check("http://a", "b", "http://a/b")
def test_relative_base(self):
self.check("", "bar", "bar")
self.check("foo", "bar", "bar")
self.check("foo/", "bar", "foo/bar")
self.check(".", "bar", "bar")
self.check("./", "bar", "bar")
self.check("./foo", "bar", "bar")
self.check("./foo/", "bar", "foo/bar")
self.check("..", "bar", "bar")
self.check("../", "bar", "../bar")
self.check("../foo", "bar", "../bar")
self.check("../foo/", "bar", "../foo/bar")
self.check("", "../bar", "../bar")
self.check("foo", "../bar", "../bar")
self.check("foo/", "../bar", "bar")
self.check(".", "../bar", "../bar")
self.check("./", "../bar", "../bar")
self.check("./foo", "../bar", "../bar")
self.check("./foo/", "../bar", "bar")
self.check("..", "../bar", "../bar")
self.check("../", "../bar", "../../bar")
self.check("../foo", "../bar", "../../bar")
self.check("../foo/", "../bar", "../bar")
|