From a730290d409b7e53770addcf0a1159a8ea736869 Mon Sep 17 00:00:00 2001 From: gammelon Date: Mon, 5 Mar 2018 16:32:41 +0100 Subject: [PATCH] Use QUrl for parsing, add tests --- qutebrowser/utils/urlutils.py | 6 ++++-- tests/unit/utils/test_urlutils.py | 36 +++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index 5f502e6fd..8bbf5b362 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -105,8 +105,10 @@ def _get_search_url(txt): if config.val.url.open_base_url and \ term in config.val.url.searchengines.keys(): - search_url = urllib.parse.urlparse(config.val.url.searchengines[term]) - url = QUrl('{}://{}'.format(search_url.scheme, search_url.netloc)) + url = qurl_from_user_input(config.val.url.searchengines[term]) + url.setPath(None) + url.setFragment(None) + url.setQuery(None) qtutils.ensure_valid(url) return url diff --git a/tests/unit/utils/test_urlutils.py b/tests/unit/utils/test_urlutils.py index 6621edfed..a96236f6e 100644 --- a/tests/unit/utils/test_urlutils.py +++ b/tests/unit/utils/test_urlutils.py @@ -277,18 +277,27 @@ class TestFuzzyUrl: def test_special_urls(url, special): assert urlutils.is_special_url(QUrl(url)) == special - -@pytest.mark.parametrize('url, host, query', [ - ('testfoo', 'www.example.com', 'q=testfoo'), - ('test testfoo', 'www.qutebrowser.org', 'q=testfoo'), - ('test testfoo bar foo', 'www.qutebrowser.org', 'q=testfoo bar foo'), - ('test testfoo ', 'www.qutebrowser.org', 'q=testfoo'), - ('!python testfoo', 'www.example.com', 'q=%21python testfoo'), - ('blub testfoo', 'www.example.com', 'q=blub testfoo'), - ('stripped ', 'www.example.com', 'q=stripped'), - ('test-with-dash testfoo', 'www.example.org', 'q=testfoo'), +@pytest.mark.parametrize('url, host, query, open_base_url', [ + ('testfoo', 'www.example.com', 'q=testfoo', False), + ('test testfoo', 'www.qutebrowser.org', 'q=testfoo', False), + ('test testfoo bar foo', 'www.qutebrowser.org', 'q=testfoo bar foo', False), + ('test testfoo ', 'www.qutebrowser.org', 'q=testfoo', False), + ('!python testfoo', 'www.example.com', 'q=%21python testfoo', False), + ('blub testfoo', 'www.example.com', 'q=blub testfoo', False), + ('stripped ', 'www.example.com', 'q=stripped', False), + ('test-with-dash testfoo', 'www.example.org', 'q=testfoo', False), + ('testfoo', 'www.example.com', 'q=testfoo', True), + ('test testfoo', 'www.qutebrowser.org', 'q=testfoo', True), + ('test testfoo bar foo', 'www.qutebrowser.org', 'q=testfoo bar foo', True), + ('test testfoo ', 'www.qutebrowser.org', 'q=testfoo', True), + ('!python testfoo', 'www.example.com', 'q=%21python testfoo', True), + ('blub testfoo', 'www.example.com', 'q=blub testfoo', True), + ('stripped ', 'www.example.com', 'q=stripped', True), + ('test-with-dash testfoo', 'www.example.org', 'q=testfoo', True), + ('test', 'www.qutebrowser.org', '', True), + ('test-with-dash', 'www.example.org', '', True), ]) -def test_get_search_url(url, host, query): +def test_get_search_url(config_stub, url, host, query, open_base_url): """Test _get_search_url(). Args: @@ -296,7 +305,12 @@ def test_get_search_url(url, host, query): host: The expected search machine host. query: The expected search query. """ + config_stub.val.url.open_base_url = open_base_url url = urlutils._get_search_url(url) + if open_base_url and query == '': + assert url.path() == '' + assert url.fragment() == '' + assert url.host() == host assert url.query() == query