Skip to content
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

aiohttp mocking does not respect ClientSession base_url or headers passed in constructor #156

Closed
pandaxtc opened this issue Nov 19, 2024 · 3 comments · Fixed by #157
Closed
Labels

Comments

@pandaxtc
Copy link

When using pook with aiohttp, the base_url and headers parameters passed to ClientSession are ignored. This causes matching to fail when using those parameters. For example,

#!/usr/bin/env python3

import aiohttp
import pook

BASE_URL = "http://example.com"
URL_SLUG = "/api/v1/get"
URL = f"{BASE_URL}{URL_SLUG}"

HEADERS = {"Authorization": "Token token"}


async def main():
    with pook.use():
        pook.get(URL).headers(HEADERS).reply(200)

        # Passes
        async with (
            aiohttp.ClientSession() as session,
            session.get(URL, headers=HEADERS) as response,
        ):
            print(response.status)

    with pook.use():
        pook.get(URL).headers(HEADERS).reply(200)

        # Fails
        async with (
            aiohttp.ClientSession(
                base_url=BASE_URL,
                headers=HEADERS,
            ) as session,
            session.get(URL_SLUG) as response,
        ):
            print(response.status)


if __name__ == "__main__":
    import asyncio

    asyncio.run(main())
@sarayourfriend
Copy link
Collaborator

Thank you for the report and reproduction code! If you're willing to work on a PR for this, I would appreciate it, otherwise I will try to attend to this next week.

@sarayourfriend
Copy link
Collaborator

Fixed and release in 2.1.2. Thanks again for the detailed report @pandaxtc!

@pandaxtc
Copy link
Author

Thanks for the quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants