diff --git a/qutebrowser/browser/qutescheme.py b/qutebrowser/browser/qutescheme.py index 35bd2a436..1679df877 100644 --- a/qutebrowser/browser/qutescheme.py +++ b/qutebrowser/browser/qutescheme.py @@ -44,6 +44,7 @@ import pkg_resources from PyQt5.QtCore import QUrlQuery, QUrl import qutebrowser +from qutebrowser.browser import pdfjs from qutebrowser.config import config, configdata, configexc, configdiff from qutebrowser.utils import (version, utils, jinja, log, message, docutils, objreg, urlutils) @@ -531,3 +532,21 @@ def qute_pastebin_version(_url): """Handler that pastebins the version string.""" version.pastebin_version() return 'text/plain', b'Paste called.' + + +@add_handler('pdfjs') +def qute_pdfjs(url): + """Handler for qute://pdfjs. Return the pdf.js viewer.""" + try: + data = pdfjs.get_pdfjs_res(url.path()) + except pdfjs.PDFJSNotFound as e: + # Logging as the error might get lost otherwise since we're not showing + # the error page if a single asset is missing. This way we don't lose + # information, as the failed pdfjs requests are still in the log. + log.misc.warning( + "pdfjs resource requested but not found: {}".format(e.path)) + raise NotFoundError("Can't find pdfjs resource '{}'".format(e.path)) + else: + mimetype, _encoding = mimetypes.guess_type(url.fileName()) + assert mimetype is not None, url + return mimetype, data diff --git a/qutebrowser/browser/webkit/network/webkitqutescheme.py b/qutebrowser/browser/webkit/network/webkitqutescheme.py index 1c3bfff82..18473addf 100644 --- a/qutebrowser/browser/webkit/network/webkitqutescheme.py +++ b/qutebrowser/browser/webkit/network/webkitqutescheme.py @@ -24,7 +24,7 @@ import mimetypes from PyQt5.QtCore import QUrl from PyQt5.QtNetwork import QNetworkReply, QNetworkAccessManager -from qutebrowser.browser import pdfjs, qutescheme +from qutebrowser.browser import qutescheme from qutebrowser.browser.webkit.network import networkreply from qutebrowser.utils import log, usertypes, qtutils @@ -81,22 +81,3 @@ def handler(request, operation, current_url): return networkreply.RedirectNetworkReply(e.url) return networkreply.FixedDataNetworkReply(request, data, mimetype) - - -@qutescheme.add_handler('pdfjs', backend=usertypes.Backend.QtWebKit) -def qute_pdfjs(url): - """Handler for qute://pdfjs. Return the pdf.js viewer.""" - try: - data = pdfjs.get_pdfjs_res(url.path()) - except pdfjs.PDFJSNotFound as e: - # Logging as the error might get lost otherwise since we're not showing - # the error page if a single asset is missing. This way we don't lose - # information, as the failed pdfjs requests are still in the log. - log.misc.warning( - "pdfjs resource requested but not found: {}".format(e.path)) - raise qutescheme.NotFoundError("Can't find pdfjs resource '{}'".format( - e.path)) - else: - mimetype, _encoding = mimetypes.guess_type(url.fileName()) - assert mimetype is not None, url - return mimetype, data diff --git a/tests/unit/browser/test_qutescheme.py b/tests/unit/browser/test_qutescheme.py index fbe9287ac..9a21efc50 100644 --- a/tests/unit/browser/test_qutescheme.py +++ b/tests/unit/browser/test_qutescheme.py @@ -20,11 +20,12 @@ import json import os import time +import logging from PyQt5.QtCore import QUrl import pytest -from qutebrowser.browser import qutescheme +from qutebrowser.browser import qutescheme, pdfjs class TestJavascriptHandler: @@ -169,3 +170,37 @@ class TestHelpHandler: mimetype, data = qutescheme.qute_help(QUrl('qute://help/foo.bin')) assert mimetype == 'application/octet-stream' assert data == b'\xff' + + +class TestPDFJSHandler: + + """Test the qute://pdfjs endpoint.""" + + @pytest.fixture(autouse=True) + def fake_pdfjs(self, monkeypatch): + def get_pdfjs_res(path): + if path == '/existing/file.html': + return b'foobar' + raise pdfjs.PDFJSNotFound(path) + + monkeypatch.setattr(pdfjs, 'get_pdfjs_res', get_pdfjs_res) + + @pytest.fixture(autouse=True) + def patch_backend(self, monkeypatch): + monkeypatch.setattr(qutescheme.objects, 'backend', + usertypes.Backend.QtWebKit) + + def test_existing_resource(self): + """Test with a resource that exists.""" + _mimetype, data = qutescheme.data_for_url( + QUrl('qute://pdfjs/existing/file.html')) + assert data == b'foobar' + + def test_nonexisting_resource(self, caplog): + """Test with a resource that does not exist.""" + with caplog.at_level(logging.WARNING, 'misc'): + with pytest.raises(qutescheme.NotFoundError): + qutescheme.data_for_url(QUrl('qute://pdfjs/no/file.html')) + assert len(caplog.records) == 1 + assert (caplog.records[0].message == + 'pdfjs resource requested but not found: /no/file.html') diff --git a/tests/unit/browser/webkit/network/test_webkitqutescheme.py b/tests/unit/browser/webkit/network/test_webkitqutescheme.py deleted file mode 100644 index 05190d230..000000000 --- a/tests/unit/browser/webkit/network/test_webkitqutescheme.py +++ /dev/null @@ -1,63 +0,0 @@ -# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: - -# Copyright 2016-2018 Daniel Schadt -# Copyright 2016-2018 Florian Bruhin (The Compiler) -# -# 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 . - -import logging - -import pytest -from PyQt5.QtCore import QUrl - -from qutebrowser.utils import usertypes -from qutebrowser.browser import pdfjs, qutescheme -# pylint: disable=unused-import -from qutebrowser.browser.webkit.network import webkitqutescheme -# pylint: enable=unused-import - - -class TestPDFJSHandler: - """Test the qute://pdfjs endpoint.""" - - @pytest.fixture(autouse=True) - def fake_pdfjs(self, monkeypatch): - def get_pdfjs_res(path): - if path == '/existing/file.html': - return b'foobar' - raise pdfjs.PDFJSNotFound(path) - - monkeypatch.setattr(pdfjs, 'get_pdfjs_res', get_pdfjs_res) - - @pytest.fixture(autouse=True) - def patch_backend(self, monkeypatch): - monkeypatch.setattr(qutescheme.objects, 'backend', - usertypes.Backend.QtWebKit) - - def test_existing_resource(self): - """Test with a resource that exists.""" - _mimetype, data = qutescheme.data_for_url( - QUrl('qute://pdfjs/existing/file.html')) - assert data == b'foobar' - - def test_nonexisting_resource(self, caplog): - """Test with a resource that does not exist.""" - with caplog.at_level(logging.WARNING, 'misc'): - with pytest.raises(qutescheme.NotFoundError): - qutescheme.data_for_url(QUrl('qute://pdfjs/no/file.html')) - assert len(caplog.records) == 1 - assert (caplog.records[0].message == - 'pdfjs resource requested but not found: /no/file.html')