async def make_request_and_envelope_response( app: web.Application, method: str, url: URL, headers: Optional[Dict[str, str]] = None, data: Optional[bytes] = None, ) -> web.Response: """ Helper to forward a request to the catalog service """ session = get_client_session(app) try: async with session.request(method, url, headers=headers, data=data) as resp: payload = await resp.json() try: resp.raise_for_status() resp_data = wrap_as_envelope(data=payload) except ClientResponseError as err: if 500 <= err.status: raise err resp_data = wrap_as_envelope(error=payload["errors"]) return web.json_response(resp_data, status=resp.status) except (asyncio.TimeoutError, ClientConnectionError, ClientResponseError) as err: logger.warning("Catalog service errors upon request %s %s: %s", method, url.relative(), err) raise web.HTTPServiceUnavailable( reason="catalog is currently unavailable") from err
def test_relative_abs_parts_are_removed(): url = URL("http://*****:*****@example.com:8080/path?a=b#frag") rel = url.relative() assert not rel.scheme assert not rel.user assert not rel.password assert not rel.host assert not rel.port
def test_url_translation(): front_url = URL( f"https://osparc.io/{api_version_prefix}/catalog/dags/123?page_size=6") rel_url = front_url.relative() assert rel_url.path.startswith(f"/{api_version_prefix}/catalog") api_target_origin = URL("http://catalog:8000") api_target_url = to_backend_service(rel_url, api_target_origin, "v5") assert str(api_target_url) == "http://catalog:8000/v5/dags/123?page_size=6"
async def test_spawned_from_client(client): """ client <-> reverse_proxy_server <-> spawner_server <-> spawned_servers """ registry = {} IMAGE = "A:latest" # spawns 3 services: client <-> reverse_proxy_server <-> spawner_server for _ in range(3): resp = await client.post("/services", params="action=start", json={ "image": IMAGE } ) data = await resp.json() assert resp.status == 200, data sid = data["id"] mountpoint = data["mountpoint"] registry[sid] = mountpoint # pings them: client <-> reverse_proxy_server <-> spawned_servers for sid, mountpoint in registry.items(): resp = await client.get(mountpoint+"/ping") assert resp.status == 200 data = await resp.json() assert data["name"] == sid assert data["image"] == IMAGE assert data["received"]["method"] == "GET" assert data["received"]["proxy_path"] == "ping" tail = client.app.router["reverse_proxy"].url_for( serviceId=sid, proxyPath="ping") url = URL(data["received"]["url"]) assert url.relative() == tail assert not data["received"]["body"]
def test_relative_is_relative(): url = URL("http://*****:*****@example.com:8080/path?a=b#frag") rel = url.relative() assert not rel.is_absolute()
def test_relative(): url = URL("http://*****:*****@example.com:8080/path?a=b#frag") rel = url.relative() assert str(rel) == "/path?a=b#frag"