Create a redirect response type
[fwx] / test_phial.py
1 import unittest
2 from unittest import mock
3
4 import phial
5
6 class ResponseTests(unittest.TestCase):
7     def test_content_can_be_positional_argument(self):
8         response = phial.Response('Hello, world\n', content_type='text/plain')
9
10         self.assertEqual(response.content, 'Hello, world\n')
11
12     def test_content_can_be_keyword_argument(self):
13         response = phial.Response(content='Hello, world\n', content_type='text/plain')
14
15         self.assertEqual(response.content, 'Hello, world\n')
16
17     def test_status_defaults_to_200(self):
18         response = phial.Response(
19             content_type='text/plain',
20             content='Hello, world\n',
21         )
22
23         self.assertEqual(response.status, 200)
24
25     def test_headers(self):
26         response = phial.Response(
27             content_type='text/plain',
28             content='Hello, world\n',
29         )
30
31         self.assertEqual(
32             response.headers,
33             (
34                 ('Content-Type', 'text/plain'),
35             ),
36         )
37
38 class HTMLResponseTests(unittest.TestCase):
39     def test_sets_content_type(self):
40         response = phial.HTMLResponse('<html><body>Hello, world</body></html>')
41         self.assertEqual(response.content_type, 'text/html')
42
43 class JSONResponseTests(unittest.TestCase):
44     def test_sets_content_type(self):
45         response = phial.JSONResponse({ 'foo': 'bar', 'baz': 42 })
46         self.assertEqual(response.content_type, 'application/json')
47
48     def test_sets_content(self):
49         response = phial.JSONResponse({ 'foo': 'bar', 'baz': 42 })
50         self.assertEqual(response.content, '{"foo": "bar", "baz": 42}')
51
52     def test_sets_content_json(self):
53         response = phial.JSONResponse({ 'foo': 'bar', 'baz': 42 })
54         self.assertEqual(response.content_json, {"foo": "bar", "baz": 42})
55
56 class TextResponseTests(unittest.TestCase):
57     def test_sets_content_type(self):
58         response = phial.TextResponse('Hello, world\n')
59         self.assertEqual(response.content_type, 'text/plain')
60
61 class RedirectResponse(unittest.TestCase):
62     def test_takes_location_as_positional_argument(self):
63         response = phial.RedirectResponse('/location')
64         self.assertEqual(response.location, '/location')
65
66     def test_takes_location_as_keyword_argument(self):
67         response = phial.RedirectResponse(location='/location')
68         self.assertEqual(response.location, '/location')
69
70     def test_permanent_defaults_to_true(self):
71         response = phial.RedirectResponse('/location')
72         self.assertEqual(response.permanent, True)
73
74     def test_status(self):
75         self.assertEqual(
76             phial.RedirectResponse('/location', permanent=True).status,
77             308,
78         )
79         self.assertEqual(
80             phial.RedirectResponse('/location', permanent=False).status,
81             307,
82         )
83
84     def test_headers(self):
85         self.assertEqual(
86             phial.RedirectResponse('/location').headers,
87             (('Location','/location'),),
88         )
89
90     def test_content(self):
91         self.assertEqual(
92             phial.RedirectResponse('/location').content,
93             (b'',),
94         )
95
96 class _get_status_Tests(unittest.TestCase):
97     def test_basic(self):
98         self.assertEqual(phial._get_status(mock.MagicMock(status=200)), '200 OK')
99         self.assertEqual(phial._get_status(mock.MagicMock(status=307)), '307 Temporary Redirect')
100         self.assertEqual(phial._get_status(mock.MagicMock(status=308)), '308 Permanent Redirect')
101
102 class _get_content_Tests(unittest.TestCase):
103     def test_bytes(self):
104         self.assertEqual(
105             phial._get_content(mock.MagicMock(content=b'Hello, world\n')),
106             (b'Hello, world\n',),
107         )
108
109     def test_str(self):
110         self.assertEqual(
111             phial._get_content(mock.MagicMock(content='Hello, world\n')),
112             (b'Hello, world\n',),
113         )
114
115 if __name__ == '__main__':
116     unittest.main()