Handle unknown filetypes with qute://help

This commit is contained in:
Florian Bruhin 2017-10-18 09:02:39 +02:00
parent 9dc9bcaf39
commit 354c3c8c9b
3 changed files with 30 additions and 3 deletions

View File

@ -35,6 +35,7 @@ Fixed
- More consistent sizing for favicons with vertical tabs. - More consistent sizing for favicons with vertical tabs.
- Using `:home` on pinned tabs is now prevented. - Using `:home` on pinned tabs is now prevented.
- Fix crash with unknown file types loaded via qute://help
Deprecated Deprecated
~~~~~~~~~~ ~~~~~~~~~~

View File

@ -29,8 +29,9 @@ import os
import time import time
import urllib.parse import urllib.parse
import textwrap import textwrap
import pkg_resources import mimetypes
import pkg_resources
from PyQt5.QtCore import QUrlQuery, QUrl from PyQt5.QtCore import QUrlQuery, QUrl
import qutebrowser import qutebrowser
@ -323,8 +324,10 @@ def qute_help(url):
"scripts/asciidoc2html.py.") "scripts/asciidoc2html.py.")
path = 'html/doc/{}'.format(urlpath) path = 'html/doc/{}'.format(urlpath)
if urlpath.endswith('.png'): if not urlpath.endswith('.html'):
return 'image/png', utils.read_file(path, binary=True) mimetype, _encoding = mimetypes.guess_type(urlpath)
assert mimetype is not None, url
return mimetype, utils.read_file(path, binary=True)
try: try:
data = utils.read_file(path) data = utils.read_file(path)

View File

@ -146,3 +146,26 @@ class TestHistoryHandler:
url = QUrl("qute://history/data?start_time={}".format(now)) url = QUrl("qute://history/data?start_time={}".format(now))
_mimetype, data = benchmark(qutescheme.qute_history, url) _mimetype, data = benchmark(qutescheme.qute_history, url)
assert len(json.loads(data)) > 1 assert len(json.loads(data)) > 1
class TestHelpHandler:
"""Tests for qute://help."""
@pytest.fixture
def data_patcher(self, monkeypatch):
def _patch(path, data):
def _read_file(name, binary=False):
assert path == name
if binary:
return data
return data.decode('utf-8')
monkeypatch.setattr(qutescheme.utils, 'read_file', _read_file)
return _patch
def test_unknown_file_type(self, data_patcher):
data_patcher('html/doc/foo.bin', b'\xff')
mimetype, data = qutescheme.qute_help(QUrl('qute://help/foo.bin'))
assert mimetype == 'application/octet-stream'
assert data == b'\xff'