Skip to content

WIP - aiohttp: support for auth from ClientSession#167

Closed
cjw296 wants to merge 1 commit intoh2non:masterfrom
cjw296:aiohttp-auth-headers
Closed

WIP - aiohttp: support for auth from ClientSession#167
cjw296 wants to merge 1 commit intoh2non:masterfrom
cjw296:aiohttp-auth-headers

Conversation

@cjw296
Copy link

@cjw296 cjw296 commented Oct 14, 2025

(not ready yet, just pushing up to see what CI looks like)

Description

PR Checklist

  • I've added tests for any code changes
  • I've documented any new features

if "Content-Type" not in req.headers:
req.headers["Content-Type"] = "application/json"

# Lifted from the ClientSession._request method we're mocking:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aiohttp is licenced under Apache 2.0. It is probably best to put this portion of code into a separate directory with its own LICENSE file (and heading) to clarify that this code is not MIT licenced with the rest of pook.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see how this goes....

Comment on lines +102 to +113
async def test_client_auth_merged(local_responder):
"""Auth headers set on the client should be matched"""
pook \
.get(local_responder + "/status/404") \
.header("Authorization", "Basic dXNlcjpwYXNzd29yZA==") \
.reply(200).body("hello from pook")
async with aiohttp.ClientSession(auth=BasicAuth('user', 'password')) as session:
res = await session.get(
local_responder + "/status/404", headers={"x-pook-secondary": "xyz"}
)
assert res.status == 200
assert await res.read() == b"hello from pook"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding a basic auth test to the standard interceptor tests 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there actually is already, the problem here is that aiohttp.ClientSession(auth=BasicAuth('user', 'password')) is an aiohttp-specific way of setting session-wide basic auth, and pook's interceptor currently doesn't handle that.

@sarayourfriend
Copy link
Collaborator

Closing in favour of #170 which fixes the issue without needing to further into aiohttp's guts 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants