1+ import os
2+ import tempfile
3+
14from aiohttp import web
2- from aiohttp .test_utils import AioHTTPTestCase , unittest_run_loop
3- from asynctest import mock as async_mock , mock_open
5+ from aiohttp .test_utils import AioHTTPTestCase
46
57from ..http import fetch , fetch_stream , FetchError , put_file , PutError
68
79
10+ class TempFile :
11+ def __init__ (self ):
12+ self .name = None
13+
14+ def __enter__ (self ):
15+ file = tempfile .NamedTemporaryFile (delete = False )
16+ file .write (b"test" )
17+ file .close ()
18+ self .name = file .name
19+ return self .name
20+
21+ def __exit__ (self , * args ):
22+ if self .name :
23+ os .unlink (self .name )
24+
25+
826class TestTransportUtils (AioHTTPTestCase ):
927 async def setUpAsync (self ):
1028 self .fail_calls = 0
1129 self .succeed_calls = 0
30+ self .redirects = 0
1231 await super ().setUpAsync ()
1332
1433 async def get_application (self ):
@@ -19,19 +38,30 @@ async def get_application(self):
1938 web .get ("/succeed" , self .succeed_route ),
2039 web .put ("/fail" , self .fail_route ),
2140 web .put ("/succeed" , self .succeed_route ),
41+ web .put ("/redirect" , self .redirect_route ),
2242 ]
2343 )
2444 return app
2545
2646 async def fail_route (self , request ):
2747 self .fail_calls += 1
48+ # avoid aiohttp test server issue: https://github.com/aio-libs/aiohttp/issues/3968
49+ await request .read ()
2850 raise web .HTTPForbidden ()
2951
3052 async def succeed_route (self , request ):
3153 self .succeed_calls += 1
3254 ret = web .json_response ([True ])
3355 return ret
3456
57+ async def redirect_route (self , request ):
58+ if self .redirects > 0 :
59+ self .redirects -= 1
60+ # avoid aiohttp test server issue: https://github.com/aio-libs/aiohttp/issues/3968
61+ await request .read ()
62+ raise web .HTTPRedirection (f"http://localhost:{ self .server .port } /success" )
63+ return await self .succeed_route (request )
64+
3565 async def test_fetch_stream (self ):
3666 server_addr = f"http://localhost:{ self .server .port } "
3767 stream = await fetch_stream (
@@ -84,40 +114,55 @@ async def test_fetch_fail(self):
84114 )
85115 assert self .fail_calls == 2
86116
87- async def test_put_file (self ):
117+ async def test_put_file_with_session (self ):
88118 server_addr = f"http://localhost:{ self .server .port } "
89- with async_mock . patch ( "builtins.open" , mock_open ( read_data = "data" )) :
119+ with TempFile () as tails :
90120 result = await put_file (
91121 f"{ server_addr } /succeed" ,
92- {"tails" : "/tmp/dummy/path" },
122+ {"tails" : tails },
93123 {"genesis" : "..." },
94124 session = self .client .session ,
95125 json = True ,
96126 )
97- assert result == [1 ]
127+ assert result == [True ]
98128 assert self .succeed_calls == 1
99129
100130 async def test_put_file_default_client (self ):
101131 server_addr = f"http://localhost:{ self .server .port } "
102- with async_mock . patch ( "builtins.open" , mock_open ( read_data = "data" )) :
132+ with TempFile () as tails :
103133 result = await put_file (
104134 f"{ server_addr } /succeed" ,
105- {"tails" : "/tmp/dummy/path" },
135+ {"tails" : tails },
106136 {"genesis" : "..." },
107137 json = True ,
108138 )
109- assert result == [1 ]
139+ assert result == [True ]
110140 assert self .succeed_calls == 1
111141
112142 async def test_put_file_fail (self ):
113143 server_addr = f"http://localhost:{ self .server .port } "
114- with async_mock . patch ( "builtins.open" , mock_open ( read_data = "data" )) :
144+ with TempFile () as tails :
115145 with self .assertRaises (PutError ):
116- result = await put_file (
146+ _ = await put_file (
117147 f"{ server_addr } /fail" ,
118- {"tails" : "/tmp/dummy/path" },
148+ {"tails" : tails },
119149 {"genesis" : "..." },
120150 max_attempts = 2 ,
121151 json = True ,
122152 )
123153 assert self .fail_calls == 2
154+
155+ async def test_put_file_redirect (self ):
156+ server_addr = f"http://localhost:{ self .server .port } "
157+ self .redirects = 1
158+ with TempFile () as tails :
159+ result = await put_file (
160+ f"{ server_addr } /redirect" ,
161+ {"tails" : tails },
162+ {"genesis" : "..." },
163+ max_attempts = 2 ,
164+ json = True ,
165+ )
166+ assert result == [True ]
167+ assert self .succeed_calls == 1
168+ assert self .redirects == 0
0 commit comments