1
1
# -*- coding: utf-8 -*-
2
2
import asyncio
3
3
import re
4
+ from asyncio import CancelledError , TimeoutError
4
5
from typing import Coroutine , Generator , Union
5
- from unittest import IsolatedAsyncioTestCase , skipIf
6
6
from unittest .mock import patch
7
7
8
8
from aiohttp import hdrs
9
9
from aiohttp import http
10
10
from aiohttp .client import ClientSession
11
11
from aiohttp .client_reqrep import ClientResponse
12
12
from ddt import ddt , data
13
- from asyncio import CancelledError , TimeoutError
13
+
14
14
try :
15
15
from aiohttp .errors import (
16
16
ClientConnectionError ,
24
24
)
25
25
from aiohttp .http_exceptions import HttpProcessingError
26
26
27
- from aioresponses .compat import AIOHTTP_VERSION , URL
27
+ from aioresponses .compat import (
28
+ AIOHTTP_VERSION , URL ,
29
+ fail_on ,
30
+ skipIf ,
31
+ AsyncTestCase
32
+ )
33
+
28
34
from aioresponses import CallbackResult , aioresponses
29
35
30
36
31
37
@ddt
32
- class AIOResponsesTestCase (IsolatedAsyncioTestCase ):
38
+ class AIOResponsesTestCase (AsyncTestCase ):
33
39
34
- async def asyncSetUp (self ):
40
+ async def setup (self ):
35
41
self .url = 'http://example.com/api?foo=bar#fragment'
36
42
self .session = ClientSession ()
37
- self .loop = asyncio .get_event_loop ()
38
- super ().setUp ()
39
43
40
- async def asyncTearDown (self ):
44
+ async def teardown (self ):
41
45
close_result = self .session .close ()
42
46
if close_result is not None :
43
47
await close_result
44
- super ().tearDown ()
45
48
46
49
def run_async (self , coroutine : Union [Coroutine , Generator ]):
47
50
return self .loop .run_until_complete (coroutine )
48
51
49
52
async def request (self , url : str ):
50
- return ( await self .session .get (url ) )
53
+ return await self .session .get (url )
51
54
52
55
@data (
53
56
hdrs .METH_HEAD ,
@@ -59,6 +62,7 @@ async def request(self, url: str):
59
62
hdrs .METH_OPTIONS ,
60
63
)
61
64
@patch ('aioresponses.aioresponses.add' )
65
+ @fail_on (unused_loop = False )
62
66
def test_shortcut_method (self , http_method , mocked ):
63
67
with aioresponses () as m :
64
68
getattr (m , http_method .lower ())(self .url )
@@ -194,6 +198,7 @@ async def foo(mocked):
194
198
195
199
await foo ()
196
200
201
+ @fail_on (unused_loop = False )
197
202
def test_mocking_as_decorator_wrong_mocked_arg_name (self ):
198
203
@aioresponses (param = 'foo' )
199
204
def foo (bar ):
@@ -310,7 +315,8 @@ def non_deep_copyable():
310
315
request = m .requests [key ][0 ]
311
316
self .assertEqual (request .args , tuple ())
312
317
self .assertEqual (request .kwargs ,
313
- {'allow_redirects' : True , "data" : generator_value })
318
+ {'allow_redirects' : True ,
319
+ "data" : generator_value })
314
320
315
321
async def test_request_retrieval_in_case_no_response (self ):
316
322
with aioresponses () as m :
@@ -359,7 +365,9 @@ async def test_pass_through_with_origin_params(self):
359
365
async def doit (params ):
360
366
# we have to hit actual url,
361
367
# otherwise we do not test pass through option properly
362
- ext_rep = (await self .session .get (URL (external_api ), params = params ))
368
+ ext_rep = await self .session .get (
369
+ URL (external_api ), params = params
370
+ )
363
371
return ext_rep
364
372
365
373
with aioresponses (passthrough = [external_api ]) as m :
@@ -475,7 +483,7 @@ async def test_exception_requests_are_tracked(self, mocked):
475
483
self .assertEqual (request .kwargs , kwargs )
476
484
477
485
478
- class AIOResponsesRaiseForStatusSessionTestCase (IsolatedAsyncioTestCase ):
486
+ class AIOResponsesRaiseForStatusSessionTestCase (AsyncTestCase ):
479
487
"""Test case for sessions with raise_for_status=True.
480
488
481
489
This flag, introduced in aiohttp v2.0.0, automatically calls
@@ -485,17 +493,14 @@ class AIOResponsesRaiseForStatusSessionTestCase(IsolatedAsyncioTestCase):
485
493
486
494
"""
487
495
488
-
489
- async def asyncSetUp (self ):
496
+ async def setup (self ):
490
497
self .url = 'http://example.com/api?foo=bar#fragment'
491
498
self .session = ClientSession (raise_for_status = True )
492
- super ().setUp ()
493
499
494
- async def asyncTearDown (self ):
500
+ async def teardown (self ):
495
501
close_result = self .session .close ()
496
502
if close_result is not None :
497
503
await close_result
498
- super ().tearDown ()
499
504
500
505
@aioresponses ()
501
506
async def test_raise_for_status (self , m ):
@@ -511,22 +516,21 @@ async def test_raise_for_status(self, m):
511
516
async def test_do_not_raise_for_status (self , m ):
512
517
m .get (self .url , status = 400 )
513
518
response = await self .session .get (self .url ,
514
- raise_for_status = False )
519
+ raise_for_status = False )
515
520
516
521
self .assertEqual (response .status , 400 )
517
522
518
523
519
- class AIOResponseRedirectTest (IsolatedAsyncioTestCase ):
520
- async def asyncSetUp (self ):
524
+ class AIOResponseRedirectTest (AsyncTestCase ):
525
+
526
+ async def setup (self ):
521
527
self .url = "http://10.1.1.1:8080/redirect"
522
528
self .session = ClientSession ()
523
- super ().setUp ()
524
529
525
- async def asyncTearDown (self ):
530
+ async def teardown (self ):
526
531
close_result = self .session .close ()
527
532
if close_result is not None :
528
533
await close_result
529
- super ().tearDown ()
530
534
531
535
@aioresponses ()
532
536
async def test_redirect_followed (self , rsps ):
0 commit comments