File: test_transports.py

package info (click to toggle)
python-respx 0.22.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 868 kB
  • sloc: python: 4,379; makefile: 17
file content (70 lines) | stat: -rw-r--r-- 1,966 bytes parent folder | download | duplicates (2)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import warnings

import httpx
import pytest

from respx.models import PassThrough
from respx.router import Router
from respx.transports import MockTransport


def test_sync_transport_handler():
    url = "https://foo.bar/"

    router = Router(assert_all_called=False)
    router.get(url) % 404
    router.post(url).pass_through()
    router.put(url)

    with warnings.catch_warnings(record=True) as w:
        transport = MockTransport(handler=router.handler)
        assert len(w) == 1

    with httpx.Client(transport=transport) as client:
        response = client.get(url)
        assert response.status_code == 404
        with pytest.raises(PassThrough):
            client.post(url)


async def test_async_transport_handler():
    url = "https://foo.bar/"

    router = Router(assert_all_called=False)
    router.get(url) % 404
    router.post(url).pass_through()
    router.put(url)

    with warnings.catch_warnings(record=True) as w:
        transport = MockTransport(async_handler=router.async_handler)
        assert len(w) == 1

    async with httpx.AsyncClient(transport=transport) as client:
        response = await client.get(url)
        assert response.status_code == 404
        with pytest.raises(PassThrough):
            await client.post(url)


async def test_transport_assertions():
    url = "https://foo.bar/"

    router = Router(assert_all_called=True)
    router.get(url) % 404
    router.post(url) % dict(json={"foo": "bar"})

    with warnings.catch_warnings(record=True) as w:
        transport = MockTransport(router=router)
        assert len(w) == 1

    with pytest.raises(  # noqa [PT012]
        AssertionError, match="some routes were not called"
    ):
        async with httpx.AsyncClient(transport=transport) as client:
            response = await client.get(url)
            assert response.status_code == 404


def test_required_kwarg():
    with pytest.raises(RuntimeError, match="argument"):
        MockTransport()