Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions httpx/_transports/mock.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import datetime
import typing

from .._models import Request, Response
Expand All @@ -24,6 +25,7 @@ def handle_request(
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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve elapsed values explicitly supplied by the mock handler

This unconditionally replaces timing metadata that the handler already supplied, rather than just defaulting a missing value:

def handler(request):
    response = httpx.Response(200, text="ok")
    response.elapsed = timedelta(seconds=7)
    return response

with httpx.Client(transport=httpx.MockTransport(handler)) as client:
    assert client.get("http://local.invalid/").elapsed == timedelta(seconds=7)

The assertion passes on the base but fails here because elapsed becomes zero. This breaks existing mocks that deliberately simulate response timings, including the explicit assignment shown in #3712. The async assignment has the same regression. Please preserve a supplied elapsed value when adding the default.

Automated review; reproduced through both Client and AsyncClient on Python 3.13.15, base b5addb6 versus head 324e16a.

return response

async def handle_async_request(
Expand All @@ -40,4 +42,5 @@ async def handle_async_request(
if not isinstance(response, Response):
response = await response

response.elapsed = datetime.timedelta()
return response
39 changes: 39 additions & 0 deletions tests/client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,42 @@ def cp1252_but_no_content_type(request):
assert response.reason_phrase == "OK"
assert response.encoding == "ISO-8859-1"
assert response.text == text


def test_mock_transport_elapsed():
"""Test that MockTransport sets the elapsed property.

Regression test for https://github.com/encode/httpx/issues/3712
"""

def handler(request):
return httpx.Response(200, json={"text": "Hello, world!"})

transport = httpx.MockTransport(handler)
with httpx.Client(transport=transport) as client:
response = client.get("http://www.example.com")
assert response.status_code == 200
# MockTransport should set elapsed to timedelta(0)
assert response.elapsed == timedelta(0)
# Accessing elapsed should not raise RuntimeError
assert isinstance(response.elapsed, timedelta)


@pytest.mark.anyio
async def test_async_mock_transport_elapsed():
"""Test that MockTransport sets the elapsed property for async requests.

Regression test for https://github.com/encode/httpx/issues/3712
"""

def handler(request):
return httpx.Response(200, json={"text": "Hello, world!"})

transport = httpx.MockTransport(handler)
async with httpx.AsyncClient(transport=transport) as client:
response = await client.get("http://www.example.com")
assert response.status_code == 200
# MockTransport should set elapsed to timedelta(0)
assert response.elapsed == timedelta(0)
# Accessing elapsed should not raise RuntimeError
assert isinstance(response.elapsed, timedelta)