11import json
2+ import sys
23
34import aiohttp
5+ import pytest
6+ from asgiref .sync import async_to_sync
47
58from mocket .mocket import Mocket , mocketize
69from mocket .mockhttp import Entry
7- from mocket .plugins .httpretty import HTTPretty , httprettified
10+ from mocket .plugins .httpretty import httprettified , httpretty
811
912timeout = aiohttp .ClientTimeout (total = 3 )
1013
@@ -16,8 +19,9 @@ def test_http_session(event_loop):
1619 Entry .single_register (Entry .GET , url , body = body , status = 404 )
1720 Entry .single_register (Entry .POST , url , body = body * 2 , status = 201 )
1821
19- async def main (_loop ):
20- async with aiohttp .ClientSession (loop = _loop , timeout = timeout ) as session :
22+ @async_to_sync
23+ async def perform_aiohttp_transactions ():
24+ async with aiohttp .ClientSession (timeout = timeout ) as session :
2125 async with session .get (url ) as get_response :
2226 assert get_response .status == 404
2327 assert await get_response .text () == body
@@ -28,19 +32,21 @@ async def main(_loop):
2832 assert Mocket .last_request ().method == "POST"
2933 assert Mocket .last_request ().body == body * 6
3034
31- event_loop . run_until_complete ( main ( event_loop ) )
35+ perform_aiohttp_transactions ( )
3236 assert len (Mocket .request_list ()) == 2
3337
3438
39+ @pytest .mark .skipif (sys .version_info >= (3 , 11 ), reason = "Failing with Python 3.11" )
3540@mocketize
3641def test_https_session (event_loop ):
3742 url = "https://httpbin.org/ip"
3843 body = "asd" * 100
3944 Entry .single_register (Entry .GET , url , body = body , status = 404 )
4045 Entry .single_register (Entry .POST , url , body = body * 2 , status = 201 )
4146
42- async def main (_loop ):
43- async with aiohttp .ClientSession (loop = _loop , timeout = timeout ) as session :
47+ @async_to_sync
48+ async def perform_aiohttp_transactions ():
49+ async with aiohttp .ClientSession (timeout = timeout ) as session :
4450 async with session .get (url ) as get_response :
4551 assert get_response .status == 404
4652 assert await get_response .text () == body
@@ -49,23 +55,26 @@ async def main(_loop):
4955 assert post_response .status == 201
5056 assert await post_response .text () == body * 2
5157
52- event_loop . run_until_complete ( main ( event_loop ) )
58+ perform_aiohttp_transactions ( )
5359 assert len (Mocket .request_list ()) == 2
5460
5561
62+ @pytest .mark .skipif (sys .version_info >= (3 , 11 ), reason = "Failing with Python 3.11" )
5663@httprettified
5764def test_httprettish_session (event_loop ):
5865 url = "https://httpbin.org/ip"
59- HTTPretty .register_uri (
60- HTTPretty .GET ,
66+ httpretty .register_uri (
67+ httpretty .GET ,
6168 url ,
6269 body = json .dumps (dict (origin = "127.0.0.1" )),
6370 )
6471
65- async def main (_loop ):
66- async with aiohttp .ClientSession (loop = _loop , timeout = timeout ) as session :
72+ @async_to_sync
73+ async def perform_aiohttp_transactions ():
74+ async with aiohttp .ClientSession (timeout = timeout ) as session :
6775 async with session .get (url ) as get_response :
6876 assert get_response .status == 200
6977 assert await get_response .text () == '{"origin": "127.0.0.1"}'
7078
71- event_loop .run_until_complete (main (event_loop ))
79+ perform_aiohttp_transactions ()
80+ assert len (httpretty .latest_requests ) == 1
0 commit comments