Use aiohttp middleware for mocking#170
Merged
sarayourfriend merged 1 commit intomasterfrom Mar 1, 2026
Merged
Conversation
sarayourfriend
commented
Mar 1, 2026
Comment on lines
-57
to
-58
| if not hasattr(headers, "__setitem__"): | ||
| raise TypeError("headers must be a dictionary") |
Collaborator
Author
There was a problem hiding this comment.
This check isn't necessary, or even accurate. The underlying HTTPHeaderDict class's extend method handles a host of different types of inputs and should be the source of any relevant TypeErrors anyway.
feb33a8 to
8206070
Compare
2 tasks
|
Awesome, thanks! I ran out of steam on this a while back, as you saw... Do you know when you'll be able to get this out into a release I'd be able to use? |
Collaborator
Author
|
Yep! Released here in v2.1.5: https://github.com/h2non/pook/releases/tag/v2.1.5 Already on PyPI as well 🙂 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #166
Replaces #167 cc @cjw296
This solves a host of issues, including #166, as well as that middlewares would not have previously worked.
This aiohttp feature is documented here: https://docs.aiohttp.org/en/stable/client_advanced.html#client-middleware
This PR changes the aiohttp implementation to be middleware based by refactoring the interceptor to implement aiohttp's ClientMiddlewareType (as the
__call__method). Rather than mocking all of aiohttp's_requestmethod, now pook injects its interceptor as the final middleware, then defers to the real_requestmethod.aiohttp's
_requestmethod picks up the middleware. As it is last, it will be the "innermost" middleware, and can intercept the actual request mechanism.Because this still prevents aiohttp's
_connect_and_send_requestfrom running when a mock matches, and thus prevents theConnector.connectmethod from running, this won't fully solve other issues like #152. Some traces will start to work now, though.PR Checklist