def test_rest_api_connection_url_handling(requests_mock, base, paths, expected_path): """Test connection __init__ and proper joining of root url and API path""" conn = RestApiConnection(base) requests_mock.get(expected_path, text="payload") requests_mock.post(expected_path, text="payload") for path in paths: assert conn.get(path).text == "payload" assert conn.post(path, {"foo": "bar"}).text == "payload"
def test_502_proxy_error(requests_mock): """EP-3387""" requests_mock.get("https://oeo.net/bar", status_code=502, text="""<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>502 Proxy Error</title> </head><body> <h1>Proxy Error</h1> <p>The proxy server received an invalid response from an upstream server.<br /> The proxy server could not handle the request <em><a href="/openeo/0.4.0/result">POST /openeo/0.4.0/result</a></em>.<p> Reason: <strong>Error reading from remote server</strong></p></p> </body></html>""") conn = RestApiConnection(API_URL) with pytest.raises(OpenEoApiError, match="Consider.*batch jobs.*instead.*synchronous"): conn.get("/bar")
def test_rest_api_expected_status(requests_mock): conn = RestApiConnection(API_URL) requests_mock.get("https://oeo.net/foo", status_code=200, json={"o": "k"}) # Expected status assert conn.get("/foo", expected_status=200).json() == {"o": "k"} assert conn.get("/foo", expected_status=[200, 201]).json() == {"o": "k"} # Unexpected status with pytest.raises(OpenEoClientException, match=r"Got status code 200 for `GET /foo` \(expected 204\)"): conn.get("/foo", expected_status=204) with pytest.raises(OpenEoClientException, match=r"Got status code 200 for `GET /foo` \(expected \[203, 204\]\)"): conn.get("/foo", expected_status=[203, 204])
def test_rest_api_headers(): conn = RestApiConnection(API_URL) with requests_mock.Mocker() as m: def text(request, context): assert request.headers["User-Agent"].startswith( "openeo-python-client") assert request.headers["X-Openeo-Bar"] == "XY123" m.get("/foo", text=text) m.post("/foo", text=text) conn.get("/foo", headers={"X-Openeo-Bar": "XY123"}) conn.post("/foo", {}, headers={"X-Openeo-Bar": "XY123"})
def test_rest_api_headers(): conn = RestApiConnection(API_URL) with requests_mock.Mocker() as m: def text(request, context): assert re.match( r"^openeo-python-client/[0-9a-z.-]+ .*python/3.* (linux|win|darwin)", request.headers["User-Agent"], re.I) assert request.headers["X-Openeo-Bar"] == "XY123" m.get("/foo", text=text) m.post("/foo", text=text) conn.get("/foo", headers={"X-Openeo-Bar": "XY123"}) conn.post("/foo", {}, headers={"X-Openeo-Bar": "XY123"})
def test_rest_api_expected_status_with_error(requests_mock): conn = RestApiConnection(API_URL) requests_mock.get("https://oeo.test/bar", status_code=406, json={ "code": "NoBar", "message": "no bar please" }) # First check for API error by default with pytest.raises(OpenEoApiError, match=r"\[406\] NoBar: no bar please"): conn.get("/bar", expected_status=200) with pytest.raises(OpenEoApiError, match=r"\[406\] NoBar: no bar please"): conn.get("/bar", expected_status=[201, 202]) # Don't fail when an error status is actually expected conn.get("/bar", expected_status=406) conn.get("/bar", expected_status=[406, 407]) with pytest.raises(OpenEoApiError, match=r"\[406\] NoBar: no bar please"): conn.get("/bar", expected_status=[401, 402]) # Don't check for error, just status conn.get("/bar", check_error=False, expected_status=406) with pytest.raises( OpenEoClientException, match=r"Got status code 406 for `GET /bar` \(expected \[302\]\)"): conn.get("/bar", check_error=False, expected_status=302) with pytest.raises( OpenEoClientException, match= r"Got status code 406 for `GET /bar` \(expected \[302, 303\]\)"): conn.get("/bar", check_error=False, expected_status=[302, 303])