-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtest_asyncio.py
69 lines (54 loc) · 1.83 KB
/
test_asyncio.py
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
import asyncio
import glob
import json
import socket
import tempfile
import aiohttp
import pytest
from mocket import Mocketizer, async_mocketize
from mocket.mockhttp import Entry
from mocket.plugins.aiohttp_connector import MocketTCPConnector
def test_asyncio_record_replay(event_loop):
async def test_asyncio_connection():
reader, writer = await asyncio.open_connection(
host="google.com",
port=80,
family=socket.AF_INET,
proto=socket.IPPROTO_TCP,
ssl=None,
server_hostname=None,
)
buf = "GET / HTTP/1.1\r\nHost: google.com\r\n\r\n"
writer.write(buf.encode())
await writer.drain()
await reader.readline()
writer.close()
await writer.wait_closed()
with tempfile.TemporaryDirectory() as temp_dir:
with Mocketizer(truesocket_recording_dir=temp_dir):
event_loop.run_until_complete(test_asyncio_connection())
files = glob.glob(f"{temp_dir}/*.json")
assert len(files) == 1
with open(files[0]) as f:
responses = json.load(f)
assert len(responses["google.com"]["80"].keys()) == 1
@pytest.mark.asyncio
@async_mocketize
async def test_aiohttp():
"""
The alternative to using the custom `connector` would be importing
`aiohttp` when Mocket is already in control (inside the decorated test).
"""
url = "https://bar.foo/"
data = {"message": "Hello"}
Entry.single_register(
Entry.GET,
url,
body=json.dumps(data),
headers={"content-type": "application/json"},
)
async with aiohttp.ClientSession(
timeout=aiohttp.ClientTimeout(total=3), connector=MocketTCPConnector()
) as session, session.get(url) as response:
response = await response.json()
assert response == data