async def test_overlapping_websocket_ctx( websocket_scope: WebsocketScope) -> None: app = Quart(__name__) websocket = Websocket( "/", b"", "ws", Headers([("host", "quart.com")]), "", "1.1", [], None, None, None, None, websocket_scope, ) ctx1 = app.websocket_context(websocket) await ctx1.__aenter__() ctx2 = app.websocket_context(websocket) await ctx2.__aenter__() await ctx1.__aexit__(None, None, None) assert has_app_context() # Ensure the app context still exists for ctx2 await ctx2.__aexit__(None, None, None)
async def test_overlapping_websocket_ctx() -> None: app = Quart(__name__) websocket = Websocket("/", b"", "ws", CIMultiDict(), "", "1.1", [], None, None, None) ctx1 = app.websocket_context(websocket) await ctx1.__aenter__() ctx2 = app.websocket_context(websocket) await ctx2.__aenter__() await ctx1.__aexit__(None, None, None) assert has_app_context() # Ensure the app context still exists for ctx2 await ctx2.__aexit__(None, None, None)
RequestContext(app, request) assert isinstance(request.routing_exception, exception_type) # type: ignore @pytest.mark.parametrize( 'request_factory, context_class, is_websocket', [ # type: ignore ( lambda method, path, headers: Request(method, 'http', path, headers ), RequestContext, True, ), ( lambda _, path, headers: Websocket(path, 'ws', headers, Mock(), Mock()), WebsocketContext, False, ), ], ) def test_bad_request_if_websocket_missmatch( request_factory: object, context_class: object, is_websocket: bool, ) -> None: app = Quart(__name__) url_adapter = Mock() url_adapter.match.return_value = Rule('/', ['GET'], 'index', is_websocket=is_websocket), {}
app.create_url_adapter = lambda *_: url_adapter # type: ignore request = Request('GET', 'http', '/', b'', CIMultiDict()) RequestContext(app, request) assert isinstance(request.routing_exception, exception_type) # type: ignore @pytest.mark.parametrize( 'request_factory, context_class, is_websocket', [ ( lambda method, path, headers: Request(method, 'http', path, b'', headers), RequestContext, True, ), ( lambda _, path, headers: Websocket( path, b'', 'ws', headers, [], Mock(), Mock(), lambda: None, ), WebsocketContext, False, ), ], ) def test_bad_request_if_websocket_missmatch( request_factory: object, context_class: object, is_websocket: bool, ) -> None: app = Quart(__name__) url_adapter = Mock() url_adapter.match.return_value = Rule('/', {'GET'}, 'index', is_websocket=is_websocket), {} app.create_url_adapter = lambda *_: url_adapter # type: ignore request_websocket = request_factory('GET', '/', CIMultiDict()) # type: ignore context_class(app, request_websocket) # type: ignore assert isinstance(request_websocket.routing_exception, BadRequest)