changeset 7374:d8b23000aad6

tests: add basic tests for canonical_url
author Thomas De Schampheleire <thomas.de_schampheleire@nokia.com>
date Mon, 17 Sep 2018 22:33:57 +0200
parents d63018164a30
children 3d39e68ff5bc
files kallithea/tests/other/test_libs.py
diffstat 1 files changed, 48 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/tests/other/test_libs.py	Sat Sep 01 16:14:30 2018 +0200
+++ b/kallithea/tests/other/test_libs.py	Mon Sep 17 22:33:57 2018 +0200
@@ -551,3 +551,51 @@
         from kallithea.lib.utils import _extract_id_from_repo_name
         _test = _extract_id_from_repo_name(test)
         assert _test == expected, 'url:%s, got:`%s` expected: `%s`' % (test, _test, expected)
+
+
+    @parametrize('canonical,test,expected', [
+        ('http://www.example.org/', '/abc/xyz', 'http://www.example.org/abc/xyz'),
+        ('http://www.example.org', '/abc/xyz', 'http://www.example.org/abc/xyz'),
+        ('http://www.example.org', '/abc/xyz/', 'http://www.example.org/abc/xyz/'),
+        ('http://www.example.org', 'abc/xyz/', 'http://www.example.org/abc/xyz/'),
+        ('http://www.example.org', 'about', 'http://www.example.org/about-page'),
+    ])
+    def test_canonical_url(self, canonical, test, expected):
+        from kallithea.lib.helpers import canonical_url
+        from tg import request
+
+        # setup url(), used by canonical_url
+        import routes
+        m = routes.Mapper()
+        m.connect('about', '/about-page')
+        url = routes.URLGenerator(m, {'HTTP_HOST': 'http_host.example.org'})
+
+        config_mock = {
+            'canonical_url': canonical,
+        }
+
+        with test_context(self.app):
+            request.environ['routes.url'] = url
+            with mock.patch('kallithea.CONFIG', config_mock):
+                assert canonical_url(test) == expected
+
+    @parametrize('canonical,expected', [
+        ('http://www.example.org', 'www.example.org'),
+    ])
+    def test_canonical_hostname(self, canonical, expected):
+        from kallithea.lib.helpers import canonical_hostname
+        from tg import request
+
+        # setup url(), used by canonical_hostname
+        import routes
+        m = routes.Mapper()
+        url = routes.URLGenerator(m, {'HTTP_HOST': 'http_host.example.org'})
+
+        config_mock = {
+            'canonical_url': canonical,
+        }
+
+        with test_context(self.app):
+            request.environ['routes.url'] = url
+            with mock.patch('kallithea.CONFIG', config_mock):
+                assert canonical_hostname() == expected