# HG changeset patch # User Thomas De Schampheleire # Date 1537216437 -7200 # Node ID d8b23000aad675e4435352b4749774d90a0f7ca4 # Parent d63018164a308f1005c250a284279fe6f15cf8a3 tests: add basic tests for canonical_url diff -r d63018164a30 -r d8b23000aad6 kallithea/tests/other/test_libs.py --- 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