Skip to content

Replace httpretty with responses #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
# httpretty >= 1.1 duplicates requests in `httpretty.latest_requests`
# https://github.com/gabrielfalcao/HTTPretty/issues/425
"httpretty < 1.1",
"responses>=0.24.1,<1",
"pytest",
"pytest-runner",
"pre-commit",
Expand Down
22 changes: 11 additions & 11 deletions tests/unit/oauth_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import uuid
from collections import namedtuple

import httpretty
import responses

from trino import constants

Expand Down Expand Up @@ -91,14 +91,14 @@ def __call__(self, request, uri, response_headers):

def _get_token_requests(challenge_id):
return list(filter(
lambda r: r.method == "GET" and r.path == f"/{TOKEN_PATH}/{challenge_id}",
httpretty.latest_requests()))
lambda r: r.method == "GET" and r.url == f"{TOKEN_RESOURCE}/{challenge_id}",
responses.calls))


def _post_statement_requests():
return list(filter(
lambda r: r.method == "POST" and r.path == constants.URL_STATEMENT_PATH,
httpretty.latest_requests()))
lambda r: r.method == "POST" and r.url == f"{SERVER_ADDRESS}{constants.URL_STATEMENT_PATH}",
responses.calls))


class MultithreadedTokenServer:
Expand All @@ -111,15 +111,15 @@ def __init__(self, sample_post_response_data, attempts=1):
self.attempts = attempts

# bind post statement
httpretty.register_uri(
method=httpretty.POST,
uri=f"{SERVER_ADDRESS}{constants.URL_STATEMENT_PATH}",
responses.add(
method=responses.POST,
url=f"{SERVER_ADDRESS}{constants.URL_STATEMENT_PATH}",
body=self.post_statement_callback)

# bind get token
httpretty.register_uri(
method=httpretty.GET,
uri=re.compile(rf"{TOKEN_RESOURCE}/.*"),
responses.add(
method=responses.GET,
url=re.compile(rf"{TOKEN_RESOURCE}/.*"),
body=self.get_token_callback)

# noinspection PyUnusedLocal
Expand Down
123 changes: 62 additions & 61 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
from zoneinfo import ZoneInfoNotFoundError

import gssapi
import httpretty
import keyring
import pytest
import requests
from httpretty import httprettified
import responses
from requests_gssapi.exceptions import SPNEGOExchangeError
from requests_kerberos.exceptions import KerberosExchangeError
from tzlocal import get_localzone_name # type: ignore
Expand Down Expand Up @@ -347,9 +346,9 @@ 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)
responses.start()
for method in [responses.POST, responses.GET]:
responses.add_callback(method, url, callback=long_call)

# timeout without retry
for request_timeout in [timeout, (timeout, timeout)]:
Expand All @@ -370,12 +369,12 @@ def long_call(request, uri, headers):
with pytest.raises(requests.exceptions.Timeout):
req.post("select 1")

httpretty.disable()
httpretty.reset()
responses.stop()
responses.reset()


@pytest.mark.parametrize("attempts", [1, 3, 5])
@httprettified
@responses.activate
def test_oauth2_authentication_flow(attempts, sample_post_response_data):
token = str(uuid.uuid4())
challenge_id = str(uuid.uuid4())
Expand All @@ -386,17 +385,17 @@ def test_oauth2_authentication_flow(attempts, sample_post_response_data):
post_statement_callback = PostStatementCallback(redirect_server, token_server, [token], sample_post_response_data)

# bind post statement
httpretty.register_uri(
method=httpretty.POST,
uri=f"{SERVER_ADDRESS}{constants.URL_STATEMENT_PATH}",
body=post_statement_callback)
responses.add_callback(
method=responses.POST,
url=f"{SERVER_ADDRESS}{constants.URL_STATEMENT_PATH}",
callback=post_statement_callback)

# bind get token
get_token_callback = GetTokenCallback(token_server, token, attempts)
httpretty.register_uri(
method=httpretty.GET,
uri=token_server,
body=get_token_callback)
responses.add_callback(
method=responses.GET,
url=token_server,
callback=get_token_callback)

redirect_handler = RedirectHandler()

Expand All @@ -417,7 +416,7 @@ def test_oauth2_authentication_flow(attempts, sample_post_response_data):
assert len(_get_token_requests(challenge_id)) == attempts


@httprettified
@responses.activate
def test_oauth2_refresh_token_flow(sample_post_response_data):
token = str(uuid.uuid4())
challenge_id = str(uuid.uuid4())
Expand All @@ -427,17 +426,17 @@ def test_oauth2_refresh_token_flow(sample_post_response_data):
post_statement_callback = PostStatementCallback(None, token_server, [token], sample_post_response_data)

# bind post statement
httpretty.register_uri(
method=httpretty.POST,
uri=f"{SERVER_ADDRESS}{constants.URL_STATEMENT_PATH}",
body=post_statement_callback)
responses.add_callback(
method=responses.POST,
url=f"{SERVER_ADDRESS}{constants.URL_STATEMENT_PATH}",
callback=post_statement_callback)

# bind get token
get_token_callback = GetTokenCallback(token_server, token)
httpretty.register_uri(
method=httpretty.GET,
uri=token_server,
body=get_token_callback)
responses.add_callback(
method=responses.GET,
url=token_server,
callback=get_token_callback)

redirect_handler = RedirectHandlerWithException(
trino.exceptions.TrinoAuthError(
Expand All @@ -460,7 +459,7 @@ def test_oauth2_refresh_token_flow(sample_post_response_data):


@pytest.mark.parametrize("attempts", [6, 10])
@httprettified
@responses.activate
def test_oauth2_exceed_max_attempts(attempts, sample_post_response_data):
token = str(uuid.uuid4())
challenge_id = str(uuid.uuid4())
Expand All @@ -471,17 +470,17 @@ def test_oauth2_exceed_max_attempts(attempts, sample_post_response_data):
post_statement_callback = PostStatementCallback(redirect_server, token_server, [token], sample_post_response_data)

# bind post statement
httpretty.register_uri(
method=httpretty.POST,
uri=f"{SERVER_ADDRESS}{constants.URL_STATEMENT_PATH}",
body=post_statement_callback)
responses.add_callback(
method=responses.POST,
url=f"{SERVER_ADDRESS}{constants.URL_STATEMENT_PATH}",
callback=post_statement_callback)

# bind get token
get_token_callback = GetTokenCallback(token_server, token, attempts)
httpretty.register_uri(
method=httpretty.GET,
uri=f"{TOKEN_RESOURCE}/{challenge_id}",
body=get_token_callback)
responses.add_callback(
method=responses.GET,
url=f"{TOKEN_RESOURCE}/{challenge_id}",
callback=get_token_callback)

redirect_handler = RedirectHandler()

Expand Down Expand Up @@ -509,13 +508,13 @@ def test_oauth2_exceed_max_attempts(attempts, sample_post_response_data):
('x_redirect_server="redirect_server", x_token_server="token_server"', 'Error: header info didn\'t match x_redirect_server="redirect_server", x_token_server="token_server"'), # noqa: E501
('Bearer x_redirect_server="redirect_server"', 'Error: header info didn\'t have x_token_server'),
])
@httprettified
@responses.activate
def test_oauth2_authentication_missing_headers(header, error):
# bind post statement
httpretty.register_uri(
method=httpretty.POST,
uri=f"{SERVER_ADDRESS}{constants.URL_STATEMENT_PATH}",
adding_headers={'WWW-Authenticate': header},
responses.add(
method=responses.POST,
url=f"{SERVER_ADDRESS}{constants.URL_STATEMENT_PATH}",
headers={'WWW-Authenticate': header},
status=401)

request = TrinoRequest(
Expand Down Expand Up @@ -543,7 +542,7 @@ def test_oauth2_authentication_missing_headers(header, error):
'x_token_server="{token_server}"'
'Bearer x_redirect_server="{redirect_server}",x_token_server="{token_server}",additional_challenge',
])
@httprettified
@responses.activate
def test_oauth2_header_parsing(header, sample_post_response_data):
token = str(uuid.uuid4())
challenge_id = str(uuid.uuid4())
Expand All @@ -560,17 +559,17 @@ def post_statement(request, uri, response_headers):
'Basic realm': '"Trino"'}, ""]

# bind post statement
httpretty.register_uri(
method=httpretty.POST,
uri=f"{SERVER_ADDRESS}{constants.URL_STATEMENT_PATH}",
body=post_statement)
responses.add_callback(
method=responses.POST,
url=f"{SERVER_ADDRESS}{constants.URL_STATEMENT_PATH}",
callback=post_statement)

# bind get token
get_token_callback = GetTokenCallback(token_server, token)
httpretty.register_uri(
method=httpretty.GET,
uri=token_server,
body=get_token_callback)
responses.add_callback(
method=responses.GET,
url=token_server,
callback=get_token_callback)

redirect_handler = RedirectHandler()

Expand All @@ -591,8 +590,7 @@ def post_statement(request, uri, response_headers):
assert len(_get_token_requests(challenge_id)) == 1


@pytest.mark.parametrize("http_status", [400, 401, 500])
@httprettified
@responses.activate
def test_oauth2_authentication_fail_token_server(http_status, sample_post_response_data):
token = str(uuid.uuid4())
challenge_id = str(uuid.uuid4())
Expand All @@ -603,16 +601,18 @@ def test_oauth2_authentication_fail_token_server(http_status, sample_post_respon
post_statement_callback = PostStatementCallback(redirect_server, token_server, [token], sample_post_response_data)

# bind post statement
httpretty.register_uri(
method=httpretty.POST,
uri=f"{SERVER_ADDRESS}{constants.URL_STATEMENT_PATH}",
body=post_statement_callback)

httpretty.register_uri(
method=httpretty.GET,
uri=f"{TOKEN_RESOURCE}/{challenge_id}",
responses.add(
method=responses.POST,
url=f"{SERVER_ADDRESS}{constants.URL_STATEMENT_PATH}",
json=post_statement_callback,
)

responses.add(
method=responses.GET,
url=f"{TOKEN_RESOURCE}/{challenge_id}",
status=http_status,
body="error")
body="error",
)

redirect_handler = RedirectHandler()

Expand All @@ -623,7 +623,8 @@ def test_oauth2_authentication_fail_token_server(http_status, sample_post_respon
user="test",
),
http_scheme=constants.HTTPS,
auth=trino.auth.OAuth2Authentication(redirect_auth_url_handler=redirect_handler))
auth=trino.auth.OAuth2Authentication(redirect_auth_url_handler=redirect_handler),
)

with pytest.raises(trino.exceptions.TrinoAuthError) as exp:
request.post("select 1")
Expand All @@ -634,7 +635,7 @@ def test_oauth2_authentication_fail_token_server(http_status, sample_post_respon
assert len(_get_token_requests(challenge_id)) == 1


@httprettified
@responses.activate
def test_multithreaded_oauth2_authentication_flow(sample_post_response_data):
redirect_handler = RedirectHandler()
auth = trino.auth.OAuth2Authentication(redirect_auth_url_handler=redirect_handler)
Expand Down
Loading
Loading