-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_json_response.py
73 lines (55 loc) · 2.45 KB
/
test_json_response.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
import unittest
import unittest.mock as mock
import gzip
from app.utils.json_response import JsonResponse
class TestCase(unittest.TestCase):
@mock.patch('aiohttp.web.Response.__init__')
def test_default_params_call_dict(self, m_aiohttp_response_class_init):
JsonResponse({})
m_aiohttp_response_class_init.assert_called_once_with(
body=b'{}',
status=200,
headers={},
content_type='application/json')
@mock.patch('aiohttp.web.Response.__init__')
def test_default_params_call_list(self, m_aiohttp_response_class_init):
JsonResponse([])
m_aiohttp_response_class_init.assert_called_once_with(
body=b'[]',
status=200,
headers={},
content_type='application/json')
@mock.patch('aiohttp.web.Response.__init__')
def test_call_custom_status(self, m_aiohttp_response_class_init):
JsonResponse({'error': 'validation_error'}, status=400)
m_aiohttp_response_class_init.assert_called_once_with(
body=b'{"error":"validation_error"}',
status=400,
headers={},
content_type='application/json')
@mock.patch('aiohttp.web.Response.__init__')
def test_call_custom_content_type(self, m_aiohttp_response_class_init):
JsonResponse({'abc': '123'}, content_type='text/x-json; charset=utf-8')
m_aiohttp_response_class_init.assert_called_once_with(
body=b'{"abc":"123"}',
status=200,
headers={},
content_type='text/x-json; charset=utf-8')
@mock.patch('aiohttp.web.Response.__init__')
def test_call_compress(self, m_aiohttp_response_class_init):
JsonResponse({}, compress=True)
m_aiohttp_response_class_init.assert_called_once_with(
body=gzip.compress(b'{}', compresslevel=5),
status=200,
headers={'Content-Encoding': 'gzip'},
content_type='application/json')
@mock.patch('aiohttp.web.Response.__init__')
def test_call_compress_with_headers(self, m_aiohttp_response_class_init):
JsonResponse({}, headers={'My-Header': 'test'}, compress=True)
m_aiohttp_response_class_init.assert_called_once_with(
body=gzip.compress(b'{}', compresslevel=5),
status=200,
headers={'My-Header': 'test', 'Content-Encoding': 'gzip'},
content_type='application/json')
if __name__ == '__main__':
unittest.main()