File: aiohttp_test.py

package info (click to toggle)
python-pook 2.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 672 kB
  • sloc: python: 3,558; makefile: 13
file content (116 lines) | stat: -rw-r--r-- 3,845 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import aiohttp
import pytest

import pook
from tests.unit.fixtures import BINARY_FILE
from tests.unit.interceptors.base import StandardTests

pytestmark = [pytest.mark.pook]


class TestStandardAiohttp(StandardTests):
    is_async = True

    async def amake_request(self, method, url, content=None, headers=None):
        async with aiohttp.ClientSession(loop=self.loop) as session:
            response = await session.request(
                method=method, url=url, data=content, headers=headers
            )
            response_content = await response.read()
            return response.status, response_content, response.headers


def _pook_url(URL):
    return pook.head(URL).reply(200).mock


@pytest.fixture
def URL(httpbin):
    return f"{httpbin.url}/status/404"


@pytest.mark.asyncio
async def test_async_with_request(URL):
    mock = _pook_url(URL)
    async with aiohttp.ClientSession() as session:
        async with session.head(URL) as req:
            assert req.status == 200

    assert len(mock.matches) == 1


@pytest.mark.asyncio
async def test_await_request(URL):
    mock = _pook_url(URL)
    async with aiohttp.ClientSession() as session:
        req = await session.head(URL)
        assert req.status == 200

    assert len(mock.matches) == 1


@pytest.mark.asyncio
async def test_binary_body(URL):
    pook.get(URL).reply(200).body(BINARY_FILE)
    async with aiohttp.ClientSession() as session:
        req = await session.get(URL)
        assert await req.read() == BINARY_FILE


@pytest.mark.asyncio
async def test_json_matcher_json_payload(URL):
    payload = {"foo": "bar"}
    pook.post(URL).json(payload).reply(200).body(BINARY_FILE)
    async with aiohttp.ClientSession() as session:
        req = await session.post(URL, json=payload)
        assert await req.read() == BINARY_FILE


@pytest.mark.asyncio
async def test_client_base_url(httpbin):
    """Client base url should be matched."""
    pook.get(httpbin + "/status/404").reply(200).body("hello from pook")
    async with aiohttp.ClientSession(base_url=httpbin.url) as session:
        res = await session.get("/status/404")
        assert res.status == 200
        assert await res.read() == b"hello from pook"


@pytest.mark.asyncio
async def test_client_headers(httpbin):
    """Headers set on the client should be matched."""
    pook.get(httpbin + "/status/404").header("x-pook", "hello").reply(200).body(
        "hello from pook"
    )
    async with aiohttp.ClientSession(headers={"x-pook": "hello"}) as session:
        res = await session.get(httpbin + "/status/404")
        assert res.status == 200
        assert await res.read() == b"hello from pook"


@pytest.mark.asyncio
async def test_client_headers_merged(httpbin):
    """Headers set on the client should be matched even if request-specific headers are sent."""
    pook.get(httpbin + "/status/404").header("x-pook", "hello").reply(200).body(
        "hello from pook"
    )
    async with aiohttp.ClientSession(headers={"x-pook": "hello"}) as session:
        res = await session.get(
            httpbin + "/status/404", headers={"x-pook-secondary": "xyz"}
        )
        assert res.status == 200
        assert await res.read() == b"hello from pook"


@pytest.mark.asyncio
async def test_client_headers_both_session_and_request(httpbin):
    """Headers should be matchable from both the session and request in the same matcher"""
    pook.get(httpbin + "/status/404").header("x-pook-session", "hello").header(
        "x-pook-request", "hey"
    ).reply(200).body("hello from pook")
    async with aiohttp.ClientSession(headers={"x-pook-session": "hello"}) as session:
        res = await session.get(
            httpbin + "/status/404", headers={"x-pook-request": "hey"}
        )
        assert res.status == 200
        assert await res.read() == b"hello from pook"