async def test_resolves_handler_parameters(self): expected_user_name = "Some User Name" class User: def __init__(self, name): self.name = name def insert_user_into_type_registry(handler): async def _wrapper(wrapper: RequestWrapper): wrapper.types_registry.set(User(name=expected_user_name)) return await call_http_handler(wrapper.http_request, handler) return _wrapper @self.app.http.get(["/"]) @insert_user_into_type_registry async def handler(user: User): return web.json_response({"name": user.name}) async with HttpClientContext(self.app) as client: settings_mock = Settings() with mock.patch("asyncworker.signals.handlers.http.settings", settings_mock): resp = await client.get("/") self.assertEqual(200, resp.status) resp_data = await resp.json() self.assertEqual({"name": expected_user_name}, resp_data)
async def test_resolves_handler_parameters_when_receiving_request(self): def my_decorator(handler): async def _wrapper(request: web.Request): return await call_http_handler(request, handler) return _wrapper @self.app.route(["/"], type=RouteTypes.HTTP, methods=["GET"]) @my_decorator async def handler(request: web.Request): return web.json_response({"num": request.query["num"]}) with mock.patch.dict(os.environ, ASYNCWORKER_HTTP_PORT="9999"): settings_mock = Settings() with mock.patch( "asyncworker.signals.handlers.http.settings", settings_mock ): await self.signal_handler.startup(self.app) async with TestClient( TestServer(self.app[RouteTypes.HTTP]["app"]), loop=asyncio.get_event_loop(), ) as client: resp = await client.get("/", params={"num": 42}) self.assertEqual(200, resp.status) resp_data = await resp.json() self.assertEqual({"num": "42"}, resp_data) await self.signal_handler.shutdown(self.app)
async def test_handler_decorator_can_receive_aiohttp_request(self): def my_decorator(handler): async def _wrapper(request: web.Request): r_wrapper = RequestWrapper( http_request=request, types_registry=request["types_registry"], ) return await call_http_handler(r_wrapper, handler) return _wrapper @self.app.http.get(["/"]) @my_decorator async def handler(wrapper: RequestWrapper): return web.json_response({"num": wrapper.http_request.query["num"]}) async with HttpClientContext(self.app) as client: settings_mock = Settings() with mock.patch( "asyncworker.signals.handlers.http.settings", settings_mock ): resp = await client.get("/", params={"num": 42}) self.assertEqual(200, resp.status) resp_data = await resp.json() self.assertEqual({"num": "42"}, resp_data)
async def test_resolves_handler_parameters(self): expected_user_name = "Some User Name" class User: def __init__(self, name): self.name = name def insert_user_into_type_registry(handler): async def _wrapper(request: web.Request): request["types_registry"].set(User(name=expected_user_name)) return await call_http_handler(request, handler) return _wrapper @self.app.route(["/"], type=RouteTypes.HTTP, methods=["GET"]) @insert_user_into_type_registry async def handler(user: User): return web.json_response({"name": user.name}) with mock.patch.dict(os.environ, ASYNCWORKER_HTTP_PORT="9999"): settings_mock = Settings() with mock.patch( "asyncworker.signals.handlers.http.settings", settings_mock ): await self.signal_handler.startup(self.app) async with TestClient( TestServer(self.app[RouteTypes.HTTP]["app"]), loop=asyncio.get_event_loop(), ) as client: resp = await client.get("/") self.assertEqual(200, resp.status) resp_data = await resp.json() self.assertEqual({"name": expected_user_name}, resp_data) await self.signal_handler.shutdown(self.app)
async def test_handler_can_receive_aiohttp_request(self): @self.app.http.get(["/"]) async def handler(request: web.Request) -> web.Response: return web.json_response({"num": request.query["num"]}) async with HttpClientContext(self.app) as client: settings_mock = Settings() with mock.patch("asyncworker.signals.handlers.http.settings", settings_mock): resp = await client.get("/", params={"num": 42}) self.assertEqual(200, resp.status) resp_data = await resp.json() self.assertEqual({"num": "42"}, resp_data)
async def test_multiple_decorators_using_call_handler(self): expected_user_name = "Some User Name" expected_account_name = "Account Name" class User: def __init__(self, name): self.name = name class Account: def __init__(self, name): self.name = name def insert_user_into_type_registry(handler): async def _wrapper(request: web.Request): request["types_registry"].set(User(name=expected_user_name)) return await call_http_handler(request, handler) return _wrapper def insert_account_into_type_registry(handler): async def _wrapper(request: web.Request): request["types_registry"].set( Account(name=expected_account_name) ) return await call_http_handler(request, handler) return _wrapper @self.app.route(["/"], type=RouteTypes.HTTP, methods=["GET"]) @insert_account_into_type_registry @insert_user_into_type_registry async def handler(user: User, account: Account): return web.json_response( {"account": account.name, "user": user.name} ) async with HttpClientContext(self.app) as client: settings_mock = Settings() with mock.patch( "asyncworker.signals.handlers.http.settings", settings_mock ): resp = await client.get("/") self.assertEqual(200, resp.status) resp_data = await resp.json() self.assertEqual( { "user": expected_user_name, "account": expected_account_name, }, resp_data, )
async def test_ignores_return_annotation_when_resolving_parameters(self): @self.app.http.get(["/"]) async def handler(wrapper: RequestWrapper) -> web.Response: return web.json_response( {"num": wrapper.http_request.query["num"]}) async with HttpClientContext(self.app) as client: settings_mock = Settings() with mock.patch("asyncworker.signals.handlers.http.settings", settings_mock): resp = await client.get("/", params={"num": 42}) self.assertEqual(200, resp.status) resp_data = await resp.json() self.assertEqual({"num": "42"}, resp_data)
async def test_add_registry_to_all_requests(self): @self.app.http.get(["/"]) async def handler(wrapper: RequestWrapper): request = wrapper.http_request registry: TypesRegistry = request["types_registry"] assert registry is not None assert isinstance(registry, TypesRegistry) return web.json_response({}) async with HttpClientContext(self.app) as client: settings_mock = Settings() with mock.patch("asyncworker.signals.handlers.http.settings", settings_mock): resp = await client.get("/") self.assertEqual(200, resp.status)
async def test_can_call_handler_without_annotation(self): """ For backward compatiilty, wew can call a handler that receives one parameter and does not have any type annotations """ @self.app.route(["/"], type=RouteTypes.HTTP, methods=["GET"]) async def handler(): return web.json_response({}) async with HttpClientContext(self.app) as client: settings_mock = Settings() with mock.patch("asyncworker.signals.handlers.http.settings", settings_mock): resp = await client.get("/") self.assertEqual(200, resp.status)
async def test_simple_handler_200_response(self): """ Tests if a response is correctly handled, Starts a real aiohttp server """ @self.app.http.get(["/"]) async def index(): return web.json_response({"OK": True}) async with HttpClientContext(self.app) as client: settings_mock = Settings() with mock.patch("asyncworker.signals.handlers.http.settings", settings_mock): resp = await client.get("/") self.assertEqual(resp.status, 200) data = await resp.json() self.assertDictEqual({"OK": True}, data)
async def test_add_registry_to_all_requests(self): @self.app.route(["/"], type=RouteTypes.HTTP, methods=["GET"]) async def handler(request: web.Request): registry: TypesRegistry = request["types_registry"] assert registry is not None assert isinstance(registry, TypesRegistry) return web.json_response({}) with mock.patch.dict(os.environ, ASYNCWORKER_HTTP_PORT="9999"): settings_mock = Settings() with mock.patch( "asyncworker.signals.handlers.http.settings", settings_mock ): await self.signal_handler.startup(self.app) async with TestClient( TestServer(self.app[RouteTypes.HTTP]["app"]), loop=asyncio.get_event_loop(), ) as client: resp = await client.get("/") self.assertEqual(200, resp.status) await self.signal_handler.shutdown(self.app)
async def test_resolves_handler_parameters_when_receiving_request(self): def my_decorator(handler): async def _wrapper(request: web.Request): return await call_http_handler(request, handler) return _wrapper @self.app.route(["/"], type=RouteTypes.HTTP, methods=["GET"]) @my_decorator async def handler(request: web.Request): return web.json_response({"num": request.query["num"]}) async with HttpClientContext(self.app) as client: settings_mock = Settings() with mock.patch( "asyncworker.signals.handlers.http.settings", settings_mock ): resp = await client.get("/", params={"num": 42}) self.assertEqual(200, resp.status) resp_data = await resp.json() self.assertEqual({"num": "42"}, resp_data)
async def test_simple_handler_200_response(self): """ Tests if a response is correctly handled, Starts a real aiohttp server """ @self.app.route(["/"], type=RouteTypes.HTTP, methods=["GET"]) async def index(r): return web.json_response({"OK": True}) with mock.patch.dict(os.environ, ASYNCWORKER_HTTP_PORT="9999"): settings_mock = Settings() with mock.patch("asyncworker.signals.handlers.http.settings", settings_mock): await self.signal_handler.startup(self.app) async with TestClient( TestServer(self.app[RouteTypes.HTTP]["app"]), loop=asyncio.get_event_loop(), ) as client: resp = await client.get("/") self.assertEqual(resp.status, 200) data = await resp.json() self.assertDictEqual({"OK": True}, data) await self.signal_handler.shutdown(self.app)
async def test_can_call_handler_without_annotation(self): """ For backward compatiilty, wew can call a handler that receives one parameter and does not have any type annotations """ @self.app.route(["/"], type=RouteTypes.HTTP, methods=["GET"]) async def handler(request): return web.json_response({}) with mock.patch.dict(os.environ, ASYNCWORKER_HTTP_PORT="9999"): settings_mock = Settings() with mock.patch( "asyncworker.signals.handlers.http.settings", settings_mock ): await self.signal_handler.startup(self.app) async with TestClient( TestServer(self.app[RouteTypes.HTTP]["app"]), loop=asyncio.get_event_loop(), ) as client: resp = await client.get("/") self.assertEqual(200, resp.status) await self.signal_handler.shutdown(self.app)