qutebrowser/tests/utils/test_urlutils.py

230 lines
7.1 KiB
Python
Raw Normal View History

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
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={}',
},
}
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.
"""
SPECIAL_URLS = (
2014-05-05 13:41:54 +02:00
'file:///tmp/foo',
'about:blank',
'qute:version'
)
2014-05-05 13:41:54 +02:00
NORMAL_URLS = (
2014-05-05 13:41:54 +02:00
'http://www.qutebrowser.org/',
'www.qutebrowser.org'
)
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
"""Test _get_search_url."""
2014-05-05 13:41:54 +02:00
2015-04-04 18:49:26 +02:00
@pytest.fixture(autouse=True)
def mock_config(self, config_stub, monkeypatch):
2015-04-05 20:30:31 +02:00
"""Fixture to patch urlutils.config with a stub."""
init_config_stub(config_stub)
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
def test_engine_pre(self):
2015-03-26 07:08:13 +01:00
"""Test search engine name with one word."""
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
def test_engine_pre_multiple_words(self):
2015-03-26 07:08:13 +01:00
"""Test search engine name with multiple words."""
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'
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."""
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
def test_engine_with_bang_pre(self):
2015-03-26 07:08:13 +01:00
"""Test search engine with a prepended !bang."""
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-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."""
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
"""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.
"""
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-05-05 13:41: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',
'31c3',
'http:foo:0',
'foo::bar',
)
2014-05-05 13:41:54 +02:00
2015-04-04 18:49:26 +02:00
@pytest.mark.parametrize('url', URLS)
def test_urls(self, monkeypatch, config_stub, url):
2014-05-27 13:06:13 +02:00
"""Test things which are URLs."""
init_config_stub(config_stub, 'naive')
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)
def test_not_urls(self, monkeypatch, config_stub, url):
2014-05-27 13:06:13 +02:00
"""Test things which are not URLs."""
init_config_stub(config_stub, 'naive')
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])
def test_search_autosearch(self, monkeypatch, config_stub, autosearch):
2015-03-26 07:08:13 +01:00
"""Test explicit search with auto-search=True."""
init_config_stub(config_stub, autosearch)
monkeypatch.setattr('qutebrowser.utils.urlutils.config', config_stub)
2015-04-04 18:49:26 +02:00
assert not urlutils.is_url('test foo')
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
"""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
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'
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'