async def test_get_exception_handler_pre_hooks_error(): hook1 = mock.AsyncMock() hook2 = mock.MagicMock() hook3 = mock.MagicMock(side_effect=ValueError('hook error')) handler = middleware.get_exception_handler(pre_hooks=[hook1, hook2, hook3]) with pytest.raises(ValueError) as err: await handler( request=Request(scope={ 'type': 'http', 'app': FastAPI() }, ), exc=ValueError('test error'), ) assert 'hook error' == str(err.value) hook1.assert_called_once() hook2.assert_called_once() hook3.assert_called_once()
async def test_get_exception_handler_pre_hooks_ok(): hook1 = mock.AsyncMock() hook2 = mock.MagicMock() hook3 = mock.MagicMock() handler = middleware.get_exception_handler(pre_hooks=[hook1, hook2, hook3]) resp = await handler( request=Request(scope={ 'type': 'http', 'app': FastAPI() }, ), exc=ValueError('test error'), ) assert isinstance(resp, middleware.ProblemResponse) assert resp.debug is False assert resp.body == b'{"exc_type":"ValueError","type":"about:blank","title":"Unexpected Server Error","status":500,"detail":"test error"}' # noqa hook1.assert_called_once() hook2.assert_called_once() hook3.assert_called_once()
from pyldapi import Renderer, Profile from pyldapi.data import RDF_MEDIATYPES from fastapi.requests import Request # Mock FastAPI / Starlette Request object req = Request({"type": "http", "query_string": None, "headers": {}}) # dummy profile to use sdo = Profile( uri="https://schema.org", label="schema.org", comment= "Schema.org is a collaborative, community activity with a mission to create, maintain, and promote schemas " "for structured data on the Internet, on web pages, in email messages, and beyond.", mediatypes=RDF_MEDIATYPES, default_mediatype="text/turtle", languages=["en"], default_language="en", ) r = Renderer(req, "http://example.com", {"sdo": sdo}, "alt") txt = r._render_alt_profile_html(alt_template="alt.html", additional_alt_template_context={ "stuff": ["one", "two"], "title": "Dummy Title" }) print(txt.body.decode())
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint): with RequestIDContext() as request_id: request.request_id = request_id return await call_next(request)
async def test_core_not_available(): try: await create_new_session(Request(scope={'type': 'http'}), 'abc', {}, {}) assert False except SuperTokensGeneralError: assert True
async def exception_handler(request: Request, _: RequiresLoginException) -> Response: "Redirect to homepage if login fails" return RedirectResponse(url=request.url_for(home_name))