fastmcp/tests/client/transports/test_transports.py
Giovanna Zanardini a6ddde27df
Allow OAuth instance to use the same httpx factory as the Transport (#2324)
* Allow OAuth instance to use the same httpx factory as the Transport

* Fix test

* Update SSL verification mode assertion in tests

* This is actually not needed

* Creating a Client instance is not needed for this test

* Fix test

* Apply httpx_client_factory fix to SSETransport

Extends the OAuth httpx_client_factory changes to SSETransport.
SSETransport had the same issues as StreamableHttpTransport where it
wasn't passing the custom httpx client factory to OAuth, causing
certificate verification settings to be ignored during OAuth flows.

Changes:
- Set httpx_client_factory before calling _set_auth()
- Pass httpx_client_factory to OAuth constructor
- Add test for SSETransport OAuth client factory propagation

---------

Co-authored-by: Jeremiah Lowin <153965+jlowin@users.noreply.github.com>
2025-11-04 12:14:28 -05:00

37 lines
1.1 KiB
Python

from ssl import VerifyMode
import httpx
from fastmcp.client.transports import SSETransport, StreamableHttpTransport
async def test_oauth_uses_same_client_as_transport_streamable_http():
transport = StreamableHttpTransport(
"https://some.fake.url/",
httpx_client_factory=lambda *args, **kwargs: httpx.AsyncClient(
verify=False, *args, **kwargs
),
auth="oauth",
)
async with transport.auth.httpx_client_factory() as httpx_client: # type: ignore[attr-defined]
assert (
httpx_client._transport._pool._ssl_context.verify_mode
== VerifyMode.CERT_NONE
)
async def test_oauth_uses_same_client_as_transport_sse():
transport = SSETransport(
"https://some.fake.url/",
httpx_client_factory=lambda *args, **kwargs: httpx.AsyncClient(
verify=False, *args, **kwargs
),
auth="oauth",
)
async with transport.auth.httpx_client_factory() as httpx_client: # type: ignore[attr-defined]
assert (
httpx_client._transport._pool._ssl_context.verify_mode
== VerifyMode.CERT_NONE
)