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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
import ipaddress
import unittest
from uritools import uricompose
class ComposeTest(unittest.TestCase):
def check(self, uri, **kwargs):
result = uricompose(**kwargs)
self.assertEqual(
uri, result, msg="%r != %r (kwargs=%r)" % (uri, result, kwargs)
)
def test_rfc3986(self):
"""uricompose test cases from [RFC3986] 3. Syntax Components"""
self.check(
"foo://example.com:42/over/there?name=ferret#nose",
scheme="foo",
authority="example.com:42",
path="/over/there",
query="name=ferret",
fragment="nose",
)
self.check(
"urn:example:animal:ferret:nose",
scheme="urn",
path="example:animal:ferret:nose",
)
def test_scheme(self):
cases = [
("foo+bar:", "foo+bar"),
("foo+bar:", b"foo+bar"),
("foo+bar:", "FOO+BAR"),
("foo+bar:", b"FOO+BAR"),
]
for uri, scheme in cases:
self.check(uri, scheme=scheme)
# invalid scheme
for scheme in ("", "foo:", "\xf6lk\xfcrbis"):
with self.assertRaises(ValueError, msg="scheme=%r" % scheme):
uricompose(scheme=scheme)
def test_authority(self):
cases = [
("", None),
("//", ""),
("//", b""),
("//example.com", "example.com"),
("//example.com", b"example.com"),
("//example.com", "example.com:"),
("//example.com", b"example.com:"),
("//user@example.com", "user@example.com"),
("//user@example.com", b"user@example.com"),
("//example.com:42", "example.com:42"),
("//example.com:42", b"example.com:42"),
("//user@example.com:42", "user@example.com:42"),
("//user@example.com:42", b"user@example.com:42"),
("//user@127.0.0.1:42", "user@127.0.0.1:42"),
("//user@127.0.0.1:42", b"user@127.0.0.1:42"),
("//user@[::1]:42", "user@[::1]:42"),
("//user@[::1]:42", b"user@[::1]:42"),
("//user:c2VjcmV0@example.com", "user:c2VjcmV0@example.com"),
("//user:c2VjcmV0@example.com", b"user:c2VjcmV0@example.com"),
]
for uri, authority in cases:
self.check(uri, authority=authority)
# invalid authority type
for authority in (True, 42, 3.14, ipaddress.IPv6Address("::1")):
with self.assertRaises(TypeError, msg="authority=%r" % authority):
uricompose(authority=authority)
def test_authority_kwargs(self):
from ipaddress import IPv4Address, IPv6Address
cases = [
("", [None, None, None]),
("//", [None, "", None]),
("//", [None, b"", None]),
("//example.com", [None, "example.com", None]),
("//example.com", [None, b"example.com", None]),
("//example.com", [None, "example.com", ""]),
("//example.com", [None, "example.com", b""]),
("//user@example.com", ["user", "example.com", None]),
("//user@example.com", [b"user", "example.com", None]),
("//user@example.com", [b"user", b"example.com", None]),
("//example.com:42", [None, "example.com", "42"]),
("//example.com:42", [None, b"example.com", "42"]),
("//example.com:42", [None, b"example.com", b"42"]),
("//example.com:42", [None, "example.com", 42]),
("//example.com:42", [None, b"example.com", 42]),
("//user@example.com:42", ["user", "example.com", "42"]),
("//user@example.com:42", [b"user", "example.com", "42"]),
("//user@example.com:42", [b"user", b"example.com", "42"]),
("//user@example.com:42", [b"user", b"example.com", b"42"]),
("//user@example.com:42", ["user", "example.com", 42]),
("//user@example.com:42", [b"user", "example.com", 42]),
("//user@example.com:42", [b"user", b"example.com", 42]),
("//user@127.0.0.1:42", ["user", "127.0.0.1", 42]),
("//user@127.0.0.1:42", ["user", b"127.0.0.1", 42]),
("//user@127.0.0.1:42", ["user", IPv4Address("127.0.0.1"), 42]),
("//user@[::1]:42", ["user", "::1", 42]),
("//user@[::1]:42", ["user", b"::1", 42]),
("//user@[::1]:42", ["user", "[::1]", 42]),
("//user@[::1]:42", ["user", b"[::1]", 42]),
("//user@[::1]:42", ["user", IPv6Address("::1"), 42]),
]
for uri, authority in cases:
self.check(uri, authority=authority)
userinfo, host, port = authority
self.check(uri, userinfo=userinfo, host=host, port=port)
# invalid authority value
for authority in ([], ["foo"], ["foo", "bar"], range(4)):
with self.assertRaises(ValueError, msg="authority=%r" % authority):
uricompose(authority=authority)
# invalid host type
for host in (True, 42, 3.14, ipaddress.IPv6Network("2001:db00::0/24")):
with self.assertRaises(AttributeError, msg="host=%r" % host):
uricompose(authority=[None, host, None])
with self.assertRaises(AttributeError, msg="host=%r" % host):
uricompose(host=host)
# invalid host ip-literal
for host in ("[foo]", "[v1.x]"):
with self.assertRaises(ValueError, msg="host=%r" % host):
uricompose(authority=[None, host, None])
with self.assertRaises(ValueError, msg="host=%r" % host):
uricompose(host=host)
# invalid port value
for port in (-1, "foo", 3.14):
with self.assertRaises(ValueError, msg="port=%r" % port):
uricompose(authority=[None, "", port])
with self.assertRaises(ValueError, msg="port=%r" % port):
uricompose(port=port)
def test_authority_override(self):
cases = [
("//user@example.com:42", None, "user", "example.com", 42),
("//user@example.com:42", "", "user", "example.com", 42),
("//user@example.com:42", "example.com", "user", None, 42),
("//user@example.com:42", "user@:42", None, "example.com", None),
]
for uri, authority, userinfo, host, port in cases:
self.check(
uri, authority=authority, userinfo=userinfo, host=host, port=port
)
def test_host_lowercase(self):
cases = [
("//hostname", "HostName"),
("//[2001:db8::1]", "[2001:DB8::1]"),
(
"//uuid%3A228f0766-a241-4050-a7a8-2c153073e3d7",
"UUID:228F0766-A241-4050-A7A8-2C153073E3D7",
),
]
for uri, host in cases:
self.check(uri, host=host)
def test_path(self):
cases = [
("foo", "foo"),
("foo", b"foo"),
("foo+bar", "foo+bar"),
("foo+bar", b"foo+bar"),
("foo%20bar", "foo bar"),
("foo%20bar", b"foo bar"),
("./this:that", "this:that"),
("./this:that", b"this:that"),
("./this:that/", "this:that/"),
("./this:that/", b"this:that/"),
]
for uri, path in cases:
self.check(uri, path=path)
# invalid path with authority
for path in ("foo", b"foo"):
with self.assertRaises(ValueError, msg="path=%r" % path):
uricompose(authority="auth", path=path)
# invalid path without authority
for path in ("//", b"//", "//foo", b"//foo"):
with self.assertRaises(ValueError, msg="path=%r" % path):
uricompose(path=path)
def test_query(self):
from collections import OrderedDict as od
cases = [
("?", ""),
("?", b""),
("?", []),
("?", {}),
("?name", "name"),
("?name", b"name"),
("?name", [("name", None)]),
("?name", [(b"name", None)]),
("?name", {"name": None}),
("?name", {b"name": None}),
("?name=foo", "name=foo"),
("?name=foo", b"name=foo"),
("?name=foo", [("name", "foo")]),
("?name=foo", [("name", b"foo")]),
("?name=foo", [(b"name", b"foo")]),
("?name=foo", {"name": "foo"}),
("?name=foo", {"name": b"foo"}),
("?name=foo", {"name": ["foo"]}),
("?name=foo", {"name": [b"foo"]}),
("?name=foo", {b"name": b"foo"}),
("?name=foo", {b"name": [b"foo"]}),
("?name=42", [("name", 42)]),
("?name=42", {"name": 42}),
("?name=42", {"name": [42]}),
("?name=foo&type=bar", [("name", "foo"), ("type", "bar")]),
("?name=foo&type=bar", od([("name", "foo"), ("type", "bar")])),
("?name=foo&name=bar", [("name", "foo"), ("name", "bar")]),
("?name=foo&name=bar", {"name": ["foo", "bar"]}),
("?name=a/b/c", dict(name="a/b/c")),
("?name=a:b:c", dict(name="a:b:c")),
("?name=a?b?c", dict(name="a?b?c")),
("?name=a@b@c", dict(name="a@b@c")),
("?name=a;b;c", dict(name="a;b;c")),
("?name=a%23b%23c", dict(name="a#b#c")),
("?name=a%26b%26c", dict(name="a&b&c")),
]
for uri, query in cases:
self.check(uri, query=query)
# invalid query type
for query in (0, [1]):
with self.assertRaises(TypeError, msg="query=%r" % query):
uricompose(query=query)
def test_query_sep(self):
cases = [
("&", "?x=foo&y=bar", [("x", "foo"), ("y", "bar")]),
(";", "?x=foo;y=bar", [("x", "foo"), ("y", "bar")]),
("&", "?x=foo;y=bar", [("x", "foo;y=bar")]),
(";", "?x=foo&y=bar", [("x", "foo&y=bar")]),
]
for sep, uri, query in cases:
self.check(uri, query=query, querysep=sep)
|