tests: Add some code for MHTML integration tests.

This commit is contained in:
Florian Bruhin 2015-11-11 07:48:36 +01:00
parent 7cddd52b2d
commit 6bd45bbf24
4 changed files with 134 additions and 0 deletions

View File

@ -0,0 +1 @@
simple.html

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Simple MHTML test</title>
</head>
<body>
<a href="/">normal link to another page</a>
</body>
</html>

View File

@ -0,0 +1,20 @@
Content-Type: multipart/related; boundary="---=_qute-6d584056-b1e4-4882-91e6-d4a6d23adb67"
MIME-Version: 1.0
-----=_qute-6d584056-b1e4-4882-91e6-d4a6d23adb67
Content-Location: http://localhost:1234/data/downloads/mhtml/simple/simple.html
MIME-Version: 1.0
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE=20html><html><head>
=20=20=20=20=20=20=20=20<meta=20charset=3D"utf-8">
=20=20=20=20=20=20=20=20<title>Simple=20MHTML=20test</title>
=20=20=20=20</head>
=20=20=20=20<body>
=20=20=20=20=20=20=20=20<a=20href=3D"/">normal=20link=20to=20another=20page=
</a>
=20=20=20=20
</body></html>
-----=_qute-6d584056-b1e4-4882-91e6-d4a6d23adb67--

View File

@ -0,0 +1,103 @@
# 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/>.
"""Test mhtml downloads based on sample files."""
import os
import re
import os.path
import pytest
def collect_tests():
basedir = os.path.dirname(__file__)
datadir = os.path.join(basedir, 'data', 'downloads', 'mhtml')
files = os.listdir(datadir)
return files
def normalize_line(line):
line = line.rstrip('\n')
line = re.sub('boundary="---=_qute-[0-9a-f-]+"',
'boundary="---=_qute-UUID"', line)
line = re.sub('^-----=_qute-[0-9a-f-]+$', '-----=_qute-UUID', line)
line = re.sub(r'localhost:\d{1,5}', 'localhost:(port)', line)
return line
class DownloadDir:
"""Abstraction over a download directory."""
def __init__(self, tmpdir):
self._tmpdir = tmpdir
self.location = str(tmpdir)
def read_file(self):
files = self._tmpdir.listdir()
assert len(files) == 1
with open(str(files[0]), 'r', encoding='utf-8') as f:
return f.readlines()
def compare_mhtml(self, filename):
with open(filename, 'r', encoding='utf-8') as f:
expected_data = [normalize_line(line) for line in f]
actual_data = self.read_file()
actual_data = [normalize_line(line) for line in actual_data]
assert actual_data == expected_data
@pytest.fixture
def download_dir(tmpdir):
return DownloadDir(tmpdir)
@pytest.mark.parametrize('test_name', collect_tests())
def test_mhtml(test_name, download_dir, quteproc, httpbin):
quteproc.set_setting('storage', 'download-directory',
download_dir.location)
quteproc.set_setting('storage', 'prompt-download-directory', 'false')
test_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)),
'data', 'downloads', 'mhtml', test_name)
test_path = 'data/downloads/mhtml/{}'.format(test_name)
quteproc.open_path('{}/{}.html'.format(test_path, test_name))
download_dest = os.path.join(download_dir.location,
'{}-downloaded.mht'.format(test_name))
quteproc.send_cmd(':download --mhtml --dest "{}"'.format(download_dest))
quteproc.wait_for(category='downloads', module='mhtml',
function='finish_file',
message='All assets downloaded, ready to finish off!')
expected_file = os.path.join(test_dir, '{}.mht'.format(test_name))
download_dir.compare_mhtml(expected_file)
with open(os.path.join(test_dir, 'requests'), encoding='utf-8') as f:
expected_requests = []
for line in f:
if line.startswith('#'):
continue
path = '/{}/{}'.format(test_path, line.strip())
expected_requests.append(httpbin.Request('GET', path))
actual_requests = httpbin.get_requests()
assert sorted(actual_requests) == sorted(expected_requests)