File: test_async.py

package info (click to toggle)
python-pytest-socket 0.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 296 kB
  • sloc: python: 663; makefile: 37
file content (52 lines) | stat: -rw-r--r-- 1,412 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from conftest import unix_sockets_only
from tests.common import assert_socket_blocked


@unix_sockets_only
def test_starlette(testdir):
    testdir.makepyfile(
        """
        from pytest_socket import disable_socket
        from starlette.responses import HTMLResponse
        from starlette.testclient import TestClient


        def pytest_runtest_setup():
            disable_socket()


        async def app(scope, receive, send):
            assert scope['type'] == 'http'
            response = HTMLResponse('<html><body>Hello, world!</body></html>')
            await response(scope, receive, send)


        def test_app():
            client = TestClient(app)
            response = client.get('/')
            assert response.status_code == 200
        """
    )
    result = testdir.runpytest("--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("--disable-socket", "--allow-unix-socket")
    assert_socket_blocked(result)