Skip to content

Commit 3f08a50

Browse files
authored
Merge pull request Colin-b#30 from Colin-b/develop
Release 0.9.0
2 parents e06bf46 + 01b55d2 commit 3f08a50

File tree

8 files changed

+36
-23
lines changed

8 files changed

+36
-23
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.9.0] - 2020-09-22
10+
### Changed
11+
- Requires [`httpx`](https://www.python-httpx.org)==0.15.*
12+
- Callbacks are now called with `ext` dictionary instead of `timeout`. To follow `httpcore` design changes. You can still retrieve timeout by using ```ext['timeout']```
13+
914
## [0.8.0] - 2020-08-26
1015
### Added
1116
- `non_mocked_hosts` fixture allowing to avoid mocking requests sent on some specific hosts.
@@ -99,7 +104,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99104
### Added
100105
- First release, should be considered as unstable for now as design might change.
101106

102-
[Unreleased]: https://github.com/Colin-b/pytest_httpx/compare/v0.8.0...HEAD
107+
[Unreleased]: https://github.com/Colin-b/pytest_httpx/compare/v0.9.0...HEAD
108+
[0.9.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.8.0...v0.9.0
103109
[0.8.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.7.0...v0.8.0
104110
[0.7.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.6.0...v0.7.0
105111
[0.6.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.5.0...v0.6.0

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ You can perform custom manipulation upon request reception by registering callba
336336

337337
Callback should expect at least two parameters:
338338
* request: The received [`httpx.Request`](https://www.python-httpx.org/api/#request).
339-
* timeout: The [timeouts](https://www.python-httpx.org/advanced/#timeout-configuration) linked to the request.
339+
* ext: The extensions (including the [timeouts](https://www.python-httpx.org/advanced/#timeout-configuration)) linked to the request.
340340

341341
If all callbacks are not executed during test execution, the test case will fail at teardown.
342342

@@ -352,7 +352,7 @@ def assert_all_responses_were_requested() -> bool:
352352

353353
### Dynamic responses
354354

355-
Callback should return a httpcore response (as a tuple), you can use `pytest_httpx.to_response` function to create such a tuple.
355+
Callback should return a `httpcore` response (as a tuple), you can use `pytest_httpx.to_response` function to create such a tuple.
356356

357357
```python
358358
import httpx
@@ -386,8 +386,8 @@ from pytest_httpx import HTTPXMock
386386

387387

388388
def test_exception_raising(httpx_mock: HTTPXMock):
389-
def raise_timeout(request, timeout):
390-
raise httpx.ReadTimeout(f"Unable to read within {timeout}", request=request)
389+
def raise_timeout(request, ext: dict):
390+
raise httpx.ReadTimeout(f"Unable to read within {ext['timeout']}", request=request)
391391

392392
httpx_mock.add_callback(raise_timeout)
393393

@@ -422,7 +422,7 @@ You can add criteria so that callback will be sent only in case of a more specif
422422

423423
#### Matching on URL
424424

425-
`url` parameter can either be a string, a python [re.Pattern](https://docs.python.org/3/library/re.html) instance or a [httpx.URL](https://www.python-httpx.org/api/#url) instance.
425+
`url` parameter can either be a string, a python [`re.Pattern`](https://docs.python.org/3/library/re.html) instance or a [`httpx.URL`](https://www.python-httpx.org/api/#url) instance.
426426

427427
Matching is performed on the full URL, query parameters included.
428428

@@ -616,6 +616,7 @@ Below is a list of parameters that will require a change in your code.
616616

617617
Sample adding a response with `aioresponses`:
618618
```python
619+
import pytest
619620
from aioresponses import aioresponses
620621

621622

pytest_httpx/_httpx_internals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def stream(
1616
if files:
1717
# TODO Get rid of this internal import
1818
# import is performed at runtime when needed to reduce impact of internal changes in httpx
19-
from httpx._content_streams import MultipartStream
19+
from httpx._multipart import MultipartStream
2020

2121
return MultipartStream(data=data or {}, files=files, boundary=boundary)
2222

pytest_httpx/_httpx_mock.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
TimeoutDict = Dict[str, Optional[float]]
1414

1515
Response = Tuple[
16-
bytes, int, bytes, Headers, Union[httpcore.SyncByteStream, httpcore.AsyncByteStream]
16+
int, Headers, Union[httpcore.SyncByteStream, httpcore.AsyncByteStream], dict
1717
]
1818

1919

@@ -153,7 +153,7 @@ def add_callback(self, callback: Callable, **matchers):
153153
:param callback: The callable that will be called upon reception of the matched request.
154154
It must expect at least 2 parameters:
155155
* request: The received httpx.Request.
156-
* timeout: The timeout linked to the request.
156+
* ext: The extensions linked to the request (such as timeout).
157157
It should return a valid httpcore response tuple, you can use pytest_httpx.to_response function to create one.
158158
:param url: Full URL identifying the request(s) to match.
159159
Can be a str, a re.Pattern instance or a httpx.URL instance.
@@ -169,7 +169,7 @@ def _handle_request(
169169
url: URL,
170170
headers: Headers = None,
171171
stream: Union[httpcore.SyncByteStream, httpcore.AsyncByteStream] = None,
172-
timeout: TimeoutDict = None,
172+
ext: dict = None,
173173
) -> Response:
174174
request = to_request(method, url, headers, stream)
175175
self._requests.append(request)
@@ -180,7 +180,7 @@ def _handle_request(
180180

181181
callback = self._get_callback(request)
182182
if callback:
183-
return callback(request=request, timeout=timeout)
183+
return callback(request=request, ext=ext)
184184

185185
raise httpx.TimeoutException(
186186
self._explain_that_no_response_was_found(request), request=request
@@ -325,17 +325,17 @@ def __init__(self, mock: HTTPXMock):
325325

326326
def request(
327327
self, *args, **kwargs
328-
) -> Tuple[bytes, int, bytes, List[Tuple[bytes, bytes]], httpcore.SyncByteStream]:
328+
) -> Tuple[int, List[Tuple[bytes, bytes]], httpcore.SyncByteStream, dict]:
329329
return self.mock._handle_request(*args, **kwargs)
330330

331331

332332
class _PytestAsyncTransport(httpcore.AsyncHTTPTransport):
333333
def __init__(self, mock: HTTPXMock):
334334
self.mock = mock
335335

336-
async def request(
336+
async def arequest(
337337
self, *args, **kwargs
338-
) -> Tuple[bytes, int, bytes, List[Tuple[bytes, bytes]], httpcore.AsyncByteStream]:
338+
) -> Tuple[int, List[Tuple[bytes, bytes]], httpcore.AsyncByteStream, dict]:
339339
return self.mock._handle_request(*args, **kwargs)
340340

341341

@@ -366,4 +366,4 @@ def to_response(
366366
else []
367367
)
368368
body = stream(data=data, files=files, json=json, boundary=boundary)
369-
return http_version.encode(), status_code, b"", headers, body
369+
return status_code, headers, body, {"http_version": http_version}

pytest_httpx/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
# Major should be incremented in case there is a breaking change. (eg: 2.5.8 -> 3.0.0)
44
# Minor should be incremented in case there is an enhancement. (eg: 2.5.8 -> 2.6.0)
55
# Patch should be incremented in case there is a bug fix. (eg: 2.5.8 -> 2.5.9)
6-
__version__ = "0.8.0"
6+
__version__ = "0.9.0"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
keywords=["pytest", "testing", "mock", "httpx"],
3737
packages=find_packages(exclude=["tests*"]),
3838
entry_points={"pytest11": ["pytest_httpx = pytest_httpx"]},
39-
install_requires=["httpx==0.14.*", "pytest==6.*"],
39+
install_requires=["httpx==0.15.*", "pytest==6.*"],
4040
extras_require={
4141
"testing": [
4242
# Used to run async test functions

tests/test_httpx_async.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -619,14 +619,17 @@ async def test_requests_json_body(httpx_mock: HTTPXMock):
619619

620620
@pytest.mark.asyncio
621621
async def test_callback_raising_exception(httpx_mock: HTTPXMock):
622-
def raise_timeout(request, timeout):
623-
raise httpx.ReadTimeout(f"Unable to read within {timeout}", request=request)
622+
def raise_timeout(request, ext):
623+
raise httpx.ReadTimeout(
624+
f"Unable to read within {ext['timeout']['read']}", request=request
625+
)
624626

625627
httpx_mock.add_callback(raise_timeout, url="http://test_url")
626628

627629
async with httpx.AsyncClient() as client:
628-
with pytest.raises(httpx.ReadTimeout):
630+
with pytest.raises(httpx.ReadTimeout) as exception_info:
629631
await client.get("http://test_url")
632+
assert str(exception_info.value) == "Unable to read within 5.0"
630633

631634

632635
@pytest.mark.asyncio

tests/test_httpx_sync.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,14 +578,17 @@ def test_requests_json_body(httpx_mock: HTTPXMock):
578578

579579

580580
def test_callback_raising_exception(httpx_mock: HTTPXMock):
581-
def raise_timeout(request, timeout):
582-
raise httpx.ReadTimeout(f"Unable to read within {timeout}", request=request)
581+
def raise_timeout(request, ext):
582+
raise httpx.ReadTimeout(
583+
f"Unable to read within {ext['timeout']['read']}", request=request
584+
)
583585

584586
httpx_mock.add_callback(raise_timeout, url="http://test_url")
585587

586588
with httpx.Client() as client:
587-
with pytest.raises(httpx.ReadTimeout):
589+
with pytest.raises(httpx.ReadTimeout) as exception_info:
588590
client.get("http://test_url")
591+
assert str(exception_info.value) == "Unable to read within 5.0"
589592

590593

591594
def test_callback_returning_response(httpx_mock: HTTPXMock):

0 commit comments

Comments
 (0)