2014-06-19 09:04:37 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2015-01-03 15:51:31 +01:00
|
|
|
# Copyright 2014-2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2014-05-05 13:41:54 +02:00
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-08-04 03:47:09 +02:00
|
|
|
# pylint: disable=protected-access
|
|
|
|
|
2014-08-26 20:33:41 +02:00
|
|
|
"""Tests for qutebrowser.utils.urlutils."""
|
2014-05-05 13:41:54 +02:00
|
|
|
|
2014-06-20 20:21:52 +02:00
|
|
|
from PyQt5.QtCore import QUrl
|
2015-04-04 18:49:26 +02:00
|
|
|
import pytest
|
2014-06-20 20:21:52 +02:00
|
|
|
|
2014-08-26 20:33:41 +02:00
|
|
|
from qutebrowser.utils import urlutils
|
2014-05-05 13:41:54 +02:00
|
|
|
|
|
|
|
|
2015-05-07 07:58:22 +02:00
|
|
|
def init_config_stub(stub, auto_search=True):
|
|
|
|
"""Initialize the given config_stub.
|
2015-02-02 22:19:43 +01:00
|
|
|
|
|
|
|
Args:
|
2015-05-07 07:58:22 +02:00
|
|
|
stub: The ConfigStub provided by the config_stub fixture.
|
2015-02-02 22:19:43 +01:00
|
|
|
auto_search: The value auto-search should have.
|
|
|
|
"""
|
2015-05-07 07:58:22 +02:00
|
|
|
stub.data = {
|
2015-02-02 22:19:43 +01:00
|
|
|
'general': {'auto-search': auto_search},
|
|
|
|
'searchengines': {
|
|
|
|
'test': 'http://www.qutebrowser.org/?q={}',
|
|
|
|
'DEFAULT': 'http://www.example.com/?q={}',
|
|
|
|
},
|
|
|
|
}
|
2014-05-05 13:41:54 +02:00
|
|
|
|
2014-05-27 13:06:13 +02:00
|
|
|
|
2015-04-04 18:49:26 +02:00
|
|
|
class TestSpecialURL:
|
2015-04-05 20:30:31 +02:00
|
|
|
|
2014-05-27 13:06:13 +02:00
|
|
|
"""Test is_special_url.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
SPECIAL_URLS: URLs which are special.
|
|
|
|
NORMAL_URLS: URLs which are not special.
|
|
|
|
"""
|
|
|
|
|
2014-06-06 17:12:54 +02:00
|
|
|
SPECIAL_URLS = (
|
2014-05-05 13:41:54 +02:00
|
|
|
'file:///tmp/foo',
|
|
|
|
'about:blank',
|
|
|
|
'qute:version'
|
2014-06-06 17:12:54 +02:00
|
|
|
)
|
2014-05-05 13:41:54 +02:00
|
|
|
|
2014-06-06 17:12:54 +02:00
|
|
|
NORMAL_URLS = (
|
2014-05-05 13:41:54 +02:00
|
|
|
'http://www.qutebrowser.org/',
|
|
|
|
'www.qutebrowser.org'
|
2014-06-06 17:12:54 +02:00
|
|
|
)
|
2014-05-05 13:41:54 +02:00
|
|
|
|
2015-04-04 18:49:26 +02:00
|
|
|
@pytest.mark.parametrize('url', SPECIAL_URLS)
|
|
|
|
def test_special_urls(self, url):
|
2014-05-27 13:06:13 +02:00
|
|
|
"""Test special URLs."""
|
2015-04-04 18:49:26 +02:00
|
|
|
u = QUrl(url)
|
|
|
|
assert urlutils.is_special_url(u)
|
2014-05-05 13:41:54 +02:00
|
|
|
|
2015-04-04 18:49:26 +02:00
|
|
|
@pytest.mark.parametrize('url', NORMAL_URLS)
|
|
|
|
def test_normal_urls(self, url):
|
2014-05-27 13:06:13 +02:00
|
|
|
"""Test non-special URLs."""
|
2015-04-04 18:49:26 +02:00
|
|
|
u = QUrl(url)
|
|
|
|
assert not urlutils.is_special_url(u)
|
2014-05-05 13:41:54 +02:00
|
|
|
|
|
|
|
|
2015-04-04 18:49:26 +02:00
|
|
|
class TestSearchUrl:
|
2015-04-05 20:30:31 +02:00
|
|
|
|
2015-03-01 22:10:16 +01:00
|
|
|
"""Test _get_search_url."""
|
2014-05-05 13:41:54 +02:00
|
|
|
|
2015-04-04 18:49:26 +02:00
|
|
|
@pytest.fixture(autouse=True)
|
2015-05-18 23:32:01 +02:00
|
|
|
def mock_config(self, config_stub, monkeypatch):
|
2015-04-05 20:30:31 +02:00
|
|
|
"""Fixture to patch urlutils.config with a stub."""
|
2015-05-07 07:58:22 +02:00
|
|
|
init_config_stub(config_stub)
|
2015-05-18 23:32:01 +02:00
|
|
|
monkeypatch.setattr('qutebrowser.utils.urlutils.config', config_stub)
|
2015-04-04 18:49:26 +02:00
|
|
|
|
2014-05-05 13:41:54 +02:00
|
|
|
def test_default_engine(self):
|
2014-05-27 13:06:13 +02:00
|
|
|
"""Test default search engine."""
|
2014-05-05 13:41:54 +02:00
|
|
|
url = urlutils._get_search_url('testfoo')
|
2015-04-04 18:49:26 +02:00
|
|
|
assert url.host() == 'www.example.com'
|
|
|
|
assert url.query() == 'q=testfoo'
|
2014-05-05 13:41:54 +02:00
|
|
|
|
2014-11-27 21:45:04 +01:00
|
|
|
def test_engine_pre(self):
|
2015-03-26 07:08:13 +01:00
|
|
|
"""Test search engine name with one word."""
|
2014-11-27 21:45:04 +01:00
|
|
|
url = urlutils._get_search_url('test testfoo')
|
2015-04-04 18:49:26 +02:00
|
|
|
assert url.host() == 'www.qutebrowser.org'
|
|
|
|
assert url.query() == 'q=testfoo'
|
2014-05-05 13:41:54 +02:00
|
|
|
|
2014-11-27 22:40:10 +01:00
|
|
|
def test_engine_pre_multiple_words(self):
|
2015-03-26 07:08:13 +01:00
|
|
|
"""Test search engine name with multiple words."""
|
2014-11-27 22:40:10 +01:00
|
|
|
url = urlutils._get_search_url('test testfoo bar foo')
|
2015-04-04 18:49:26 +02:00
|
|
|
assert url.host() == 'www.qutebrowser.org'
|
|
|
|
assert url.query() == 'q=testfoo bar foo'
|
2014-11-27 22:40:10 +01:00
|
|
|
|
2014-11-27 21:45:04 +01:00
|
|
|
def test_engine_pre_whitespace_at_end(self):
|
2015-03-26 07:08:13 +01:00
|
|
|
"""Test search engine name with one word and whitespace."""
|
2014-11-27 21:45:04 +01:00
|
|
|
url = urlutils._get_search_url('test testfoo ')
|
2015-04-04 18:49:26 +02:00
|
|
|
assert url.host() == 'www.qutebrowser.org'
|
|
|
|
assert url.query() == 'q=testfoo'
|
2014-05-05 13:41:54 +02:00
|
|
|
|
2014-11-27 21:45:04 +01:00
|
|
|
def test_engine_with_bang_pre(self):
|
2015-03-26 07:08:13 +01:00
|
|
|
"""Test search engine with a prepended !bang."""
|
2014-11-27 21:45:04 +01:00
|
|
|
url = urlutils._get_search_url('!python testfoo')
|
2015-04-04 18:49:26 +02:00
|
|
|
assert url.host() == 'www.example.com'
|
|
|
|
assert url.query() == 'q=%21python testfoo'
|
2014-11-27 21:45:04 +01:00
|
|
|
|
2014-05-05 13:41:54 +02:00
|
|
|
def test_engine_wrong(self):
|
2014-05-27 13:06:13 +02:00
|
|
|
"""Test with wrong search engine."""
|
2014-11-27 21:45:04 +01:00
|
|
|
url = urlutils._get_search_url('blub testfoo')
|
2015-04-04 18:49:26 +02:00
|
|
|
assert url.host() == 'www.example.com'
|
|
|
|
assert url.query() == 'q=blub testfoo'
|
2014-05-05 13:41:54 +02:00
|
|
|
|
|
|
|
|
2015-04-04 18:49:26 +02:00
|
|
|
class TestIsUrl:
|
2015-04-05 20:30:31 +02:00
|
|
|
|
2014-08-12 07:09:40 +02:00
|
|
|
"""Tests for is_url.
|
2014-05-27 13:06:13 +02:00
|
|
|
|
|
|
|
Class attributes:
|
|
|
|
URLS: A list of strings which are URLs.
|
|
|
|
NOT_URLS: A list of strings which aren't URLs.
|
|
|
|
"""
|
|
|
|
|
2014-06-06 17:12:54 +02:00
|
|
|
URLS = (
|
2014-05-05 13:41:54 +02:00
|
|
|
'http://foobar',
|
|
|
|
'localhost:8080',
|
|
|
|
'qutebrowser.org',
|
2014-09-02 07:11:01 +02:00
|
|
|
' qutebrowser.org ',
|
2014-09-02 06:53:52 +02:00
|
|
|
'127.0.0.1',
|
|
|
|
'::1',
|
|
|
|
'2001:41d0:2:6c11::1',
|
|
|
|
'94.23.233.17',
|
2014-09-02 08:21:53 +02:00
|
|
|
'http://user:password@qutebrowser.org/foo?bar=baz#fish',
|
2014-06-06 17:12:54 +02:00
|
|
|
)
|
2014-05-05 13:41:54 +02:00
|
|
|
|
2014-06-06 17:12:54 +02:00
|
|
|
NOT_URLS = (
|
2014-05-05 13:41:54 +02:00
|
|
|
'foo bar',
|
|
|
|
'localhost test',
|
|
|
|
'another . test',
|
2014-05-06 14:25:11 +02:00
|
|
|
'foo',
|
2014-09-02 06:53:52 +02:00
|
|
|
'this is: not an URL',
|
|
|
|
'23.42',
|
|
|
|
'1337',
|
|
|
|
'deadbeef',
|
2014-12-22 18:32:58 +01:00
|
|
|
'31c3',
|
2015-01-09 20:10:39 +01:00
|
|
|
'http:foo:0',
|
2015-03-11 17:48:24 +01:00
|
|
|
'foo::bar',
|
2014-06-06 17:12:54 +02:00
|
|
|
)
|
2014-05-05 13:41:54 +02:00
|
|
|
|
2015-04-04 18:49:26 +02:00
|
|
|
@pytest.mark.parametrize('url', URLS)
|
2015-05-18 23:32:01 +02:00
|
|
|
def test_urls(self, monkeypatch, config_stub, url):
|
2014-05-27 13:06:13 +02:00
|
|
|
"""Test things which are URLs."""
|
2015-05-07 07:58:22 +02:00
|
|
|
init_config_stub(config_stub, 'naive')
|
2015-05-18 23:32:01 +02:00
|
|
|
monkeypatch.setattr('qutebrowser.utils.urlutils.config', config_stub)
|
2015-04-04 18:49:26 +02:00
|
|
|
assert urlutils.is_url(url), url
|
2014-05-05 13:41:54 +02:00
|
|
|
|
2015-04-04 18:49:26 +02:00
|
|
|
@pytest.mark.parametrize('url', NOT_URLS)
|
2015-05-18 23:32:01 +02:00
|
|
|
def test_not_urls(self, monkeypatch, config_stub, url):
|
2014-05-27 13:06:13 +02:00
|
|
|
"""Test things which are not URLs."""
|
2015-05-07 07:58:22 +02:00
|
|
|
init_config_stub(config_stub, 'naive')
|
2015-05-18 23:32:01 +02:00
|
|
|
monkeypatch.setattr('qutebrowser.utils.urlutils.config', config_stub)
|
2015-04-04 18:49:26 +02:00
|
|
|
assert not urlutils.is_url(url), url
|
2014-05-05 13:41:54 +02:00
|
|
|
|
2015-04-04 18:49:26 +02:00
|
|
|
@pytest.mark.parametrize('autosearch', [True, False])
|
2015-05-18 23:32:01 +02:00
|
|
|
def test_search_autosearch(self, monkeypatch, config_stub, autosearch):
|
2015-03-26 07:08:13 +01:00
|
|
|
"""Test explicit search with auto-search=True."""
|
2015-05-07 07:58:22 +02:00
|
|
|
init_config_stub(config_stub, autosearch)
|
2015-05-18 23:32:01 +02:00
|
|
|
monkeypatch.setattr('qutebrowser.utils.urlutils.config', config_stub)
|
2015-04-04 18:49:26 +02:00
|
|
|
assert not urlutils.is_url('test foo')
|
2015-02-02 22:19:43 +01:00
|
|
|
|
2014-05-05 13:41:54 +02:00
|
|
|
|
2015-04-04 18:49:26 +02:00
|
|
|
class TestQurlFromUserInput:
|
2015-04-05 20:30:31 +02:00
|
|
|
|
2014-09-02 08:20:33 +02:00
|
|
|
"""Tests for qurl_from_user_input."""
|
|
|
|
|
|
|
|
def test_url(self):
|
|
|
|
"""Test a normal URL."""
|
2015-04-05 20:30:31 +02:00
|
|
|
url = urlutils.qurl_from_user_input('qutebrowser.org')
|
|
|
|
assert url.toString() == 'http://qutebrowser.org'
|
2014-09-02 08:20:33 +02:00
|
|
|
|
|
|
|
def test_url_http(self):
|
|
|
|
"""Test a normal URL with http://."""
|
2015-04-05 20:30:31 +02:00
|
|
|
url = urlutils.qurl_from_user_input('http://qutebrowser.org')
|
|
|
|
assert url.toString() == 'http://qutebrowser.org'
|
2014-09-02 08:20:33 +02:00
|
|
|
|
|
|
|
def test_ipv6_bare(self):
|
|
|
|
"""Test an IPv6 without brackets."""
|
2015-04-05 20:30:31 +02:00
|
|
|
url = urlutils.qurl_from_user_input('::1/foo')
|
|
|
|
assert url.toString() == 'http://[::1]/foo'
|
2014-09-02 08:20:33 +02:00
|
|
|
|
|
|
|
def test_ipv6(self):
|
|
|
|
"""Test an IPv6 with brackets."""
|
2015-04-05 20:30:31 +02:00
|
|
|
url = urlutils.qurl_from_user_input('[::1]/foo')
|
|
|
|
assert url.toString() == 'http://[::1]/foo'
|
2014-09-02 08:20:33 +02:00
|
|
|
|
|
|
|
def test_ipv6_http(self):
|
|
|
|
"""Test an IPv6 with http:// and brackets."""
|
2015-04-05 20:30:31 +02:00
|
|
|
url = urlutils.qurl_from_user_input('http://[::1]')
|
|
|
|
assert url.toString() == 'http://[::1]'
|
2014-09-02 08:20:33 +02:00
|
|
|
|
|
|
|
|
2015-04-04 18:49:26 +02:00
|
|
|
class TestFilenameFromUrl:
|
2015-04-05 20:30:31 +02:00
|
|
|
|
2014-11-30 18:07:44 +01:00
|
|
|
"""Tests for filename_from_url."""
|
|
|
|
|
|
|
|
def test_invalid_url(self):
|
|
|
|
"""Test with an invalid QUrl."""
|
2015-04-05 20:30:31 +02:00
|
|
|
assert urlutils.filename_from_url(QUrl()) is None
|
2014-11-30 18:07:44 +01:00
|
|
|
|
|
|
|
def test_url_path(self):
|
|
|
|
"""Test with an URL with path."""
|
|
|
|
url = QUrl('http://qutebrowser.org/test.html')
|
2015-04-04 18:49:26 +02:00
|
|
|
assert urlutils.filename_from_url(url) == 'test.html'
|
2014-11-30 18:07:44 +01:00
|
|
|
|
|
|
|
def test_url_host(self):
|
|
|
|
"""Test with an URL with no path."""
|
|
|
|
url = QUrl('http://qutebrowser.org/')
|
2015-04-04 18:49:26 +02:00
|
|
|
assert urlutils.filename_from_url(url) == 'qutebrowser.org.html'
|