Mocking fails when using Authlib HTTPX integration OAuth2 client
See original GitHub issueI’m trying to mock the responses from Keycloak using the Authlib HTTPX integration client for OAuth2.
I’m getting the following exception because the auth_flow() hook for the OAuth2 client is rewriting the request.url attribute so it is no longer a URLResponse object, it’s a plain URL (again).
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/home/bobh/.virtualenvs/traffica_stc/lib/python3.7/site-packages/authlib/integrations/httpx_client/oauth2_client.py:109: in _fetch_token
auth=auth, **kwargs)
/home/bobh/.virtualenvs/traffica_stc/lib/python3.7/site-packages/httpx/client.py:1316: in post
timeout=timeout,
/home/bobh/.virtualenvs/traffica_stc/lib/python3.7/site-packages/authlib/integrations/httpx_client/oauth2_client.py:89: in request
method, url, auth=auth, **kwargs)
/home/bobh/.virtualenvs/traffica_stc/lib/python3.7/site-packages/httpx/client.py:1097: in request
request, auth=auth, allow_redirects=allow_redirects, timeout=timeout,
/home/bobh/.virtualenvs/traffica_stc/lib/python3.7/site-packages/respx/mocks.py:162: in unbound_async_send
return await self.__AsyncClient__send__spy(client, request, **kwargs)
/home/bobh/.virtualenvs/traffica_stc/lib/python3.7/site-packages/respx/mocks.py:533: in __AsyncClient__send__spy
response = await _AsyncClient__send(client, request, **kwargs)
/home/bobh/.virtualenvs/traffica_stc/lib/python3.7/site-packages/httpx/client.py:1118: in send
request, auth=auth, timeout=timeout, allow_redirects=allow_redirects,
/home/bobh/.virtualenvs/traffica_stc/lib/python3.7/site-packages/httpx/client.py:1148: in send_handling_redirects
request, auth=auth, timeout=timeout, history=history
/home/bobh/.virtualenvs/traffica_stc/lib/python3.7/site-packages/httpx/client.py:1184: in send_handling_auth
response = await self.send_single_request(request, timeout)
/home/bobh/.virtualenvs/traffica_stc/lib/python3.7/site-packages/httpx/client.py:1208: in send_single_request
response = await dispatcher.send(request, timeout=timeout)
/home/bobh/.virtualenvs/traffica_stc/lib/python3.7/site-packages/httpx/dispatch/connection_pool.py:157: in send
raise exc
/home/bobh/.virtualenvs/traffica_stc/lib/python3.7/site-packages/httpx/dispatch/connection_pool.py:153: in send
response = await connection.send(request, timeout=timeout)
/home/bobh/.virtualenvs/traffica_stc/lib/python3.7/site-packages/httpx/dispatch/connection.py:42: in send
self.connection = await self.connect(timeout=timeout)
/home/bobh/.virtualenvs/traffica_stc/lib/python3.7/site-packages/httpx/dispatch/connection.py:63: in connect
host, port, ssl_context, timeout
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <respx.mocks.HTTPXMock object at 0x7f1e34a9a898>
hostname = 'myhost', port = 443
ssl_context = <ssl.SSLContext object at 0x7f1e33f9b8b8>
timeout = Timeout(timeout=5.0)
async def __Backend__open_tcp_stream__mock(
self,
hostname: str,
port: int,
ssl_context: typing.Optional[ssl.SSLContext],
timeout: Timeout,
) -> BaseSocketStream:
response = getattr(hostname, "attachment", None) # Pickup attached template
> return await response.socket_stream
E AttributeError: 'NoneType' object has no attribute 'socket_stream'
By this time the response is gone so I don’t know what the mocked method could return. It would have to go back and re-match on the request (somehow).
Any ideas to work around or fix this? I’m happy to push a PR if someone can point me at a possible fix.
Thanks
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
How to use the authlib.integrations.httpx_client.OAuth2Client ...
To help you get started, we've selected a few authlib.integrations.httpx_client.OAuth2Client examples, based on popular ways it is used in public projects.
Read more >OAuth for HTTPX — Authlib 1.2.0 documentation
HTTPX is a next-generation HTTP client for Python. Authlib enables OAuth 1.0 and OAuth 2.0 for HTTPX with its async versions: OAuth1Client ·...
Read more >spring - Mocking OAuth2 client with WebTestClient for servlet ...
Now I'd like to write an integration test ( @SpringBootTest ) to verify the behavior of a REST endpoint secured by OAuth2. The...
Read more >Third Party Packages - HTTPX
Plugins. Authlib. GitHub - Documentation. The ultimate Python library in building OAuth and OpenID Connect clients and servers. Includes an OAuth HTTPX client....
Read more >Integrate OAuth 2 Into Your Django/DRF Back-end | Toptal
So you've implemented user authentication. Now, you want to allow your users to log in with Twitter, Facebook, or Google. No problem.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For future @lundberg this is something that may/will change if/when Middleware becomes a thing: https://github.com/encode/httpx/pull/800
Sorry, only got around to updating our pin recently. I can confirm with authlib 0.15.1, httpx 0.16.1 and respx 0.14.0 the workaround is unnecessary.