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
|
"""
Part of the tagit test suite.
A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2022
"""
# imports
import operator
import unittest
# objects to test
from bsfs.utils.uri import URI
## code ##
class TestURI(unittest.TestCase):
def test_new(self):
# cannot create an unparseable URI
self.assertRaises(ValueError, URI, 'http://')
# returns URI otherwise
self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment'), URI)
def test_is_parseable(self):
# empty string is a parseable uri
self.assertTrue(URI.is_parseable(''))
# examples from the RFC are parseable
self.assertTrue(URI.is_parseable('foo://example.com:8042/over/there?name=ferret#nose'))
self.assertTrue(URI.is_parseable('urn:example:animal:ferret:nose'))
self.assertTrue(URI.is_parseable('mailto:fred@xample.com'))
self.assertTrue(URI.is_parseable('www.w3.org/Addressing/'))
self.assertTrue(URI.is_parseable('ftp://cnn.example.com&store=breaking_news@10.0.0.1/top_story.htm'))
self.assertTrue(URI.is_parseable('ftp://ftp.is.co.za/rfc/rfc1808.txt'))
self.assertTrue(URI.is_parseable('http://www.ietf.org/rfc/rfc2396.txt'))
self.assertTrue(URI.is_parseable('ldap://[2001:db8::7]/c=GB?objectClass?one'))
self.assertTrue(URI.is_parseable('mailto:John.Doe@example.com'))
self.assertTrue(URI.is_parseable('news:comp.infosystems.www.servers.unix'))
self.assertTrue(URI.is_parseable('tel:+1-816-555-1212'))
self.assertTrue(URI.is_parseable('telnet://192.0.2.16:80/'))
self.assertTrue(URI.is_parseable('urn:oasis:names:specification:docbook:dtd:xml:4.1.2'))
# uri cannot end with a scheme delimiter
self.assertFalse(URI.is_parseable('http://'))
# port must be a number
self.assertFalse(URI.is_parseable('http://example.com:foo/'))
# the double slash (//) implies a authority
self.assertFalse(URI.is_parseable('http:///path0/path1?query#fragment'))
def test_compose(self):
self.assertEqual(URI.compose('path'), '/path')
self.assertEqual(URI.compose('/path'), '/path') # leading slash is not repeated
self.assertEqual(URI.compose('path', scheme='scheme'), 'scheme:/path')
self.assertEqual(URI.compose('path', authority='authority'), '//authority/path')
self.assertEqual(URI.compose('path', host='host'), '//host/path')
self.assertEqual(URI.compose('path', user='user'), '/path') # user w/o host is ignored
self.assertEqual(URI.compose('path', host='host', user='user'), '//user@host/path')
self.assertEqual(URI.compose('path', port='port'), '/path') # port w/o host is ignored
self.assertEqual(URI.compose('path', host='host', port=1234), '//host:1234/path')
self.assertEqual(URI.compose('path', host='host', port='1234'), '//host:1234/path')
self.assertRaises(ValueError, URI.compose, 'path', host='host', port='foo') # port must be a number
self.assertEqual(URI.compose('path', host='host', user='foo', port='1234'), '//foo@host:1234/path')
self.assertEqual(URI.compose('path', query='query'), '/path?query')
self.assertEqual(URI.compose('path', fragment='fragment'), '/path#fragment')
self.assertEqual(URI.compose('path', 'scheme', 'authority', 'user', 'host', 1234, 'query', 'fragment'),
'scheme://user@host:1234/path?query#fragment')
def test_get(self):
# get returns the respective component
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query#fragment').get('scheme'), 'http')
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query#fragment').get('authority'), 'user@www.example.com:1234')
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query#fragment').get('userinfo'), 'user')
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query#fragment').get('host'), 'www.example.com')
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query#fragment').get('port'), 1234)
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query#fragment').get('path'), '/path0/path1')
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query#fragment').get('query'), 'query')
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query#fragment').get('fragment'), 'fragment')
# get returns a default value if the component is missing
class Foo(): pass
foo = Foo()
self.assertEqual(URI('//user@www.example.com:1234/path0/path1?query#fragment').get('scheme', foo), foo)
self.assertEqual(URI('/path0/path1?query#fragment').get('authority', foo), foo)
self.assertEqual(URI('http://www.example.com:1234/path0/path1?query#fragment').get('userinfo', foo), foo)
self.assertEqual(URI('/path0/path1?query#fragment').get('host', foo), foo)
self.assertEqual(URI('http://user@www.example.com/path0/path1?query#fragment').get('port', foo), foo)
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1#fragment').get('query', foo), foo)
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query').get('fragment', foo), foo)
# can only get components
self.assertRaises(ValueError, URI('').get, 'foobar')
def test_scheme(self):
# full URI
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query#fragment').scheme, 'http')
self.assertEqual(URI('ftp://user@www.example.com:1234/path0/path1?query#fragment').scheme, 'ftp')
self.assertEqual(URI('myown://user@www.example.com:1234/path0/path1?query#fragment').scheme, 'myown')
# empty scheme
self.assertRaises(ValueError, getattr, URI('www.example.com/path0/path1?query#fragment'), 'scheme')
# empty URI
self.assertRaises(ValueError, getattr, URI(''), 'scheme')
def test_authority(self):
# full URI
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query#fragment').authority, 'user@www.example.com:1234')
# empty authority
self.assertRaises(ValueError, getattr, URI('http/path0/path1?query#fragment'), 'authority')
# empty URI
self.assertRaises(ValueError, getattr, URI(''), 'authority')
def test_userinfo(self):
# full URI
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query#fragment').scheme, 'http')
# empty authority
self.assertRaises(ValueError, getattr, URI('http/path0/path1?query#fragment'), 'userinfo')
# empty userinfo
self.assertRaises(ValueError, getattr, URI('http://www.example.com:1234/path0/path1?query#fragment'), 'userinfo')
# empty URI
self.assertRaises(ValueError, getattr, URI(''), 'userinfo')
def test_host(self):
# full URI
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query#fragment').host, 'www.example.com')
# IPv4 host
self.assertEqual(URI('http://user@10.0.0.1:1234/path0/path1?query#fragment').host, '10.0.0.1')
# IPv6 host
self.assertEqual(URI('http://user@[::64]:1234/path0/path1?query#fragment').host, '[::64]')
# empty authority
self.assertRaises(ValueError, getattr, URI('http/path0/path1?query#fragment'), 'host')
# empty URI
self.assertRaises(ValueError, getattr, URI(''), 'host')
def test_port(self):
# full URI
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query#fragment').port, 1234)
# empty authority
self.assertRaises(ValueError, getattr, URI('http/path0/path1?query#fragment'), 'port')
# empty port
self.assertRaises(ValueError, getattr, URI('http://user@www.example.com/path0/path1?query#fragment'), 'port')
# empty URI
self.assertRaises(ValueError, getattr, URI(''), 'port')
def test_path(self):
# full URI
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query#fragment').path, '/path0/path1')
# empty path
self.assertEqual(URI('http://user@www.example.com:1234?query#fragment').path, '')
# empty URI
self.assertEqual(URI('').path, '')
def test_query(self):
# full URI
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query#fragment').query, 'query')
# empty query
self.assertRaises(ValueError, getattr, URI('http://user@www.example.com:1234/path0/path1#fragment'), 'query')
# empty URI
self.assertRaises(ValueError, getattr, URI(''), 'query')
def test_fragment(self):
# full URI
self.assertEqual(URI('http://user@www.example.com:1234/path0/path1?query#fragment').fragment, 'fragment')
# empty fragment
self.assertRaises(ValueError, getattr, URI('http://user@www.example.com:1234/path0/path1?query'), 'fragment')
# empty URI
self.assertRaises(ValueError, getattr, URI(''), 'fragment')
def test_overloaded(self):
# composition
self.assertIsInstance(URI('http://user@www.example.com:1234/{}/path1?{}#fragment') + 'hello', URI)
self.assertIsInstance(URI('http://user@www.example.com:1234/{}/path1?{}#fragment') * 2, URI)
self.assertIsInstance(2 * URI('http://user@www.example.com:1234/{}/path1?{}#fragment'), URI) # rmul
self.assertIsInstance(URI('http://user@www.example.com:1234/{}/path1?{}#fragment').join(['hello', 'world']) , URI)
# stripping
self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').strip(), URI)
self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').lstrip(), URI)
self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').rstrip(), URI)
# case fold
self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').lower(), URI)
self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').upper(), URI)
# formatting
self.assertIsInstance(URI('http://user@www.example.com:1234/{}/path1?{}#fragment').format('hello', 'world'), URI)
self.assertIsInstance(URI('http://user@www.example.com:1234/%s/path1?%s#fragment') % ('hello', 'world'), URI)
self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').replace('path0', 'pathX'), URI)
## main ##
if __name__ == '__main__':
unittest.main()
## EOF ##
|