forked from pnuckowski/aioresponses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_aioresponses.py
409 lines (345 loc) · 13.8 KB
/
test_aioresponses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# -*- coding: utf-8 -*-
import asyncio
import re
from typing import Coroutine, Generator, Union
from unittest.mock import patch
from aiohttp import hdrs
from aiohttp import http
from aiohttp.client import ClientSession
from aiohttp.client_reqrep import ClientResponse
from asynctest import fail_on, skipIf
from asynctest.case import TestCase
from ddt import ddt, data
try:
from aiohttp.errors import (
ClientConnectionError,
ClientResponseError,
HttpProcessingError,
)
except ImportError:
from aiohttp.client_exceptions import (
ClientConnectionError,
ClientResponseError,
)
from aiohttp.http_exceptions import HttpProcessingError
from aioresponses.compat import AIOHTTP_VERSION, URL
from aioresponses import CallbackResult, aioresponses
@ddt
class AIOResponsesTestCase(TestCase):
use_default_loop = False
@asyncio.coroutine
def setUp(self):
self.url = 'http://example.com/api?foo=bar#fragment'
self.session = ClientSession()
super().setUp()
@asyncio.coroutine
def tearDown(self):
close_result = self.session.close()
if close_result is not None:
yield from close_result
super().tearDown()
def run_async(self, coroutine: Union[Coroutine, Generator]):
return self.loop.run_until_complete(coroutine)
@asyncio.coroutine
def request(self, url: str):
return (yield from self.session.get(url))
@data(
hdrs.METH_HEAD,
hdrs.METH_GET,
hdrs.METH_POST,
hdrs.METH_PUT,
hdrs.METH_PATCH,
hdrs.METH_DELETE,
hdrs.METH_OPTIONS,
)
@patch('aioresponses.aioresponses.add')
@fail_on(unused_loop=False)
def test_shortcut_method(self, http_method, mocked):
with aioresponses() as m:
getattr(m, http_method.lower())(self.url)
mocked.assert_called_once_with(self.url, method=http_method)
@aioresponses()
def test_returned_instance(self, m):
m.get(self.url)
response = self.run_async(self.session.get(self.url))
self.assertIsInstance(response, ClientResponse)
@aioresponses()
@asyncio.coroutine
def test_returned_instance_and_status_code(self, m):
m.get(self.url, status=204)
response = yield from self.session.get(self.url)
self.assertIsInstance(response, ClientResponse)
self.assertEqual(response.status, 204)
@aioresponses()
@asyncio.coroutine
def test_returned_response_headers(self, m):
m.get(self.url,
content_type='text/html',
headers={'Connection': 'keep-alive'})
response = yield from self.session.get(self.url)
self.assertEqual(response.headers['Connection'], 'keep-alive')
self.assertEqual(response.headers[hdrs.CONTENT_TYPE], 'text/html')
@aioresponses()
@asyncio.coroutine
def test_returned_response_raw_headers(self, m):
m.get(self.url,
content_type='text/html',
headers={'Connection': 'keep-alive'})
response = yield from self.session.get(self.url)
expected_raw_headers = (
(b'Content-Type', b'text/html'),
(b'Connection', b'keep-alive')
)
self.assertEqual(response.raw_headers, expected_raw_headers)
@aioresponses()
@asyncio.coroutine
def test_raise_for_status(self, m):
m.get(self.url, status=400)
with self.assertRaises(ClientResponseError) as cm:
response = yield from self.session.get(self.url)
response.raise_for_status()
self.assertEqual(cm.exception.message, http.RESPONSES[400][0])
@aioresponses()
@asyncio.coroutine
@skipIf(condition=AIOHTTP_VERSION < '3.4.0',
reason='aiohttp<3.4.0 does not support raise_for_status '
'arguments for requests')
def test_request_raise_for_status(self, m):
m.get(self.url, status=400)
with self.assertRaises(ClientResponseError) as cm:
yield from self.session.get(self.url, raise_for_status=True)
self.assertEqual(cm.exception.message, http.RESPONSES[400][0])
@aioresponses()
@asyncio.coroutine
def test_returned_instance_and_params_handling(self, m):
expected_url = 'http://example.com/api?foo=bar&x=42#fragment'
m.get(expected_url)
response = yield from self.session.get(self.url, params={'x': 42})
self.assertIsInstance(response, ClientResponse)
self.assertEqual(response.status, 200)
expected_url = 'http://example.com/api?x=42#fragment'
m.get(expected_url)
response = yield from self.session.get(
'http://example.com/api#fragment',
params={'x': 42}
)
self.assertIsInstance(response, ClientResponse)
self.assertEqual(response.status, 200)
@aioresponses()
def test_method_dont_match(self, m):
m.get(self.url)
with self.assertRaises(ClientConnectionError):
self.run_async(self.session.post(self.url))
@aioresponses()
@asyncio.coroutine
def test_streaming(self, m):
m.get(self.url, body='Test')
resp = yield from self.session.get(self.url)
content = yield from resp.content.read()
self.assertEqual(content, b'Test')
@aioresponses()
@asyncio.coroutine
def test_streaming_up_to(self, m):
m.get(self.url, body='Test')
resp = yield from self.session.get(self.url)
content = yield from resp.content.read(2)
self.assertEqual(content, b'Te')
content = yield from resp.content.read(2)
self.assertEqual(content, b'st')
@asyncio.coroutine
def test_mocking_as_context_manager(self):
with aioresponses() as aiomock:
aiomock.add(self.url, payload={'foo': 'bar'})
resp = yield from self.session.get(self.url)
self.assertEqual(resp.status, 200)
payload = yield from resp.json()
self.assertDictEqual(payload, {'foo': 'bar'})
def test_mocking_as_decorator(self):
@aioresponses()
def foo(loop, m):
m.add(self.url, payload={'foo': 'bar'})
resp = loop.run_until_complete(self.session.get(self.url))
self.assertEqual(resp.status, 200)
payload = loop.run_until_complete(resp.json())
self.assertDictEqual(payload, {'foo': 'bar'})
foo(self.loop)
@asyncio.coroutine
def test_passing_argument(self):
@aioresponses(param='mocked')
@asyncio.coroutine
def foo(mocked):
mocked.add(self.url, payload={'foo': 'bar'})
resp = yield from self.session.get(self.url)
self.assertEqual(resp.status, 200)
yield from foo()
@fail_on(unused_loop=False)
def test_mocking_as_decorator_wrong_mocked_arg_name(self):
@aioresponses(param='foo')
def foo(bar):
# no matter what is here it should raise an error
pass
with self.assertRaises(TypeError) as cm:
foo()
exc = cm.exception
self.assertIn("foo() got an unexpected keyword argument 'foo'",
str(exc))
@asyncio.coroutine
def test_unknown_request(self):
with aioresponses() as aiomock:
aiomock.add(self.url, payload={'foo': 'bar'})
with self.assertRaises(ClientConnectionError):
yield from self.session.get('http://example.com/foo')
@asyncio.coroutine
def test_raising_custom_error(self):
with aioresponses() as aiomock:
aiomock.get(self.url, exception=HttpProcessingError(message='foo'))
with self.assertRaises(HttpProcessingError):
yield from self.session.get(self.url)
@asyncio.coroutine
def test_multiple_requests(self):
with aioresponses() as m:
m.get(self.url, status=200)
m.get(self.url, status=201)
m.get(self.url, status=202)
resp = yield from self.session.get(self.url)
self.assertEqual(resp.status, 200)
resp = yield from self.session.get(self.url)
self.assertEqual(resp.status, 201)
resp = yield from self.session.get(self.url)
self.assertEqual(resp.status, 202)
key = ('GET', URL(self.url))
self.assertIn(key, m.requests)
self.assertEqual(len(m.requests[key]), 3)
self.assertEqual(m.requests[key][0].args, tuple())
self.assertEqual(m.requests[key][0].kwargs,
{'allow_redirects': True})
@asyncio.coroutine
def test_address_as_instance_of_url_combined_with_pass_through(self):
external_api = 'http://httpbin.org/status/201'
@asyncio.coroutine
def doit():
api_resp = yield from self.session.get(self.url)
# we have to hit actual url,
# otherwise we do not test pass through option properly
ext_rep = yield from self.session.get(URL(external_api))
return api_resp, ext_rep
with aioresponses(passthrough=[external_api]) as m:
m.get(self.url, status=200)
api, ext = yield from doit()
self.assertEqual(api.status, 200)
self.assertEqual(ext.status, 201)
@aioresponses()
@asyncio.coroutine
def test_custom_response_class(self, m):
class CustomClientResponse(ClientResponse):
pass
m.get(self.url, body='Test', response_class=CustomClientResponse)
resp = yield from self.session.get(self.url)
self.assertTrue(isinstance(resp, CustomClientResponse))
@aioresponses()
def test_exceptions_in_the_middle_of_responses(self, mocked):
mocked.get(self.url, payload={}, status=204)
mocked.get(self.url, exception=ValueError('oops'), )
mocked.get(self.url, payload={}, status=204)
mocked.get(self.url, exception=ValueError('oops'), )
mocked.get(self.url, payload={}, status=200)
@asyncio.coroutine
def doit():
return (yield from self.session.get(self.url))
self.assertEqual(self.run_async(doit()).status, 204)
with self.assertRaises(ValueError):
self.run_async(doit())
self.assertEqual(self.run_async(doit()).status, 204)
with self.assertRaises(ValueError):
self.run_async(doit())
self.assertEqual(self.run_async(doit()).status, 200)
@aioresponses()
@asyncio.coroutine
def test_request_should_match_regexp(self, mocked):
mocked.get(
re.compile(r'^http://example\.com/api\?foo=.*$'),
payload={}, status=200
)
response = yield from self.request(self.url)
self.assertEqual(response.status, 200)
@aioresponses()
@asyncio.coroutine
def test_request_does_not_match_regexp(self, mocked):
mocked.get(
re.compile(r'^http://exampleexample\.com/api\?foo=.*$'),
payload={}, status=200
)
with self.assertRaises(ClientConnectionError):
yield from self.request(self.url)
@aioresponses()
def test_timeout(self, mocked):
mocked.get(self.url, timeout=True)
with self.assertRaises(asyncio.TimeoutError):
self.run_async(self.request(self.url))
@aioresponses()
def test_callback(self, m):
body = b'New body'
def callback(url, **kwargs):
self.assertEqual(str(url), self.url)
self.assertEqual(kwargs, {'allow_redirects': True})
return CallbackResult(body=body)
m.get(self.url, callback=callback)
response = self.run_async(self.request(self.url))
data = self.run_async(response.read())
assert data == body
@aioresponses()
def test_callback_coroutine(self, m):
body = b'New body'
event = asyncio.Event()
@asyncio.coroutine
def callback(url, **kwargs):
yield from event.wait()
self.assertEqual(str(url), self.url)
self.assertEqual(kwargs, {'allow_redirects': True})
return CallbackResult(body=body)
m.get(self.url, callback=callback)
future = asyncio.ensure_future(self.request(self.url))
self.run_async(asyncio.wait([future], timeout=0))
assert not future.done()
event.set()
self.run_async(asyncio.wait([future], timeout=0))
assert future.done()
response = future.result()
data = self.run_async(response.read())
assert data == body
class AIOResponsesRaiseForStatusSessionTestCase(TestCase):
"""Test case for sessions with raise_for_status=True.
This flag, introduced in aiohttp v2.0.0, automatically calls
`raise_for_status()`.
It is overriden by the `raise_for_status` argument of the request since
aiohttp v3.4.a0.
"""
use_default_loop = False
@asyncio.coroutine
def setUp(self):
self.url = 'http://example.com/api?foo=bar#fragment'
self.session = ClientSession(raise_for_status=True)
super().setUp()
@asyncio.coroutine
def tearDown(self):
close_result = self.session.close()
if close_result is not None:
yield from close_result
super().tearDown()
@aioresponses()
@asyncio.coroutine
def test_raise_for_status(self, m):
m.get(self.url, status=400)
with self.assertRaises(ClientResponseError) as cm:
yield from self.session.get(self.url)
self.assertEqual(cm.exception.message, http.RESPONSES[400][0])
@aioresponses()
@asyncio.coroutine
@skipIf(condition=AIOHTTP_VERSION < '3.4.0',
reason='aiohttp<3.4.0 does not support raise_for_status '
'arguments for requests')
def test_do_not_raise_for_status(self, m):
m.get(self.url, status=400)
response = yield from self.session.get(self.url,
raise_for_status=False)
self.assertEqual(response.status, 400)