Use QUrl for parsing, add tests

This commit is contained in:
gammelon 2018-03-05 16:32:41 +01:00
parent 16218a9900
commit a730290d40
2 changed files with 29 additions and 13 deletions

View File

@ -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

View File

@ -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