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