2 from unittest import mock
6 class RequestTests(unittest.TestCase):
8 request = fwx.Request('GET', '/', {
9 'QUERY_STRING': 'foo=bar&baz=qux',
12 self.assertEqual(request.GET['foo'], ['bar'])
13 self.assertEqual(request.GET['baz'], ['qux'])
15 def test_parameters(self):
16 request = fwx.Request('GET', '/', {
17 'QUERY_STRING': 'foo=bar&baz=qux',
20 self.assertEqual(request.parameters['foo'], ['bar'])
21 self.assertEqual(request.parameters['baz'], ['qux'])
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')
27 self.assertEqual(response.content, 'Hello, world\n')
29 def test_content_can_be_keyword_argument(self):
30 response = fwx.Response(content='Hello, world\n', content_type='text/plain')
32 self.assertEqual(response.content, 'Hello, world\n')
34 def test_status_defaults_to_200(self):
35 response = fwx.Response(
36 content_type='text/plain',
37 content='Hello, world\n',
40 self.assertEqual(response.status, 200)
42 def test_headers(self):
43 response = fwx.Response(
44 content_type='text/plain',
45 content='Hello, world\n',
51 ('Content-Type', 'text/plain'),
52 ('X-Content-Type-Options', 'nosniff'),
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')
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')
66 def test_sets_content(self):
67 response = fwx.JSONResponse({ 'foo': 'bar', 'baz': 42 })
68 self.assertEqual(response.content, '{"foo": "bar", "baz": 42}')
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})
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')
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')
84 def test_takes_location_as_keyword_argument(self):
85 response = fwx.RedirectResponse(location='/location')
86 self.assertEqual(response.location, '/location')
88 def test_permanent_defaults_to_true(self):
89 response = fwx.RedirectResponse('/location')
90 self.assertEqual(response.permanent, True)
92 def test_status(self):
94 fwx.RedirectResponse('/location', permanent=True).status,
98 fwx.RedirectResponse('/location', permanent=False).status,
102 def test_headers(self):
104 fwx.RedirectResponse('/location').headers,
105 (('Location','/location'),),
108 def test_content(self):
110 fwx.RedirectResponse('/location').content,
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')
120 class _get_content_Tests(unittest.TestCase):
121 def test_bytes(self):
123 fwx._get_content(mock.MagicMock(content=b'Hello, world\n')),
124 (b'Hello, world\n',),
129 fwx._get_content(mock.MagicMock(content='Hello, world\n')),
130 (b'Hello, world\n',),
133 class route_on_subpath_Tests(unittest.TestCase):
134 def test_routes(self):
135 router = fwx.route_on_subpath(
137 'foo': lambda request: fwx.TextResponse('foo'),
138 'bar': lambda request: fwx.TextResponse('bar'),
139 'baz': lambda request: fwx.TextResponse('baz'),
144 router(fwx.Request('GET', '/bar/bara/anne/')).content,
148 def test_resets_subpath(self):
149 router = fwx.route_on_subpath(
151 'foo': lambda request: fwx.TextResponse('foo'),
152 'bar': lambda request: fwx.TextResponse(request.subpath),
153 'baz': lambda request: fwx.TextResponse('baz'),
158 router(fwx.Request('GET', '/bar/bara/anne/')).content,
162 def test_leaves_path_intact(self):
163 router = fwx.route_on_subpath(
165 'foo': lambda request: fwx.TextResponse('foo'),
166 'bar': lambda request: fwx.TextResponse(request.path),
167 'baz': lambda request: fwx.TextResponse('baz'),
172 router(fwx.Request('GET', '/bar/bara/anne/')).content,
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/'),
182 self.assertNotEqual(response, None)
184 if __name__ == '__main__':