Skip to content

Commit

Permalink
Merge pull request #1 from adjust/kusaku/upgrade_httpcore
Browse files Browse the repository at this point in the history
  • Loading branch information
SlavaSkvortsov authored Nov 16, 2021
2 parents 05b0772 + edbacf0 commit 116b7de
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 55 deletions.
51 changes: 13 additions & 38 deletions pytest_httpx_blockage/blockage.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,35 @@
from contextlib import contextmanager
from typing import Any, Dict, Generator, Tuple
from typing import Generator
from unittest.mock import patch

from httpcore import AsyncByteStream, SyncByteStream
from httpcore import Request, Response
from httpcore._async.connection import AsyncHTTPConnection
from httpcore._sync.connection import SyncHTTPConnection
from httpcore._types import URL, Headers
from httpcore._sync.connection import HTTPConnection

from pytest_httpx_blockage.contextvar import is_blockage_enabled
from pytest_httpx_blockage.exceptions import RequestBlockageException

base_request_sync = SyncHTTPConnection.handle_request
base_request_sync = HTTPConnection.handle_request
base_request_async = AsyncHTTPConnection.handle_async_request


def side_effect(
self: SyncHTTPConnection,
method: bytes,
url: URL,
*args: Any,
**kwargs: Any,
) -> Tuple[int, Headers, SyncByteStream, Dict[Any, Any]]:
def side_effect(self: HTTPConnection, request: Request) -> Response:
if is_blockage_enabled.get():
raise RequestBlockageException(f'Unmocked "{method.decode()}" request to host="{url}"')
raise RequestBlockageException(f'Unmocked "{request.method.decode()}" request to host="{request.url}"')
else:
return base_request_sync(
self,
method,
url,
*args,
**kwargs,
)


async def async_side_effect(
self: AsyncHTTPConnection,
method: bytes,
url: URL,
*args: Any,
**kwargs: Any,
) -> Tuple[int, Headers, AsyncByteStream, Dict[Any, Any]]:
return base_request_sync(self, request)


async def async_side_effect(self: AsyncHTTPConnection, request: Request) -> Response:
if is_blockage_enabled.get():
raise RequestBlockageException(f'Unmocked "{method.decode()}" request to host="{url}"')
raise RequestBlockageException(f'Unmocked "{request.method.decode()}" request to host="{request.url}"')
else:
return await base_request_async(
self,
method,
url,
*args,
**kwargs,
)
return await base_request_async(self, request)


@contextmanager
def blockage() -> Generator[None, None, None]:
patch_sync = patch.object(SyncHTTPConnection, 'handle_request', autospec=True)
patch_sync = patch.object(HTTPConnection, 'handle_request', autospec=True)
patch_async = patch.object(AsyncHTTPConnection, 'handle_async_request', autospec=True)

with patch_sync as mocked_sync, patch_async as mocked_async:
Expand Down
26 changes: 13 additions & 13 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
-r requirements.txt

pytest-asyncio==0.15.1
respx==0.17.0
pytest-asyncio==0.16.0
respx==0.19.0

isort==5.8.0
mypy==0.812
isort==5.10.1
mypy==0.910
safety==1.10.3
cognitive-complexity==1.2.0

mccabe==0.6.1
flake8==3.9.1
flake8==4.0.1
flake8-blind-except==0.2.0
flake8-broken-line==0.3.0
flake8-bugbear==21.4.3
flake8-broken-line==0.4.0
flake8-bugbear==21.9.2
flake8-builtins==1.5.3
flake8-class-attributes-order==0.1.2
flake8-cognitive-complexity==0.1.0
flake8-commas==2.0.0
flake8-comprehensions==3.4.0
flake8-commas==2.1.0
flake8-comprehensions==3.7.0
flake8-debugger==4.0.0
flake8-eradicate==1.0.0
flake8-eradicate==1.2.0
flake8-functions==0.0.6
flake8-isort==4.0.0
flake8-isort==4.1.1
flake8-mock==0.3
flake8-mutable==1.2.0
flake8-print==4.0.0
flake8-pytest==1.3
flake8-pytest-style==1.4.1
flake8-quotes==3.2.0
flake8-pytest-style==1.5.1
flake8-quotes==3.3.1
flake8-string-format==0.3.0
flake8-variables-names==0.0.4
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
httpx==0.18.0
pytest==6.2.3
httpx==0.21.0
pytest==6.2.5
2 changes: 1 addition & 1 deletion tests/examples/test_disabled.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from httpx import Client

URL = 'https://google.com'
URL = 'https://httpbin.org/status/200'


def test_integration() -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/test_enabled.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pytest_httpx_blockage.exceptions import RequestBlockageException

URL = 'https://google.com'
URL = 'https://httpbin.org/status/200'


@pytest.mark.asyncio()
Expand Down

0 comments on commit 116b7de

Please sign in to comment.