def test_authentication_fail_retry(monkeypatch): post_retry = RetryRecorder(error=KerberosExchangeError()) monkeypatch.setattr(PrestoRequest.http.Session, "post", post_retry) get_retry = RetryRecorder(error=KerberosExchangeError()) monkeypatch.setattr(PrestoRequest.http.Session, "get", get_retry) attempts = 3 kerberos_auth = KerberosAuthentication() req = PrestoRequest( host="coordinator", port=8080, user="******", http_scheme=constants.HTTPS, auth=kerberos_auth, max_attempts=attempts, ) with pytest.raises(KerberosExchangeError): req.post("URL") assert post_retry.retry_count == attempts with pytest.raises(KerberosExchangeError): req.get("URL") assert post_retry.retry_count == attempts
def test_request_timeout(): timeout = 0.1 http_scheme = "http" host = "coordinator" port = 8080 url = http_scheme + "://" + host + ":" + str( port) + constants.URL_STATEMENT_PATH def long_call(request, uri, headers): time.sleep(timeout * 2) return (200, headers, "delayed success") httpretty.enable() for method in [httpretty.POST, httpretty.GET]: httpretty.register_uri(method, url, body=long_call) # timeout without retry for request_timeout in [timeout, (timeout, timeout)]: req = PrestoRequest( host=host, port=port, user="******", http_scheme=http_scheme, max_attempts=1, request_timeout=request_timeout, ) with pytest.raises(requests.exceptions.Timeout): req.get(url) with pytest.raises(requests.exceptions.Timeout): req.post("select 1") httpretty.disable() httpretty.reset()
def test_additional_request_post_headers(monkeypatch): """ Tests that the `PrestoRequest.post` function can take addtional headers and that it combines them with the existing ones to perform the request. """ post_recorder = ArgumentsRecorder() monkeypatch.setattr(PrestoRequest.http.Session, "post", post_recorder) req = PrestoRequest( host="coordinator", port=8080, user="******", source="test", catalog="test", schema="test", http_scheme="http", session_properties={}, ) sql = 'select 1' additional_headers = { 'X-Presto-Fake-1': 'one', 'X-Presto-Fake-2': 'two', } combined_headers = req.http_headers combined_headers.update(additional_headers) req.post(sql, additional_headers) # Validate that the post call was performed including the addtional headers assert post_recorder.kwargs['headers'] == combined_headers
def test_request_headers(monkeypatch): post_recorder = ArgumentsRecorder() monkeypatch.setattr(PrestoRequest.http.Session, "post", post_recorder) get_recorder = ArgumentsRecorder() monkeypatch.setattr(PrestoRequest.http.Session, "get", get_recorder) catalog = "test_catalog" schema = "test_schema" user = "******" source = "test_source" accept_encoding_header = "accept-encoding" accept_encoding_value = "identity,deflate,gzip" client_info_header = constants.HEADER_PREFIX + "Client-Info" client_info_value = "some_client_info" req = PrestoRequest( host="coordinator", port=8080, user=user, source=source, catalog=catalog, schema=schema, http_scheme="http", session_properties={}, http_headers={ accept_encoding_header: accept_encoding_value, client_info_header: client_info_value, }, redirect_handler=None, ) def assert_headers(headers): assert headers[constants.HEADER_CATALOG] == catalog assert headers[constants.HEADER_SCHEMA] == schema assert headers[constants.HEADER_SOURCE] == source assert headers[constants.HEADER_USER] == user assert headers[constants.HEADER_SESSION] == "" assert headers[accept_encoding_header] == accept_encoding_value assert headers[client_info_header] == client_info_value assert len(headers.keys()) == 8 req.post("URL") assert_headers(post_recorder.kwargs["headers"]) req.get("URL") assert_headers(get_recorder.kwargs["headers"])
def test_503_error_retry(monkeypatch): http_resp = PrestoRequest.http.Response() http_resp.status_code = 503 post_retry = RetryRecorder(result=http_resp) monkeypatch.setattr(PrestoRequest.http.Session, "post", post_retry) get_retry = RetryRecorder(result=http_resp) monkeypatch.setattr(PrestoRequest.http.Session, "get", get_retry) attempts = 3 req = PrestoRequest(host="coordinator", port=8080, user="******", max_attempts=attempts) req.post("URL") assert post_retry.retry_count == attempts req.get("URL") assert post_retry.retry_count == attempts
def test_gateway_redirect(monkeypatch): http_resp = PrestoRequest.http.Response() http_resp.status_code = 200 gateway_response = FakeGatewayResponse(http_resp, redirect_count=3) monkeypatch.setattr(PrestoRequest.http.Session, "post", gateway_response) monkeypatch.setattr(socket, "gethostbyaddr", lambda *args: ("finalhost", ["finalhost"], "1.2.3.4")) req = PrestoRequest(host="coordinator", port=8080, user="******") result = req.post("http://host:80/path/") assert gateway_response.count == 3 assert result.ok