def test_parser_async(body, doc): with disable_asgi_non_coroutine_wrapping(): class WrappedRespondersBodyParserAsyncResource: @falcon.before(validate_param_async, 'limit', 100) @falcon.before(parse_body_async) async def on_get(self, req, resp, doc=None): self.req = req self.resp = resp self.doc = doc app = create_app(asgi=True) resource = WrappedRespondersBodyParserAsyncResource() app.add_route('/', resource) testing.simulate_get(app, '/', body=body) assert resource.doc == doc 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 testing.invoke_coroutine_sync(test_direct)
def test_coroutine_required(self, client): with disable_asgi_non_coroutine_wrapping(): with pytest.raises(TypeError) as exinfo: client.app.add_route('/', PartialCoroutineResource()) assert 'responder must be a non-blocking async coroutine' in str( exinfo.value)
def test_add_sync_sink(self, client, asgi): if asgi: with disable_asgi_non_coroutine_wrapping(): with pytest.raises(falcon.CompatibilityError): client.app.add_sink(kitchen_sink) else: client.app.add_sink(kitchen_sink, '/features') self._verify_kitchen_sink(client)
def test_handler_must_be_coroutine_for_asgi(self): async def legacy_handler(err, rq, rs, prms): pass app = create_app(True) with disable_asgi_non_coroutine_wrapping(): with pytest.raises(ValueError): app.add_error_handler(Exception, capture_error)
def test_jsonchema_validator(client): with disable_asgi_non_coroutine_wrapping(): client.app.add_route('/', _cythonized.TestResourceWithValidation()) with pytest.raises(TypeError): client.app.add_route( '/wowsuchfail', _cythonized.TestResourceWithValidationNoHint()) client.simulate_get()
def test_hooks(client): with disable_asgi_non_coroutine_wrapping(): with pytest.raises(TypeError): client.app.add_route('/', _cythonized.TestResourceWithHooksNoHintBefore()) client.app.add_route('/', _cythonized.TestResourceWithHooksNoHintAfter()) client.app.add_route('/', _cythonized.TestResourceWithHooks()) result = client.simulate_get() assert result.headers['x-answer'] == '42' assert result.json == {'answer': 42}
def test_both_schemas_validation_failure(asgi): bad_resp = MockResp(False) with pytest.raises(falcon.HTTPInternalServerError) as excinfo: call_method(asgi, 'both_validated', MockReq(asgi), bad_resp) assert excinfo.value.title == 'Response data failed validation' with pytest.raises(falcon.HTTPBadRequest) as excinfo: call_method(asgi, 'both_validated', MockReq(asgi, False), MockResp()) assert excinfo.value.title == 'Request data failed validation' client = testing.TestClient(create_app(asgi)) resource = ResourceAsync() if asgi else Resource() with disable_asgi_non_coroutine_wrapping(): client.app.add_route('/test', resource) result = client.simulate_put('/test', json=_INVALID_MEDIA) assert result.status_code == 400
def cors_client(asgi): # NOTE(kgriffs): Disable wrapping to test that built-in middleware does # not require it (since this will be the case for non-test apps). with disable_asgi_non_coroutine_wrapping(): app = create_app(asgi, cors_enable=True) return testing.TestClient(app)