def test_pathlib_path(asgi, patch_open): patch_open() sr = create_sr(asgi, '/static/', pathlib.Path('/var/www/statics')) req_path = '/static/css/test.css' req = _util.create_req(asgi, host='test.com', path=req_path, root_path='statics') resp = _util.create_resp(asgi) if asgi: async def run(): await sr(req, resp) return await resp.stream.read() body = falcon.async_to_sync(run) else: sr(req, resp) body = resp.stream.read() assert body.decode() == os.path.normpath('/var/www/statics/css/test.css')
def test_good_path(asgi, uri_prefix, uri_path, expected_path, mtype, monkeypatch): monkeypatch.setattr(io, 'open', lambda path, mode: io.BytesIO(path.encode())) sr = create_sr(asgi, uri_prefix, '/var/www/statics') req_path = uri_prefix[:-1] if uri_prefix.endswith('/') else uri_prefix req_path += uri_path req = _util.create_req(asgi, host='test.com', path=req_path, root_path='statics') resp = _util.create_resp(asgi) if asgi: async def run(): await sr(req, resp) return await resp.stream.read() body = testing.invoke_coroutine_sync(run) else: sr(req, resp) body = resp.stream.read() assert resp.content_type == mtype assert body.decode() == '/var/www/statics' + expected_path
def test_good_path(asgi, uri_prefix, uri_path, expected_path, mtype, patch_open): patch_open() sr = create_sr(asgi, uri_prefix, '/var/www/statics') req_path = uri_prefix[:-1] if uri_prefix.endswith('/') else uri_prefix req_path += uri_path req = _util.create_req(asgi, host='test.com', path=req_path, root_path='statics') resp = _util.create_resp(asgi) if asgi: async def run(): await sr(req, resp) return await resp.stream.read() body = falcon.async_to_sync(run) else: sr(req, resp) body = resp.stream.read() assert resp.content_type in _MIME_ALTERNATIVE.get(mtype, (mtype, )) assert body.decode() == os.path.normpath('/var/www/statics' + expected_path) assert resp.headers.get('accept-ranges') == 'bytes'
def test_good_path(asgi, uri_prefix, uri_path, expected_path, mtype, monkeypatch): monkeypatch.setattr(io, 'open', lambda path, mode: io.BytesIO(path.encode())) sr = create_sr(asgi, uri_prefix, '/var/www/statics') req_path = uri_prefix[:-1] if uri_prefix.endswith('/') else uri_prefix req_path += uri_path req = _util.create_req(asgi, host='test.com', path=req_path, root_path='statics') resp = _util.create_resp(asgi) if asgi: async def run(): await sr(req, resp) return await resp.stream.read() body = falcon.async_to_sync(run) else: sr(req, resp) body = resp.stream.read() assert resp.content_type in _MIME_ALTERNATIVE.get(mtype, (mtype, )) assert body.decode() == os.path.normpath('/var/www/statics' + expected_path)
async def test_direct(): resource = WrappedRespondersBodyParserAsyncResource() req = testing.create_asgi_req() resp = create_resp(True) await resource.on_get(req, resp, doc) assert resource.doc == doc
async def test_direct(): resource = ClassResourceWithURIFieldsAsync() req = testing.create_asgi_req() resp = create_resp(True) await resource.on_get(req, resp, '1', '2') assert resource.fields == ('1', '2')
def test_fallback_filename(asgi, uri, default, expected, content_type, downloadable, patch_open, monkeypatch): def validate(path): if os.path.normpath(default) not in path: raise IOError() patch_open(validate=validate) monkeypatch.setattr('os.path.isfile', lambda file: os.path.normpath(default) in file) sr = create_sr( asgi, '/static', '/var/www/statics', downloadable=downloadable, fallback_filename=default, ) req_path = '/static/' + uri req = _util.create_req(asgi, host='test.com', path=req_path, root_path='statics') resp = _util.create_resp(asgi) if asgi: async def run(): await sr(req, resp) return await resp.stream.read() body = falcon.async_to_sync(run) else: sr(req, resp) body = resp.stream.read() assert sr.match(req.path) expected_content = os.path.normpath( os.path.join('/var/www/statics', expected)) assert body.decode() == expected_content assert resp.content_type in _MIME_ALTERNATIVE.get(content_type, (content_type, )) assert resp.headers.get('accept-ranges') == 'bytes' if downloadable: assert os.path.basename(expected) in resp.downloadable_as else: assert resp.downloadable_as is None
def test_bad_path(asgi, uri, patch_open): patch_open(b'') sr_type = StaticRouteAsync if asgi else StaticRoute sr = sr_type('/static', '/var/www/statics') req = _util.create_req(asgi, host='test.com', path=uri, root_path='statics') resp = _util.create_resp(asgi) with pytest.raises(falcon.HTTPNotFound): if asgi: falcon.async_to_sync(sr, req, resp) else: sr(req, resp)
def test_fallback_filename(asgi, uri, default, expected, content_type, downloadable, monkeypatch): def mock_open(path, mode): if os.path.normpath(default) in path: return io.BytesIO(path.encode()) raise IOError() monkeypatch.setattr(io, 'open', mock_open) monkeypatch.setattr('os.path.isfile', lambda file: os.path.normpath(default) in file) sr = create_sr(asgi, '/static', '/var/www/statics', downloadable=downloadable, fallback_filename=default) req_path = '/static/' + uri req = _util.create_req(asgi, host='test.com', path=req_path, root_path='statics') resp = _util.create_resp(asgi) if asgi: async def run(): await sr(req, resp) return await resp.stream.read() body = falcon.invoke_coroutine_sync(run) else: sr(req, resp) body = resp.stream.read() assert sr.match(req.path) assert body.decode() == os.path.normpath( os.path.join('/var/www/statics', expected)) assert resp.content_type in _MIME_ALTERNATIVE.get(content_type, (content_type, )) if downloadable: assert os.path.basename(expected) in resp.downloadable_as else: assert resp.downloadable_as is None
def test_bad_path(asgi, uri, monkeypatch): monkeypatch.setattr(io, 'open', lambda path, mode: io.BytesIO()) sr_type = StaticRouteAsync if asgi else StaticRoute sr = sr_type('/static', '/var/www/statics') req = _util.create_req(asgi, host='test.com', path=uri, root_path='statics') resp = _util.create_resp(asgi) with pytest.raises(falcon.HTTPNotFound): if asgi: testing.invoke_coroutine_sync(sr, req, resp) else: sr(req, resp)
def resp(asgi): return create_resp(asgi)
def resp(request): return create_resp(asgi=request.param)