2015-09-17 18:59:00 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2017-05-09 21:37:03 +02:00
|
|
|
# Copyright 2015-2017 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2015-09-17 18:59:00 +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/>.
|
|
|
|
|
2017-09-19 10:35:54 +02:00
|
|
|
"""Test the server webserver used for tests."""
|
2015-09-17 18:59:00 +02:00
|
|
|
|
2015-11-26 14:37:47 +01:00
|
|
|
import json
|
2015-09-17 06:53:28 +02:00
|
|
|
import urllib.request
|
2015-09-17 07:33:52 +02:00
|
|
|
import urllib.error
|
2015-09-17 06:53:28 +02:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('path, content, expected', [
|
2017-09-19 10:35:54 +02:00
|
|
|
('/', 'qutebrowser test webserver', True),
|
|
|
|
# https://github.com/Runscope/server/issues/245
|
2015-09-17 06:53:28 +02:00
|
|
|
('/', 'www.google-analytics.com', False),
|
2015-09-17 07:45:12 +02:00
|
|
|
('/data/hello.txt', 'Hello World!', True),
|
2015-09-17 06:53:28 +02:00
|
|
|
])
|
2017-09-19 10:35:54 +02:00
|
|
|
def test_server(server, qtbot, path, content, expected):
|
|
|
|
with qtbot.waitSignal(server.new_request, timeout=100):
|
|
|
|
url = 'http://localhost:{}{}'.format(server.port, path)
|
2015-09-17 07:33:52 +02:00
|
|
|
try:
|
|
|
|
response = urllib.request.urlopen(url)
|
|
|
|
except urllib.error.HTTPError as e:
|
|
|
|
# "Though being an exception (a subclass of URLError), an HTTPError
|
|
|
|
# can also function as a non-exceptional file-like return value
|
|
|
|
# (the same thing that urlopen() returns)."
|
|
|
|
# ...wat
|
|
|
|
print(e.read().decode('utf-8'))
|
|
|
|
raise
|
2015-09-17 06:53:28 +02:00
|
|
|
|
|
|
|
data = response.read().decode('utf-8')
|
|
|
|
|
2017-09-19 10:35:54 +02:00
|
|
|
assert server.get_requests() == [server.ExpectedRequest('GET', path)]
|
2015-09-17 06:53:28 +02:00
|
|
|
assert (content in data) == expected
|
2015-11-16 23:14:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('line, verb, path, equal', [
|
2016-01-21 18:07:56 +01:00
|
|
|
({'verb': 'GET', 'path': '/', 'status': 200}, 'GET', '/', True),
|
|
|
|
({'verb': 'GET', 'path': '/foo/', 'status': 200}, 'GET', '/foo', True),
|
2015-11-25 10:36:27 +01:00
|
|
|
|
2016-01-21 18:07:56 +01:00
|
|
|
({'verb': 'GET', 'path': '/', 'status': 200}, 'GET', '/foo', False),
|
|
|
|
({'verb': 'POST', 'path': '/', 'status': 200}, 'GET', '/', False),
|
2015-11-16 23:14:24 +01:00
|
|
|
])
|
2017-09-19 10:35:54 +02:00
|
|
|
def test_expected_request(server, line, verb, path, equal):
|
|
|
|
expected = server.ExpectedRequest(verb, path)
|
|
|
|
request = server.Request(json.dumps(line))
|
2015-11-16 23:14:24 +01:00
|
|
|
assert (expected == request) == equal
|