Skip to content

Commit

Permalink
Assert elapsed is computed
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin-b committed Dec 20, 2021
1 parent b5c1aa9 commit b3e2e53
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 3 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.17.1] - 2021-12-20
### Fixed
- Do not consider a response as read, even if it is not a stream, before returning to `httpx`. Allowing any specific httpx handling to be triggered such as `httpx.Response.elapsed` computing.

## [0.17.0] - 2021-12-20
### Changed
- `httpx_mock.add_response` `data` parameter is only used for multipart content. It was deprecated since `0.14.0`. Refer to this version changelog entry for more details on how to update your code.
Expand Down Expand Up @@ -180,7 +184,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- First release, should be considered as unstable for now as design might change.

[Unreleased]: https://github.com/Colin-b/pytest_httpx/compare/v0.17.0...HEAD
[Unreleased]: https://github.com/Colin-b/pytest_httpx/compare/v0.17.1...HEAD
[0.17.1]: https://github.com/Colin-b/pytest_httpx/compare/v0.17.0...v0.17.1
[0.17.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.16.0...v0.17.0
[0.16.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.15.0...v0.16.0
[0.15.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.14.0...v0.15.0
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<a href="https://github.com/Colin-b/pytest_httpx/actions"><img alt="Build status" src="https://github.com/Colin-b/pytest_httpx/workflows/Release/badge.svg"></a>
<a href="https://github.com/Colin-b/pytest_httpx/actions"><img alt="Coverage" src="https://img.shields.io/badge/coverage-100%25-brightgreen"></a>
<a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
<a href="https://github.com/Colin-b/pytest_httpx/actions"><img alt="Number of tests" src="https://img.shields.io/badge/tests-143 passed-blue"></a>
<a href="https://github.com/Colin-b/pytest_httpx/actions"><img alt="Number of tests" src="https://img.shields.io/badge/tests-145 passed-blue"></a>
<a href="https://pypi.org/project/pytest-httpx/"><img alt="Number of downloads" src="https://img.shields.io/pypi/dm/pytest_httpx"></a>
</p>

Expand Down
4 changes: 4 additions & 0 deletions pytest_httpx/_httpx_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,17 @@ def _get_response(self, request: httpx.Request) -> Optional[httpx.Response]:
# Allow to read the response on client side
response.is_stream_consumed = False
response.is_closed = False
if hasattr(response, "_content"):
del response._content
return response

# Or the last registered
matcher.nb_calls += 1
# Allow to read the response on client side
response.is_stream_consumed = False
response.is_closed = False
if hasattr(response, "_content"):
del response._content
return response

def _get_callback(
Expand Down
2 changes: 1 addition & 1 deletion pytest_httpx/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# Major should be incremented in case there is a breaking change. (eg: 2.5.8 -> 3.0.0)
# Minor should be incremented in case there is an enhancement. (eg: 2.5.8 -> 2.6.0)
# Patch should be incremented in case there is a bug fix. (eg: 2.5.8 -> 2.5.9)
__version__ = "0.17.0"
__version__ = "0.17.1"
9 changes: 9 additions & 0 deletions tests/test_httpx_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -1444,3 +1444,12 @@ async def test_header_as_httpx_headers(httpx_mock: HTTPXMock) -> None:
response = await client.get("https://test_url")

assert dict(response.cookies) == {"key": "value"}


@pytest.mark.asyncio
async def test_elapsed(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response()

async with httpx.AsyncClient() as client:
response = await client.get("https://test_url")
assert response.elapsed is not None
8 changes: 8 additions & 0 deletions tests/test_httpx_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -1343,3 +1343,11 @@ def test_header_as_httpx_headers(httpx_mock: HTTPXMock) -> None:
response = client.get("https://test_url")

assert dict(response.cookies) == {"key": "value"}


def test_elapsed(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response()

with httpx.Client() as client:
response = client.get("https://test_url")
assert response.elapsed is not None

0 comments on commit b3e2e53

Please sign in to comment.