def test_fallback_filename(uri, default, expected, content_type, downloadable, monkeypatch): def mockOpen(path, mode): if default in path: return path raise IOError() monkeypatch.setattr(io, 'open', mockOpen) monkeypatch.setattr('os.path.isfile', lambda file: default in file) sr = StaticRoute('/static', '/var/www/statics', downloadable=downloadable, fallback_filename=default) req_path = '/static/' + uri req = Request(testing.create_environ( host='test.com', path=req_path, app='statics' )) resp = Response() sr(req, resp) assert sr.match(req.path) assert resp.stream == os.path.join('/var/www/statics', expected) assert resp.content_type == content_type if downloadable: assert os.path.basename(expected) in resp.downloadable_as else: assert resp.downloadable_as is None
def test_invalid_args_fallback_filename(client, default): prefix, directory = '/static', '/var/www/statics' with pytest.raises(ValueError, match='fallback_filename'): StaticRoute(prefix, directory, fallback_filename=default) with pytest.raises(ValueError, match='fallback_filename'): client.app.add_static_route(prefix, directory, fallback_filename=default)
def test_bad_path(uri, monkeypatch): monkeypatch.setattr(io, 'open', lambda path, mode: path) sr = StaticRoute('/static', '/var/www/statics') req = Request( testing.create_environ(host='test.com', path=uri, app='statics')) resp = Response() with pytest.raises(falcon.HTTPNotFound): sr(req, resp)
def test_good_path(uri_prefix, uri_path, expected_path, mtype, monkeypatch): monkeypatch.setattr(io, 'open', lambda path, mode: path) sr = StaticRoute(uri_prefix, '/var/www/statics') req_path = uri_prefix[:-1] if uri_prefix.endswith('/') else uri_prefix req_path += uri_path req = Request( testing.create_environ(host='test.com', path=req_path, app='statics')) resp = Response() sr(req, resp) assert resp.content_type == mtype assert resp.stream == '/var/www/statics' + expected_path
def test_match(default, path, expected, monkeypatch): monkeypatch.setattr('os.path.isfile', lambda file: True) sr = StaticRoute('/static', '/var/www/statics', fallback_filename=default) assert sr.match(path) == expected
def test_invalid_args(prefix, directory, client): with pytest.raises(ValueError): StaticRoute(prefix, directory) with pytest.raises(ValueError): client.app.add_static_route(prefix, directory)