This commit is contained in:
2024si96557-ship-it 2026-02-26 05:23:29 +00:00 committed by GitHub
commit f5b5c1245b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 0 deletions

View File

@ -1,5 +1,6 @@
from __future__ import annotations
import datetime
import typing
from .._models import Request, Response
@ -24,6 +25,7 @@ class MockTransport(AsyncBaseTransport, BaseTransport):
response = self.handler(request)
if not isinstance(response, Response): # pragma: no cover
raise TypeError("Cannot use an async handler in a sync Client")
response.elapsed = datetime.timedelta(seconds=0)
return response
async def handle_async_request(
@ -40,4 +42,5 @@ class MockTransport(AsyncBaseTransport, BaseTransport):
if not isinstance(response, Response):
response = await response
response.elapsed = datetime.timedelta(seconds=0)
return response

24
tests/test_transports.py Normal file
View File

@ -0,0 +1,24 @@
import datetime
import pytest
import httpx
def test_mock_transport_sets_elapsed():
"""Test that MockTransport sets the elapsed property on responses."""
# 1. Create a simple handler that returns a response
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(200, content=b"Hello")
# 2. Create a MockTransport with that handler
transport = httpx.MockTransport(handler)
# 3. Create a client with the transport
client = httpx.Client(transport=transport)
# 4. Make a request
response = client.get("http://example.com/")
# 5. Assert that elapsed is set and is a timedelta
assert hasattr(response, "_elapsed")
assert isinstance(response.elapsed, datetime.timedelta)