Add qute:javascript to serve JS files.

This commit is contained in:
Imran Sobir 2017-02-27 22:37:24 +05:00
parent 783769d302
commit 3b3846c9dc
2 changed files with 57 additions and 0 deletions

View File

@ -248,6 +248,19 @@ def qute_history(url):
return 'text/html', jinja.render('history.html', title='History')
@add_handler('javascript')
def qute_javascript(url):
"""Handler for qute:javascript.
Return content of file given as query parameter.
"""
path = url.path()
if path:
return 'text/html', utils.read_file("javascript" + path, binary=False)
else:
raise QuteSchemeError("No file specified", ValueError())
@add_handler('pyeval')
def qute_pyeval(_url):
"""Handler for qute:pyeval."""

View File

@ -18,6 +18,7 @@
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
import json
import os
import time
from PyQt5.QtCore import QUrl
@ -27,6 +28,49 @@ from qutebrowser.browser import history, qutescheme
from qutebrowser.utils import objreg
class TestJavascriptHandler:
"""Test the qute://javascript endpoint."""
# Tuples of fake JS files and their content.
js_files = [
('foo.js', "var a = 'foo';"),
('bar.js', "var a = 'bar';"),
]
@pytest.fixture(autouse=True)
def patch_read_file(self, monkeypatch):
"""Patch utils.read_file to return few fake JS files."""
def _read_file(path, binary=False):
"""Faked utils.read_file"""
assert not binary
for filename, content in self.js_files:
if path == os.path.join('javascript', filename):
return content
raise IOError("File not found {}!".format(path))
monkeypatch.setattr('qutebrowser.utils.utils.read_file', _read_file)
@pytest.mark.parametrize("filename, content", js_files)
def test_qutejavascript(self, filename, content):
url = QUrl("qute://javascript/{}".format(filename))
_mimetype, data = qutescheme.qute_javascript(url)
assert data == content
def test_qutejavascript_error(self):
url = QUrl("qute://javascript/404.js")
with pytest.raises(IOError):
qutescheme.qute_javascript(url)
def test_qutejavascript_empty_query(self):
url = QUrl("qute://javascript")
with pytest.raises(qutescheme.QuteSchemeError):
qutescheme.qute_javascript(url)
class TestHistoryHandler:
"""Test the qute://history endpoint."""