From e8ddd80886b03cafa9800e9f94f9107572e4abd7 Mon Sep 17 00:00:00 2001 From: Olli Paakkunainen Date: Thu, 25 Nov 2021 15:23:08 +0900 Subject: [PATCH] Fix pytest-socket raises IndexError with httpx --- pyproject.toml | 3 ++- pytest_socket.py | 2 +- tests/test_async.py | 21 ++++++++++++++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5828d45..0e9734a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,7 @@ classifiers = [ ] [tool.poetry.dependencies] -python = "^3.6" +python = "^3.6.2" pytest = ">=3.6.3" [tool.poetry.dev-dependencies] @@ -42,6 +42,7 @@ pytest-flake8 = "^1.0.4" asynctest = "^0.13.0" requests = "^2.26.0" starlette = "^0.14.2" +httpx = "^0.21.1" [tool.poetry.plugins.pytest11] socket = 'pytest_socket' diff --git a/pytest_socket.py b/pytest_socket.py index 5e4ca3c..053c18d 100644 --- a/pytest_socket.py +++ b/pytest_socket.py @@ -87,7 +87,7 @@ class GuardedSocket(socket.socket): def __new__(cls, *args, **kwargs): try: is_unix_socket = args[0] == socket.AF_UNIX - except AttributeError: + except (AttributeError, IndexError): # AF_UNIX not supported on Windows https://bugs.python.org/issue33408 is_unix_socket = False diff --git a/tests/test_async.py b/tests/test_async.py index 963ba73..e521e59 100644 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -1,7 +1,7 @@ import socket import pytest - +from test_socket import assert_socket_blocked unix_sockets_only = pytest.mark.skipif( not hasattr(socket, "AF_UNIX"), @@ -54,3 +54,22 @@ def test_app(): """) result = testdir.runpytest("--verbose", "--disable-socket", "--allow-unix-socket") result.assert_outcomes(passed=1, skipped=0, failed=0) + + +@unix_sockets_only +def test_httpx_fails(testdir): + testdir.makepyfile(""" + import pytest + import httpx + + + @pytest.fixture(autouse=True) + def anyio_backend(): + return "asyncio" + + async def test_httpx(): + async with httpx.AsyncClient() as client: + await client.get("http://www.example.com/") + """) + result = testdir.runpytest("--verbose", "--disable-socket", "--allow-unix-socket") + assert_socket_blocked(result)