Skip to content
Merged
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
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.6.0] - 2020-08-07
### Changed
- Requires [`httpx`](https://www.python-httpx.org)==0.14.*

## [0.5.0] - 2020-07-31
### Changed
- requires [`pytest`](https://docs.pytest.org/en/latest/) 6.
Expand Down Expand Up @@ -84,7 +88,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.5.0...HEAD
[Unreleased]: https://github.com/Colin-b/pytest_httpx/compare/v0.6.0...HEAD
[0.6.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.5.0...v0.6.0
[0.5.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.4.0...v0.5.0
[0.4.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.3.0...v0.4.0
[0.3.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.2.1...v0.3.0
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ from pytest_httpx import HTTPXMock


def test_headers_matching(httpx_mock: HTTPXMock):
httpx_mock.add_response(match_headers={'user-agent': 'python-httpx/0.11.1'})
httpx_mock.add_response(match_headers={'user-agent': 'python-httpx/0.14.0'})

with httpx.Client() as client:
response = client.get("http://test_url")
Expand Down Expand Up @@ -382,8 +382,8 @@ from pytest_httpx import HTTPXMock


def test_exception_raising(httpx_mock: HTTPXMock):
def raise_timeout(*args, **kwargs):
raise httpx.ReadTimeout()
def raise_timeout(request, timeout):
raise httpx.ReadTimeout(f"Unable to read within {timeout}", request=request)

httpx_mock.add_callback(raise_timeout)

Expand Down
4 changes: 2 additions & 2 deletions pytest_httpx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def httpx_mock(monkeypatch, assert_all_responses_were_requested: bool) -> HTTPXM
mock = HTTPXMock()
# Mock synchronous requests
monkeypatch.setattr(
httpx.Client, "transport_for_url", lambda self, url: _PytestSyncTransport(mock)
httpx.Client, "_transport_for_url", lambda self, url: _PytestSyncTransport(mock)
)
# Mock asynchronous requests
monkeypatch.setattr(
httpx.AsyncClient,
"transport_for_url",
"_transport_for_url",
lambda self, url: _PytestAsyncTransport(mock),
)
yield mock
Expand Down
23 changes: 5 additions & 18 deletions pytest_httpx/_httpx_internals.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
from typing import Union, Any, Iterator, AsyncIterator
from typing import Union, Any
from json import dumps

import httpcore


class ByteStream(httpcore.AsyncByteStream, httpcore.SyncByteStream):
def __init__(self, data: bytes):
httpcore.AsyncByteStream.__init__(self)
httpcore.SyncByteStream.__init__(self)
self.data = data

def __iter__(self) -> Iterator[bytes]:
yield self.data

async def __aiter__(self) -> AsyncIterator[bytes]:
yield self.data


class IteratorStream(httpcore.AsyncByteStream, httpcore.SyncByteStream):
class IteratorStream(httpcore.AsyncIteratorByteStream, httpcore.IteratorByteStream):
def __init__(self, iterator):
httpcore.AsyncByteStream.__init__(self, aiterator=iterator)
httpcore.SyncByteStream.__init__(self, iterator=iterator)
httpcore.AsyncIteratorByteStream.__init__(self, aiterator=iterator)
httpcore.IteratorByteStream.__init__(self, iterator=iterator)


def stream(
Expand All @@ -41,6 +28,6 @@ def stream(
data = b""

if isinstance(data, bytes):
return ByteStream(data)
return httpcore.PlainByteStream(data)

return IteratorStream(data)
2 changes: 1 addition & 1 deletion pytest_httpx/_httpx_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def to_request(
stream: Union[httpcore.SyncByteStream, httpcore.AsyncByteStream] = None,
) -> httpx.Request:
scheme, host, port, path = url
port = f":{port}" if port not in [80, 443] else ""
port = f":{port}" if port not in [80, 443, None] else ""
path = path.decode() if path != b"/" else ""
if path.startswith("/?"):
path = path[1:]
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.5.0"
__version__ = "0.6.0"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
keywords=["pytest", "testing", "mock", "httpx"],
packages=find_packages(exclude=["tests*"]),
entry_points={"pytest11": ["pytest_httpx = pytest_httpx"]},
install_requires=["httpx==0.13.*", "pytest==6.*"],
install_requires=["httpx==0.14.*", "pytest==6.*"],
extras_require={
"testing": [
# Used to run async test functions
Expand Down
4 changes: 2 additions & 2 deletions tests/test_httpx_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,8 @@ async def test_requests_json_body(httpx_mock: HTTPXMock):

@pytest.mark.asyncio
async def test_callback_raising_exception(httpx_mock: HTTPXMock):
def raise_timeout(*args, **kwargs):
raise httpx.ReadTimeout()
def raise_timeout(request, timeout):
raise httpx.ReadTimeout(f"Unable to read within {timeout}", request=request)

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

Expand Down
4 changes: 2 additions & 2 deletions tests/test_httpx_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,8 @@ def test_requests_json_body(httpx_mock: HTTPXMock):


def test_callback_raising_exception(httpx_mock: HTTPXMock):
def raise_timeout(*args, **kwargs):
raise httpx.ReadTimeout()
def raise_timeout(request, timeout):
raise httpx.ReadTimeout(f"Unable to read within {timeout}", request=request)

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

Expand Down