qutebrowser/qutebrowser/test/utils/test_url.py

148 lines
3.9 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:
2014-05-05 13:41:54 +02:00
# Copyright 2014 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/>.
2014-08-04 03:47:09 +02:00
# pylint: disable=protected-access
2014-05-05 13:41:54 +02:00
"""Tests for qutebrowser.utils.url."""
import unittest
2014-06-20 20:21:52 +02:00
from PyQt5.QtCore import QUrl
2014-05-05 13:41:54 +02:00
import qutebrowser.utils.url as urlutils
2014-06-23 19:44:21 +02:00
from qutebrowser.test.stubs import ConfigStub
2014-05-05 13:41:54 +02:00
2014-06-23 19:44:21 +02:00
CONFIG = {
'general': {'auto-search': True},
'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
2014-06-23 19:44:21 +02:00
class SpecialURLTests(unittest.TestCase):
2014-05-05 13:41:54 +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
def test_special_urls(self):
2014-05-27 13:06:13 +02:00
"""Test special URLs."""
2014-05-05 13:41:54 +02:00
for url in self.SPECIAL_URLS:
2014-06-20 20:21:52 +02:00
u = QUrl(url)
self.assertTrue(urlutils.is_special_url(u))
2014-05-05 13:41:54 +02:00
def test_normal_urls(self):
2014-05-27 13:06:13 +02:00
"""Test non-special URLs."""
2014-05-05 13:41:54 +02:00
for url in self.NORMAL_URLS:
2014-06-20 20:21:52 +02:00
u = QUrl(url)
self.assertFalse(urlutils.is_special_url(u))
2014-05-05 13:41:54 +02:00
2014-06-23 19:44:21 +02:00
class SearchUrlTests(unittest.TestCase):
2014-05-05 13:41:54 +02:00
2014-05-27 13:06:13 +02:00
"""Test _get_search_url.
Attributes:
config: The urlutils.config instance.
"""
2014-05-05 13:41:54 +02:00
def setUp(self):
self.config = urlutils.config
2014-06-23 19:44:21 +02:00
urlutils.config = ConfigStub(CONFIG)
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')
self.assertEqual(url.host(), 'www.example.com')
self.assertEqual(url.query(), 'q=testfoo')
def test_engine_post(self):
2014-05-27 13:06:13 +02:00
"""Test search engine with an appended !hasbang."""
2014-05-05 13:41:54 +02:00
url = urlutils._get_search_url('testfoo !test')
self.assertEqual(url.host(), 'www.qutebrowser.org')
self.assertEqual(url.query(), 'q=testfoo')
def test_engine_pre(self):
2014-05-27 13:06:13 +02:00
"""Test search engine with a prepended !hasbang."""
2014-05-05 13:41:54 +02:00
url = urlutils._get_search_url('!test testfoo')
self.assertEqual(url.host(), 'www.qutebrowser.org')
self.assertEqual(url.query(), 'q=testfoo')
def test_engine_wrong(self):
2014-05-27 13:06:13 +02:00
"""Test with wrong search engine."""
2014-06-20 23:57:52 +02:00
with self.assertRaises(urlutils.FuzzyUrlError):
2014-05-05 13:41:54 +02:00
_ = urlutils._get_search_url('!blub testfoo')
def tearDown(self):
urlutils.config = self.config
2014-06-23 19:44:21 +02:00
class IsUrlNaiveTests(unittest.TestCase):
2014-05-05 13:41:54 +02:00
2014-05-27 13:06:13 +02:00
"""Tests for _is_url_naive.
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-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-05-05 13:41:54 +02:00
def test_urls(self):
2014-05-27 13:06:13 +02:00
"""Test things which are URLs."""
2014-05-05 13:41:54 +02:00
for url in self.URLS:
self.assertTrue(urlutils._is_url_naive(url), url)
def test_not_urls(self):
2014-05-27 13:06:13 +02:00
"""Test things which are not URLs."""
2014-05-05 13:41:54 +02:00
for url in self.NOT_URLS:
self.assertFalse(urlutils._is_url_naive(url), url)
if __name__ == '__main__':
unittest.main()