async def route_req(self, table_name: str, key: K, web: Web, request: Request) -> Response: """Route request to worker having key in table. Arguments: table_name: Name of the table. key: The key that we want. web: The currently sued web driver, request: The web request currently being served. """ app = self.app try: dest_url: URL = app.router.key_store(table_name, key) except KeyError: raise ServiceUnavailable() dest_ident = (host, port) = self._urlident(dest_url) if dest_ident == self._urlident(app.conf.canonical_url): raise SameNode() routed_url = request.url.with_host(host).with_port(int(port)) async with app.http_client.get(routed_url) as response: return web.text( await response.text(), content_type=response.content_type, status=response.status, )
async def route_req(self, table_name: str, key: K, web: Web, request: Request) -> Response: app = self.app dest_url: URL = app.router.key_store(table_name, key) dest_ident = (host, port) = self._urlident(dest_url) if dest_ident == self._urlident(app.conf.canonical_url): raise SameNode() routed_url = request.url.with_host(host).with_port(int(port)) async with app.http_client.get(routed_url) as response: return web.text(await response.text(), content_type=response.content_type)
async def _route_req(self, dest_url: URL, web: Web, request: Request) -> Response: app = self.app dest_ident = (host, port) = self._urlident(dest_url) if dest_ident == self._urlident(app.conf.canonical_url): raise SameNode() routed_url = request.url.with_host(host).with_port(int(port)) async with app.http_client.request(request.method, routed_url) as response: return web.text( await response.text(), content_type=response.content_type, status=response.status, )
async def test_table_route__exact_key(self, *, app): table = app.Table('foo') view = Mock() request = Mock() app.router.route_req = AsyncMock() @app.table_route(table, exact_key='active') async def routed(self, request): return 42 ret = await routed(view, request) assert ret is app.router.route_req.coro.return_value app.router.route_req.coro.side_effect = SameNode() ret = await routed(view, request) assert ret == 42
async def test_topic_route__exact_key(self, *, app): topic = app.topic("foo") view = Mock() request = Mock() app.router.route_topic_req = AsyncMock() @app.topic_route(topic, exact_key="active") async def routed(self, request): return 42 ret = await routed(view, request) assert ret is app.router.route_topic_req.coro.return_value app.router.route_topic_req.coro.side_effect = SameNode() ret = await routed(view, request) assert ret == 42
async def test_table_route__match_info(self, *, app): table = app.Table('foo') view = Mock() request = Mock() app.router.route_req = AsyncMock() @app.table_route(table, match_info='q') async def routed(self, request): return 42 request.match_info = {'q': 'KEY'} ret = await routed(view, request) assert ret is app.router.route_req.coro.return_value app.router.route_req.coro.side_effect = SameNode() ret = await routed(view, request) assert ret == 42
async def test_table_route__query_param(self, *, app): table = app.Table("foo") view = Mock() request = Mock() app.router.route_req = AsyncMock() @app.table_route(table, query_param="q") async def routed(self, request): return 42 request.query = {"q": "KEY"} ret = await routed(view, request) assert ret is app.router.route_req.coro.return_value app.router.route_req.coro.side_effect = SameNode() ret = await routed(view, request) assert ret == 42
async def test_topic_route__match_info(self, *, app): topic = app.topic("foo") view = Mock() request = Mock() app.router.route_topic_req = AsyncMock() @app.topic_route(topic, match_info="q") async def routed(self, request): return 42 request.match_info = {"q": "KEY"} ret = await routed(view, request) assert ret is app.router.route_topic_req.coro.return_value app.router.route_topic_req.coro.side_effect = SameNode() ret = await routed(view, request) assert ret == 42
def router_same(app): app.router.route_req = Mock(name='app.router.route_req') app.router.route_req.side_effect = SameNode() return app.router