aboutsummaryrefslogtreecommitdiffstats
path: root/test/utils/test_uri.py
diff options
context:
space:
mode:
authorMatthias Baumgartner <dev@igsor.net>2022-12-18 14:21:11 +0100
committerMatthias Baumgartner <dev@igsor.net>2022-12-18 14:21:11 +0100
commit91437ba89d35bf482f3d9671bb99ef2fc69f5985 (patch)
treee9bfe27e5a641c040cfa8fe747a7cbb28091079c /test/utils/test_uri.py
parent87e4cd5a4581094f490f79d4f1cf91f51897660f (diff)
parente94368c75468e3e94382b12705e55d396249eaca (diff)
downloadbsfs-91437ba89d35bf482f3d9671bb99ef2fc69f5985.tar.gz
bsfs-91437ba89d35bf482f3d9671bb99ef2fc69f5985.tar.bz2
bsfs-91437ba89d35bf482f3d9671bb99ef2fc69f5985.zip
Merge branch 'develop' into main
Diffstat (limited to 'test/utils/test_uri.py')
-rw-r--r--test/utils/test_uri.py189
1 files changed, 189 insertions, 0 deletions
diff --git a/test/utils/test_uri.py b/test/utils/test_uri.py
new file mode 100644
index 0000000..770e65a
--- /dev/null
+++ b/test/utils/test_uri.py
@@ -0,0 +1,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 ##