|
1 | 1 | import asyncio |
| 2 | +import glob |
| 3 | +import io |
2 | 4 | import json |
3 | | -from unittest import TestCase |
| 5 | +import shutil |
4 | 6 | import socket |
5 | | -import io |
| 7 | +import tempfile |
| 8 | +from unittest import TestCase |
6 | 9 |
|
7 | | -from mocket.mocket import Mocket, mocketize |
| 10 | +from mocket.mocket import mocketize |
8 | 11 |
|
9 | 12 |
|
10 | 13 | class AsyncIoRecordTestCase(TestCase): |
| 14 | + temp_dir = tempfile.mkdtemp() |
| 15 | + |
| 16 | + @mocketize(truesocket_recording_dir=temp_dir) |
11 | 17 | def test_asyncio_record_replay(self): |
12 | 18 | async def test_asyncio_connection(): |
13 | | - mock_out = b'HTTP/1.1 301 Moved Permanently\r\n' |
14 | 19 | reader, writer = await asyncio.open_connection( |
15 | | - host='google.com', |
| 20 | + host="google.com", |
16 | 21 | port=80, |
17 | 22 | family=socket.AF_INET, |
18 | 23 | proto=socket.IPPROTO_TCP, |
19 | 24 | ssl=None, |
20 | 25 | server_hostname=None, |
21 | 26 | ) |
22 | 27 |
|
23 | | - buf = 'GET / HTTP/1.1\r\nHost: google.com\r\n\r\n' |
| 28 | + buf = "GET / HTTP/1.1\r\nHost: google.com\r\n\r\n" |
24 | 29 | writer.write(buf.encode()) |
25 | 30 | await writer.drain() |
26 | 31 |
|
27 | | - r = await reader.readline() |
| 32 | + await reader.readline() |
28 | 33 | writer.close() |
29 | 34 | await writer.wait_closed() |
30 | | - |
31 | | - mock_out = b'HTTP/1.1 301 Moved Permanently\r\n' |
32 | | - |
33 | | - test_name = 'test_asyncio_record' |
34 | | - # This enables mocket to record the response |
35 | | - Mocket.enable(test_name, ".") |
36 | 35 |
|
37 | 36 | loop = asyncio.get_event_loop() |
38 | 37 | loop.set_debug(True) |
39 | 38 | loop.run_until_complete(test_asyncio_connection()) |
40 | 39 |
|
41 | | - dump_filename = f'./{test_name}.json' |
42 | | - with io.open(dump_filename) as f: |
| 40 | + files = glob.glob(f"{self.temp_dir}/*.json") |
| 41 | + self.assertEqual(len(files), 1) |
| 42 | + |
| 43 | + with io.open(files[0]) as f: |
43 | 44 | responses = json.load(f) |
44 | 45 |
|
45 | | - assert len(responses["google.com"]["80"].keys()) == 1 |
| 46 | + self.assertEqual(len(responses["google.com"]["80"].keys()), 1) |
| 47 | + |
| 48 | + shutil.rmtree(self.temp_dir) |
0 commit comments