97a7f74a5b7788742fb6f445b5f505b7d5796583
[fwx] / src / test_fwx.py
1 import unittest
2 from unittest import mock
3
4 import fwx
5
6 class RequestTests(unittest.TestCase):
7     def test_GET(self):
8         request = fwx.Request('GET', '/', {
9             'QUERY_STRING': 'foo=bar&baz=qux',
10         })
11
12         self.assertEqual(request.GET['foo'], ['bar'])
13         self.assertEqual(request.GET['baz'], ['qux'])
14
15     def test_parameters(self):
16         request = fwx.Request('GET', '/', {
17             'QUERY_STRING': 'foo=bar&baz=qux',
18         })
19
20         self.assertEqual(request.parameters['foo'], ['bar'])
21         self.assertEqual(request.parameters['baz'], ['qux'])
22
23 class ResponseTests(unittest.TestCase):
24     def test_content_can_be_positional_argument(self):
25         response = fwx.Response('Hello, world\n', content_type='text/plain')
26
27         self.assertEqual(response.content, 'Hello, world\n')
28
29     def test_content_can_be_keyword_argument(self):
30         response = fwx.Response(content='Hello, world\n', content_type='text/plain')
31
32         self.assertEqual(response.content, 'Hello, world\n')
33
34     def test_status_defaults_to_200(self):
35         response = fwx.Response(
36             content_type='text/plain',
37             content='Hello, world\n',
38         )
39
40         self.assertEqual(response.status, 200)
41
42     def test_headers(self):
43         response = fwx.Response(
44             content_type='text/plain',
45             content='Hello, world\n',
46         )
47
48         self.assertEqual(
49             response.headers,
50             (
51                 ('Content-Type', 'text/plain'),
52                 ('X-Content-Type-Options', 'nosniff'),
53             ),
54         )
55
56 class HTMLResponseTests(unittest.TestCase):
57     def test_sets_content_type(self):
58         response = fwx.HTMLResponse('<html><body>Hello, world</body></html>')
59         self.assertEqual(response.content_type, 'text/html')
60
61 class JSONResponseTests(unittest.TestCase):
62     def test_sets_content_type(self):
63         response = fwx.JSONResponse({ 'foo': 'bar', 'baz': 42 })
64         self.assertEqual(response.content_type, 'application/json')
65
66     def test_sets_content(self):
67         response = fwx.JSONResponse({ 'foo': 'bar', 'baz': 42 })
68         self.assertEqual(response.content, '{"foo": "bar", "baz": 42}')
69
70     def test_sets_content_json(self):
71         response = fwx.JSONResponse({ 'foo': 'bar', 'baz': 42 })
72         self.assertEqual(response.content_json, {"foo": "bar", "baz": 42})
73
74 class TextResponseTests(unittest.TestCase):
75     def test_sets_content_type(self):
76         response = fwx.TextResponse('Hello, world\n')
77         self.assertEqual(response.content_type, 'text/plain')
78
79 class RedirectResponse(unittest.TestCase):
80     def test_takes_location_as_positional_argument(self):
81         response = fwx.RedirectResponse('/location')
82         self.assertEqual(response.location, '/location')
83
84     def test_takes_location_as_keyword_argument(self):
85         response = fwx.RedirectResponse(location='/location')
86         self.assertEqual(response.location, '/location')
87
88     def test_permanent_defaults_to_true(self):
89         response = fwx.RedirectResponse('/location')
90         self.assertEqual(response.permanent, True)
91
92     def test_status(self):
93         self.assertEqual(
94             fwx.RedirectResponse('/location', permanent=True).status,
95             308,
96         )
97         self.assertEqual(
98             fwx.RedirectResponse('/location', permanent=False).status,
99             307,
100         )
101
102     def test_headers(self):
103         self.assertEqual(
104             fwx.RedirectResponse('/location').headers,
105             (('Location','/location'),),
106         )
107
108     def test_content(self):
109         self.assertEqual(
110             fwx.RedirectResponse('/location').content,
111             (b'',),
112         )
113
114 class _get_status_Tests(unittest.TestCase):
115     def test_basic(self):
116         self.assertEqual(fwx._get_status(mock.MagicMock(status=200)), '200 OK')
117         self.assertEqual(fwx._get_status(mock.MagicMock(status=307)), '307 Temporary Redirect')
118         self.assertEqual(fwx._get_status(mock.MagicMock(status=308)), '308 Permanent Redirect')
119
120 class _get_content_Tests(unittest.TestCase):
121     def test_bytes(self):
122         self.assertEqual(
123             fwx._get_content(mock.MagicMock(content=b'Hello, world\n')),
124             (b'Hello, world\n',),
125         )
126
127     def test_str(self):
128         self.assertEqual(
129             fwx._get_content(mock.MagicMock(content='Hello, world\n')),
130             (b'Hello, world\n',),
131         )
132
133 class route_on_subpath_Tests(unittest.TestCase):
134     def test_routes(self):
135         router = fwx.route_on_subpath(
136             routes={
137                 'foo': lambda request: fwx.TextResponse('foo'),
138                 'bar': lambda request: fwx.TextResponse('bar'),
139                 'baz': lambda request: fwx.TextResponse('baz'),
140             },
141         )
142
143         self.assertEqual(
144             router(fwx.Request('GET', '/bar/bara/anne/')).content,
145             'bar',
146         )
147
148     def test_resets_subpath(self):
149         router = fwx.route_on_subpath(
150             routes={
151                 'foo': lambda request: fwx.TextResponse('foo'),
152                 'bar': lambda request: fwx.TextResponse(request.subpath),
153                 'baz': lambda request: fwx.TextResponse('baz'),
154             },
155         )
156
157         self.assertEqual(
158             router(fwx.Request('GET', '/bar/bara/anne/')).content,
159             'bara/anne/',
160         )
161
162     def test_leaves_path_intact(self):
163         router = fwx.route_on_subpath(
164             routes={
165                 'foo': lambda request: fwx.TextResponse('foo'),
166                 'bar': lambda request: fwx.TextResponse(request.path),
167                 'baz': lambda request: fwx.TextResponse('baz'),
168             },
169         )
170
171         self.assertEqual(
172             router(fwx.Request('GET', '/bar/bara/anne/')).content,
173             '/bar/bara/anne/',
174         )
175
176 class default_file_not_found_Tests(unittest.TestCase):
177     def test_responds(self):
178         response = fwx.default_file_not_found_handler(
179             fwx.Request('GET', '/bar/bara/anne/'),
180         )
181
182         self.assertNotEqual(response, None)
183
184 if __name__ == '__main__':
185     unittest.main()