qutebrowser/tests/utils/test_urlutils.py
Martin Tournoij a346644c71 Changed:
- same_domain: If the tld is unknown, only return True if the hostnames are the same
- Fix when starting without an open page
2015-06-27 21:15:54 +02:00

262 lines
8.4 KiB
Python

# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2014-2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# 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/>.
# pylint: disable=protected-access
"""Tests for qutebrowser.utils.urlutils."""
from PyQt5.QtCore import QUrl
import pytest
from qutebrowser.utils import urlutils
def init_config_stub(stub, auto_search=True):
"""Initialize the given config_stub.
Args:
stub: The ConfigStub provided by the config_stub fixture.
auto_search: The value auto-search should have.
"""
stub.data = {
'general': {'auto-search': auto_search},
'searchengines': {
'test': 'http://www.qutebrowser.org/?q={}',
'DEFAULT': 'http://www.example.com/?q={}',
},
}
class TestSpecialURL:
"""Test is_special_url.
Attributes:
SPECIAL_URLS: URLs which are special.
NORMAL_URLS: URLs which are not special.
"""
SPECIAL_URLS = (
'file:///tmp/foo',
'about:blank',
'qute:version'
)
NORMAL_URLS = (
'http://www.qutebrowser.org/',
'www.qutebrowser.org'
)
@pytest.mark.parametrize('url', SPECIAL_URLS)
def test_special_urls(self, url):
"""Test special URLs."""
u = QUrl(url)
assert urlutils.is_special_url(u)
@pytest.mark.parametrize('url', NORMAL_URLS)
def test_normal_urls(self, url):
"""Test non-special URLs."""
u = QUrl(url)
assert not urlutils.is_special_url(u)
class TestSearchUrl:
"""Test _get_search_url."""
@pytest.fixture(autouse=True)
def mock_config(self, config_stub, monkeypatch):
"""Fixture to patch urlutils.config with a stub."""
init_config_stub(config_stub)
monkeypatch.setattr('qutebrowser.utils.urlutils.config', config_stub)
def test_default_engine(self):
"""Test default search engine."""
url = urlutils._get_search_url('testfoo')
assert url.host() == 'www.example.com'
assert url.query() == 'q=testfoo'
def test_engine_pre(self):
"""Test search engine name with one word."""
url = urlutils._get_search_url('test testfoo')
assert url.host() == 'www.qutebrowser.org'
assert url.query() == 'q=testfoo'
def test_engine_pre_multiple_words(self):
"""Test search engine name with multiple words."""
url = urlutils._get_search_url('test testfoo bar foo')
assert url.host() == 'www.qutebrowser.org'
assert url.query() == 'q=testfoo bar foo'
def test_engine_pre_whitespace_at_end(self):
"""Test search engine name with one word and whitespace."""
url = urlutils._get_search_url('test testfoo ')
assert url.host() == 'www.qutebrowser.org'
assert url.query() == 'q=testfoo'
def test_engine_with_bang_pre(self):
"""Test search engine with a prepended !bang."""
url = urlutils._get_search_url('!python testfoo')
assert url.host() == 'www.example.com'
assert url.query() == 'q=%21python testfoo'
def test_engine_wrong(self):
"""Test with wrong search engine."""
url = urlutils._get_search_url('blub testfoo')
assert url.host() == 'www.example.com'
assert url.query() == 'q=blub testfoo'
class TestIsUrl:
"""Tests for is_url.
Class attributes:
URLS: A list of strings which are URLs.
NOT_URLS: A list of strings which aren't URLs.
"""
URLS = (
'http://foobar',
'localhost:8080',
'qutebrowser.org',
' qutebrowser.org ',
'127.0.0.1',
'::1',
'2001:41d0:2:6c11::1',
'94.23.233.17',
'http://user:password@qutebrowser.org/foo?bar=baz#fish',
)
NOT_URLS = (
'foo bar',
'localhost test',
'another . test',
'foo',
'this is: not an URL',
'23.42',
'1337',
'deadbeef',
'31c3',
'http:foo:0',
'foo::bar',
)
@pytest.mark.parametrize('url', URLS)
def test_urls(self, monkeypatch, config_stub, url):
"""Test things which are URLs."""
init_config_stub(config_stub, 'naive')
monkeypatch.setattr('qutebrowser.utils.urlutils.config', config_stub)
assert urlutils.is_url(url), url
@pytest.mark.parametrize('url', NOT_URLS)
def test_not_urls(self, monkeypatch, config_stub, url):
"""Test things which are not URLs."""
init_config_stub(config_stub, 'naive')
monkeypatch.setattr('qutebrowser.utils.urlutils.config', config_stub)
assert not urlutils.is_url(url), url
@pytest.mark.parametrize('autosearch', [True, False])
def test_search_autosearch(self, monkeypatch, config_stub, autosearch):
"""Test explicit search with auto-search=True."""
init_config_stub(config_stub, autosearch)
monkeypatch.setattr('qutebrowser.utils.urlutils.config', config_stub)
assert not urlutils.is_url('test foo')
class TestQurlFromUserInput:
"""Tests for qurl_from_user_input."""
def test_url(self):
"""Test a normal URL."""
url = urlutils.qurl_from_user_input('qutebrowser.org')
assert url.toString() == 'http://qutebrowser.org'
def test_url_http(self):
"""Test a normal URL with http://."""
url = urlutils.qurl_from_user_input('http://qutebrowser.org')
assert url.toString() == 'http://qutebrowser.org'
def test_ipv6_bare(self):
"""Test an IPv6 without brackets."""
url = urlutils.qurl_from_user_input('::1/foo')
assert url.toString() == 'http://[::1]/foo'
def test_ipv6(self):
"""Test an IPv6 with brackets."""
url = urlutils.qurl_from_user_input('[::1]/foo')
assert url.toString() == 'http://[::1]/foo'
def test_ipv6_http(self):
"""Test an IPv6 with http:// and brackets."""
url = urlutils.qurl_from_user_input('http://[::1]')
assert url.toString() == 'http://[::1]'
class TestFilenameFromUrl:
"""Tests for filename_from_url."""
def test_invalid_url(self):
"""Test with an invalid QUrl."""
assert urlutils.filename_from_url(QUrl()) is None
def test_url_path(self):
"""Test with an URL with path."""
url = QUrl('http://qutebrowser.org/test.html')
assert urlutils.filename_from_url(url) == 'test.html'
def test_url_host(self):
"""Test with an URL with no path."""
url = QUrl('http://qutebrowser.org/')
assert urlutils.filename_from_url(url) == 'qutebrowser.org.html'
class TestSameDomain:
"""Tests for dame_domain."""
def test_same_domains(self):
"""Test for domains that should be considered the same."""
hosts = (
('http://example.com', 'http://www.example.com'),
('http://bbc.co.uk', 'https://www.bbc.co.uk'),
('http://many.levels.of.domains.example.com', 'http://www.example.com'),
('http://idn.иком.museum', 'http://idn2.иком.museum'),
('http://one.not_a_valid_tld', 'http://one.not_a_valid_tld'),
)
for host1, host2 in hosts:
assert urlutils.same_domain(QUrl(host1), QUrl(host2))
assert urlutils.same_domain(QUrl(host2), QUrl(host1))
def test_not_same_domains(self):
"""Test for domains that should be NOT considered the same."""
hosts = (
('http://bbc.co.uk', 'http://example.co.uk'),
('https://example.kids.museum', 'http://example.kunst.museum'),
('http://idn.иком.museum', 'http://idn.ירושלים.museum'),
('http://one.not_a_valid_tld', 'http://two.not_a_valid_tld'),
)
for host1, host2 in hosts:
assert not urlutils.same_domain(QUrl(host1), QUrl(host2))
assert not urlutils.same_domain(QUrl(host2), QUrl(host1))