async def test_endpoint_config_with_non_existent_cafile(tmp_path: Path): cafile = "data/test_endpoints/no_file.pem" endpoint = endpoint_utils.EndpointConfig("https://example.com/", cafile=str(cafile)) with pytest.raises(FileNotFoundException): await endpoint.request("post")
async def test_request_non_json_response(): with aioresponses() as mocked: endpoint = endpoint_utils.EndpointConfig("https://example.com/") mocked.post( "https://example.com/test", payload="ok", content_type="application/text", status=200, ) response = await endpoint.request("post", subpath="test") assert not response
async def test_endpoint_config(): with aioresponses() as mocked: endpoint = endpoint_utils.EndpointConfig( "https://example.com/", params={"A": "B"}, headers={"X-Powered-By": "Rasa"}, basic_auth={ "username": "******", "password": "******" }, token="mytoken", token_name="letoken", type="redis", port=6379, db=0, password="******", timeout=30000, ) mocked.post( "https://example.com/test?A=B&P=1&letoken=mytoken", payload={"ok": True}, repeat=True, status=200, ) await endpoint.request( "post", subpath="test", content_type="application/text", json={"c": "d"}, params={"P": "1"}, ) r = latest_request(mocked, "post", "https://example.com/test?A=B&P=1&letoken=mytoken") assert r assert json_of_latest_request(r) == {"c": "d"} assert r[-1].kwargs.get("params", {}).get("A") == "B" assert r[-1].kwargs.get("params", {}).get("P") == "1" assert r[-1].kwargs.get("params", {}).get("letoken") == "mytoken" # unfortunately, the mock library won't report any headers stored on # the session object, so we need to verify them separately async with endpoint.session() as s: assert s._default_headers.get("X-Powered-By") == "Rasa" assert s._default_auth.login == "user" assert s._default_auth.password == "pass"
async def test_endpoint_config_with_cafile(tmp_path: Path): cafile = "data/test_endpoints/cert.pem" with aioresponses() as mocked: endpoint = endpoint_utils.EndpointConfig("https://example.com/", cafile=str(cafile)) mocked.post("https://example.com/", status=200) await endpoint.request("post") request = latest_request(mocked, "post", "https://example.com/")[-1] ssl_context = request.kwargs["ssl"] certs = ssl_context.get_ca_certs() assert certs[0]["subject"][4][0] == ("organizationalUnitName", "rasa")