2015-09-17 18:59:00 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
|
|
|
# Copyright 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/>.
|
|
|
|
|
|
|
|
"""Things needed for integration testing."""
|
|
|
|
|
2015-09-17 06:53:28 +02:00
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import socket
|
|
|
|
import os.path
|
2015-09-17 08:09:21 +02:00
|
|
|
import collections
|
2015-09-17 06:53:28 +02:00
|
|
|
|
|
|
|
import pytest
|
2015-09-17 18:59:00 +02:00
|
|
|
import pytestqt.plugin # pylint: disable=import-error
|
2015-09-17 06:53:28 +02:00
|
|
|
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QProcess, QObject
|
|
|
|
|
|
|
|
|
2015-09-17 08:09:21 +02:00
|
|
|
Request = collections.namedtuple('Request', 'verb, url')
|
|
|
|
|
|
|
|
|
2015-09-17 06:53:28 +02:00
|
|
|
class InvalidLine(Exception):
|
|
|
|
|
|
|
|
"""Exception raised when HTTPBin prints a line which is not parsable."""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class HTTPBin(QObject):
|
|
|
|
|
2015-09-17 18:59:00 +02:00
|
|
|
"""Abstraction over a running HTTPbin server process.
|
|
|
|
|
|
|
|
Reads the log from its stdout and parses it.
|
|
|
|
|
|
|
|
Class attributes:
|
|
|
|
LOG_RE: Used to parse the CLF log which httpbin outputs.
|
|
|
|
|
|
|
|
Signals:
|
|
|
|
ready: Emitted when the server finished starting up.
|
|
|
|
new_request: Emitted when there's a new request received.
|
|
|
|
"""
|
|
|
|
|
2015-09-17 06:53:28 +02:00
|
|
|
ready = pyqtSignal()
|
2015-09-17 08:09:21 +02:00
|
|
|
new_request = pyqtSignal(Request)
|
|
|
|
|
|
|
|
LOG_RE = re.compile(r"""
|
|
|
|
(?P<host>[^ ]*)
|
|
|
|
\ ([^ ]*) # ignored
|
|
|
|
\ (?P<user>[^ ]*)
|
|
|
|
\ \[(?P<date>[^]]*)\]
|
|
|
|
\ "(?P<request>
|
|
|
|
(?P<verb>[^ ]*)
|
|
|
|
\ (?P<url>[^ ]*)
|
|
|
|
\ (?P<protocol>[^ ]*)
|
|
|
|
)"
|
|
|
|
\ (?P<status>[^ ]*)
|
|
|
|
\ (?P<size>[^ ]*)
|
|
|
|
""", re.VERBOSE)
|
2015-09-17 06:53:28 +02:00
|
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent)
|
|
|
|
self._invalid = False
|
2015-09-17 08:09:21 +02:00
|
|
|
self._requests = []
|
2015-09-17 06:53:28 +02:00
|
|
|
self.port = self._get_port()
|
|
|
|
self.proc = QProcess()
|
|
|
|
self.proc.setReadChannel(QProcess.StandardError)
|
|
|
|
|
|
|
|
def _get_port(self):
|
2015-09-17 18:59:00 +02:00
|
|
|
"""Get a random free port to use for the server."""
|
2015-09-17 06:53:28 +02:00
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
sock.bind(('localhost', 0))
|
|
|
|
port = sock.getsockname()[1]
|
|
|
|
sock.close()
|
|
|
|
return port
|
|
|
|
|
2015-09-17 08:09:21 +02:00
|
|
|
def get_requests(self):
|
2015-09-17 18:59:00 +02:00
|
|
|
"""Get the requests to the server during this test.
|
|
|
|
|
|
|
|
Also waits for 0.5s to make sure any new requests are received.
|
|
|
|
"""
|
2015-09-17 06:53:28 +02:00
|
|
|
self.proc.waitForReadyRead(500)
|
2015-09-17 08:09:21 +02:00
|
|
|
self.read_log()
|
|
|
|
return self._requests
|
2015-09-17 06:53:28 +02:00
|
|
|
|
|
|
|
@pyqtSlot()
|
2015-09-17 08:09:21 +02:00
|
|
|
def read_log(self):
|
2015-09-17 18:59:00 +02:00
|
|
|
"""Read the log from httpbin's stdout and parse it."""
|
2015-09-17 06:53:28 +02:00
|
|
|
while self.proc.canReadLine():
|
|
|
|
line = self.proc.readLine()
|
|
|
|
line = bytes(line).decode('utf-8').rstrip('\n')
|
|
|
|
print(line)
|
|
|
|
|
|
|
|
if line == (' * Running on http://127.0.0.1:{}/ (Press CTRL+C to '
|
|
|
|
'quit)'.format(self.port)):
|
|
|
|
self.ready.emit()
|
|
|
|
continue
|
|
|
|
|
2015-09-17 08:09:21 +02:00
|
|
|
match = self.LOG_RE.match(line)
|
2015-09-17 06:53:28 +02:00
|
|
|
if match is None:
|
|
|
|
self._invalid = True
|
|
|
|
print("INVALID: {}".format(line))
|
|
|
|
continue
|
2015-09-17 08:09:21 +02:00
|
|
|
|
|
|
|
# FIXME do we need to allow other options?
|
|
|
|
assert match.group('protocol') == 'HTTP/1.1'
|
|
|
|
|
|
|
|
request = Request(verb=match.group('verb'), url=match.group('url'))
|
|
|
|
print(request)
|
|
|
|
self._requests.append(request)
|
|
|
|
self.new_request.emit(request)
|
2015-09-17 06:53:28 +02:00
|
|
|
|
|
|
|
def start(self):
|
2015-09-17 18:59:00 +02:00
|
|
|
"""Start the webserver."""
|
2015-09-17 06:53:28 +02:00
|
|
|
filename = os.path.join(os.path.dirname(__file__), 'webserver.py')
|
|
|
|
self.proc.start(sys.executable, [filename, str(self.port)])
|
|
|
|
ok = self.proc.waitForStarted()
|
|
|
|
assert ok
|
2015-09-17 08:09:21 +02:00
|
|
|
self.proc.readyRead.connect(self.read_log)
|
2015-09-17 06:53:28 +02:00
|
|
|
|
|
|
|
def after_test(self):
|
2015-09-17 18:59:00 +02:00
|
|
|
"""Clean request list after each test.
|
|
|
|
|
|
|
|
Also checks self._invalid so the test counts as failed if there were
|
|
|
|
unexpected output lines earlier.
|
|
|
|
"""
|
2015-09-17 08:09:21 +02:00
|
|
|
self._requests.clear()
|
2015-09-17 06:53:28 +02:00
|
|
|
if self._invalid:
|
|
|
|
raise InvalidLine
|
|
|
|
|
|
|
|
def cleanup(self):
|
2015-09-17 18:59:00 +02:00
|
|
|
"""Clean up and shut down the process."""
|
2015-09-17 06:53:28 +02:00
|
|
|
self.proc.terminate()
|
|
|
|
self.proc.waitForFinished()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.yield_fixture(scope='session', autouse=True)
|
|
|
|
def httpbin(qapp):
|
2015-09-17 18:59:00 +02:00
|
|
|
"""Fixture for a httpbin object which ensures clean setup/teardown."""
|
2015-09-17 06:53:28 +02:00
|
|
|
httpbin = HTTPBin()
|
|
|
|
|
|
|
|
blocker = pytestqt.plugin.SignalBlocker(timeout=5000, raising=True)
|
|
|
|
blocker.connect(httpbin.ready)
|
|
|
|
with blocker:
|
|
|
|
httpbin.start()
|
|
|
|
|
|
|
|
yield httpbin
|
|
|
|
|
|
|
|
httpbin.cleanup()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.yield_fixture(autouse=True)
|
|
|
|
def httpbin_clean(httpbin):
|
2015-09-17 18:59:00 +02:00
|
|
|
"""Fixture to clean httpbin request list after each test."""
|
2015-09-17 06:53:28 +02:00
|
|
|
yield
|
|
|
|
httpbin.after_test()
|