Skip to content

Commit

Permalink
fix: session object was never used in aiohttp request (#700) (#701)
Browse files Browse the repository at this point in the history
* fix: session object was never used in aiohttp request (#700)

* fixup! fix: session object was never used in aiohttp request (#700)

* fixup! fixup! fix: session object was never used in aiohttp request (#700)

Co-authored-by: arithmetic1728 <58957152+arithmetic1728@users.noreply.github.com>
  • Loading branch information
greshilov and arithmetic1728 committed Jun 3, 2021
1 parent 9f83764 commit 09e0389
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
7 changes: 6 additions & 1 deletion google/auth/transport/_aiohttp_requests.py
Expand Up @@ -138,7 +138,12 @@ class Request(transport.Request):
"""

def __init__(self, session=None):
self.session = None
# TODO: Use auto_decompress property for aiohttp 3.7+
if session is not None and session._auto_decompress:
raise ValueError(
"Client sessions with auto_decompress=True are not supported."
)
self.session = session

async def __call__(
self,
Expand Down
14 changes: 10 additions & 4 deletions system_tests/system_tests_async/conftest.py
Expand Up @@ -26,9 +26,7 @@
from google.auth.transport import _aiohttp_requests as aiohttp_requests
from system_tests.system_tests_sync import conftest as sync_conftest

ASYNC_REQUESTS_SESSION = aiohttp.ClientSession()

ASYNC_REQUESTS_SESSION.verify = False
TOKEN_INFO_URL = "https://www.googleapis.com/oauth2/v3/tokeninfo"


Expand All @@ -49,10 +47,18 @@ def authorized_user_file():
"""The full path to a valid authorized user file."""
yield sync_conftest.AUTHORIZED_USER_FILE


@pytest.fixture
async def aiohttp_session():
async with aiohttp.ClientSession(auto_decompress=False) as session:
yield session


@pytest.fixture(params=["aiohttp"])
async def http_request(request):
async def http_request(request, aiohttp_session):
"""A transport.request object."""
yield aiohttp_requests.Request(ASYNC_REQUESTS_SESSION)
yield aiohttp_requests.Request(aiohttp_session)


@pytest.fixture
async def token_info(http_request):
Expand Down
15 changes: 12 additions & 3 deletions tests_async/transport/test_aiohttp_requests.py
Expand Up @@ -112,11 +112,18 @@ def make_request(self):
return aiohttp_requests.Request()

def make_with_parameter_request(self):
http = mock.create_autospec(aiohttp.ClientSession, instance=True)
http = aiohttp.ClientSession(auto_decompress=False)
return aiohttp_requests.Request(http)

def test_unsupported_session(self):
http = aiohttp.ClientSession(auto_decompress=True)
with pytest.raises(ValueError):
aiohttp_requests.Request(http)

def test_timeout(self):
http = mock.create_autospec(aiohttp.ClientSession, instance=True)
http = mock.create_autospec(
aiohttp.ClientSession, instance=True, _auto_decompress=False
)
request = aiohttp_requests.Request(http)
request(url="http://example.com", method="GET", timeout=5)

Expand All @@ -142,7 +149,9 @@ def test_constructor(self):
assert authed_session.credentials == mock.sentinel.credentials

def test_constructor_with_auth_request(self):
http = mock.create_autospec(aiohttp.ClientSession)
http = mock.create_autospec(
aiohttp.ClientSession, instance=True, _auto_decompress=False
)
auth_request = aiohttp_requests.Request(http)

authed_session = aiohttp_requests.AuthorizedSession(
Expand Down

0 comments on commit 09e0389

Please sign in to comment.