|
1 | 1 | import asyncio
|
2 | 2 | import json
|
3 |
| -import platform |
4 | 3 | from unittest import TestCase
|
5 | 4 |
|
6 | 5 | import aiohttp
|
7 |
| -import async_timeout |
8 | 6 |
|
9 | 7 | from mocket.mocket import Mocket, mocketize
|
10 | 8 | from mocket.mockhttp import Entry
|
11 | 9 | from mocket.plugins.httpretty import HTTPretty, httprettified
|
12 | 10 |
|
13 |
| -if not platform.python_version().startswith("3.11."): |
14 |
| - # Python 3.11 needs async decorators, or aiohttp |
15 |
| - # will fail with "Cannot write to closing transport" |
16 |
| - class AioHttpEntryTestCase(TestCase): |
17 |
| - @mocketize |
18 |
| - def test_http_session(self): |
19 |
| - url = "http://httpbin.org/ip" |
20 |
| - body = "asd" * 100 |
21 |
| - Entry.single_register(Entry.GET, url, body=body, status=404) |
22 |
| - Entry.single_register(Entry.POST, url, body=body * 2, status=201) |
23 | 11 |
|
24 |
| - async def main(_loop): |
25 |
| - async with aiohttp.ClientSession(loop=_loop) as session: |
26 |
| - async with async_timeout.timeout(3): |
27 |
| - async with session.get(url) as get_response: |
28 |
| - assert get_response.status == 404 |
29 |
| - assert await get_response.text() == body |
| 12 | +class AioHttpEntryTestCase(TestCase): |
| 13 | + timeout = aiohttp.ClientTimeout(total=3) |
30 | 14 |
|
31 |
| - async with async_timeout.timeout(3): |
32 |
| - async with session.post(url, data=body * 6) as post_response: |
33 |
| - assert post_response.status == 201 |
34 |
| - assert await post_response.text() == body * 2 |
35 |
| - assert Mocket.last_request().method == "POST" |
36 |
| - assert Mocket.last_request().body == body * 6 |
| 15 | + @mocketize |
| 16 | + def test_http_session(self): |
| 17 | + url = "http://httpbin.org/ip" |
| 18 | + body = "asd" * 100 |
| 19 | + Entry.single_register(Entry.GET, url, body=body, status=404) |
| 20 | + Entry.single_register(Entry.POST, url, body=body * 2, status=201) |
37 | 21 |
|
38 |
| - loop = asyncio.new_event_loop() |
39 |
| - loop.set_debug(True) |
40 |
| - loop.run_until_complete(main(loop)) |
41 |
| - self.assertEqual(len(Mocket.request_list()), 2) |
| 22 | + async def main(_loop): |
| 23 | + async with aiohttp.ClientSession( |
| 24 | + loop=_loop, timeout=self.timeout |
| 25 | + ) as session: |
| 26 | + async with session.get(url) as get_response: |
| 27 | + assert get_response.status == 404 |
| 28 | + assert await get_response.text() == body |
42 | 29 |
|
43 |
| - @mocketize |
44 |
| - def test_https_session(self): |
45 |
| - url = "https://httpbin.org/ip" |
46 |
| - body = "asd" * 100 |
47 |
| - Entry.single_register(Entry.GET, url, body=body, status=404) |
48 |
| - Entry.single_register(Entry.POST, url, body=body * 2, status=201) |
| 30 | + async with session.post(url, data=body * 6) as post_response: |
| 31 | + assert post_response.status == 201 |
| 32 | + assert await post_response.text() == body * 2 |
| 33 | + assert Mocket.last_request().method == "POST" |
| 34 | + assert Mocket.last_request().body == body * 6 |
49 | 35 |
|
50 |
| - async def main(_loop): |
51 |
| - async with aiohttp.ClientSession(loop=_loop) as session: |
52 |
| - async with async_timeout.timeout(3): |
53 |
| - async with session.get(url) as get_response: |
54 |
| - assert get_response.status == 404 |
55 |
| - assert await get_response.text() == body |
| 36 | + loop = asyncio.new_event_loop() |
| 37 | + loop.set_debug(True) |
| 38 | + loop.run_until_complete(main(loop)) |
| 39 | + self.assertEqual(len(Mocket.request_list()), 2) |
56 | 40 |
|
57 |
| - async with async_timeout.timeout(3): |
58 |
| - async with session.post(url, data=body * 6) as post_response: |
59 |
| - assert post_response.status == 201 |
60 |
| - assert await post_response.text() == body * 2 |
| 41 | + @mocketize |
| 42 | + def test_https_session(self): |
| 43 | + url = "https://httpbin.org/ip" |
| 44 | + body = "asd" * 100 |
| 45 | + Entry.single_register(Entry.GET, url, body=body, status=404) |
| 46 | + Entry.single_register(Entry.POST, url, body=body * 2, status=201) |
61 | 47 |
|
62 |
| - loop = asyncio.new_event_loop() |
63 |
| - loop.set_debug(True) |
64 |
| - loop.run_until_complete(main(loop)) |
65 |
| - self.assertEqual(len(Mocket.request_list()), 2) |
| 48 | + async def main(_loop): |
| 49 | + async with aiohttp.ClientSession( |
| 50 | + loop=_loop, timeout=self.timeout |
| 51 | + ) as session: |
| 52 | + async with session.get(url) as get_response: |
| 53 | + assert get_response.status == 404 |
| 54 | + assert await get_response.text() == body |
66 | 55 |
|
67 |
| - @httprettified |
68 |
| - def test_httprettish_session(self): |
69 |
| - url = "https://httpbin.org/ip" |
70 |
| - HTTPretty.register_uri( |
71 |
| - HTTPretty.GET, |
72 |
| - url, |
73 |
| - body=json.dumps(dict(origin="127.0.0.1")), |
74 |
| - ) |
| 56 | + async with session.post(url, data=body * 6) as post_response: |
| 57 | + assert post_response.status == 201 |
| 58 | + assert await post_response.text() == body * 2 |
75 | 59 |
|
76 |
| - async def main(_loop): |
77 |
| - async with aiohttp.ClientSession(loop=_loop) as session: |
78 |
| - async with async_timeout.timeout(3): |
79 |
| - async with session.get(url) as get_response: |
80 |
| - assert get_response.status == 200 |
81 |
| - assert ( |
82 |
| - await get_response.text() == '{"origin": "127.0.0.1"}' |
83 |
| - ) |
| 60 | + loop = asyncio.new_event_loop() |
| 61 | + loop.set_debug(True) |
| 62 | + loop.run_until_complete(main(loop)) |
| 63 | + self.assertEqual(len(Mocket.request_list()), 2) |
84 | 64 |
|
85 |
| - loop = asyncio.new_event_loop() |
86 |
| - loop.set_debug(True) |
87 |
| - loop.run_until_complete(main(loop)) |
| 65 | + @httprettified |
| 66 | + def test_httprettish_session(self): |
| 67 | + url = "https://httpbin.org/ip" |
| 68 | + HTTPretty.register_uri( |
| 69 | + HTTPretty.GET, |
| 70 | + url, |
| 71 | + body=json.dumps(dict(origin="127.0.0.1")), |
| 72 | + ) |
| 73 | + |
| 74 | + async def main(_loop): |
| 75 | + async with aiohttp.ClientSession( |
| 76 | + loop=_loop, timeout=self.timeout |
| 77 | + ) as session: |
| 78 | + async with session.get(url) as get_response: |
| 79 | + assert get_response.status == 200 |
| 80 | + assert await get_response.text() == '{"origin": "127.0.0.1"}' |
| 81 | + |
| 82 | + loop = asyncio.new_event_loop() |
| 83 | + loop.set_debug(True) |
| 84 | + loop.run_until_complete(main(loop)) |
0 commit comments