Skip to content

Commit

Permalink
Merge pull request #62 from Colin-b/develop
Browse files Browse the repository at this point in the history
Release 0.17.2
  • Loading branch information
Colin-b authored Dec 23, 2021
2 parents 83f3116 + 7b09946 commit 8dec995
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 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.2] - 2021-12-23
### Fixed
- Do not consider a callback 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.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.
Expand Down Expand Up @@ -184,7 +188,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.1...HEAD
[Unreleased]: https://github.com/Colin-b/pytest_httpx/compare/v0.17.2...HEAD
[0.17.2]: https://github.com/Colin-b/pytest_httpx/compare/v0.17.1...v0.17.2
[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
Expand Down
24 changes: 10 additions & 14 deletions pytest_httpx/_httpx_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,19 @@ def _handle_request(
self._requests.append(request)

response = self._get_response(request)
if not response:
callback = self._get_callback(request)
if callback:
response = callback(request)

if 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

callback = self._get_callback(request)
if callback:
return callback(request)

raise httpx.TimeoutException(
self._explain_that_no_response_was_found(request), request=request
)
Expand Down Expand Up @@ -224,20 +230,10 @@ def _get_response(self, request: httpx.Request) -> Optional[httpx.Response]:
# Return the first not yet called
if not matcher.nb_calls:
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

# 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.1"
__version__ = "0.17.2"
11 changes: 10 additions & 1 deletion tests/test_httpx_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -1447,9 +1447,18 @@ async def test_header_as_httpx_headers(httpx_mock: HTTPXMock) -> None:


@pytest.mark.asyncio
async def test_elapsed(httpx_mock: HTTPXMock) -> None:
async def test_elapsed_when_add_response(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


@pytest.mark.asyncio
async def test_elapsed_when_add_callback(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_callback(callback=lambda req: httpx.Response(status_code=200, json={'foo': 'bar'}))

async with httpx.AsyncClient() as client:
response = await client.get("https://test_url")
assert response.elapsed is not None
10 changes: 9 additions & 1 deletion tests/test_httpx_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,9 +1345,17 @@ def test_header_as_httpx_headers(httpx_mock: HTTPXMock) -> None:
assert dict(response.cookies) == {"key": "value"}


def test_elapsed(httpx_mock: HTTPXMock) -> None:
def test_elapsed_when_add_response(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


def test_elapsed_when_add_callback(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_callback(callback=lambda req: httpx.Response(status_code=200, json={'foo': 'bar'}))

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

0 comments on commit 8dec995

Please sign in to comment.