2 from unittest import mock
6 class RequestTests(unittest.TestCase):
8 request = fwx.Request({
10 'QUERY_STRING': 'foo=bar&baz=qux',
11 'REQUEST_METHOD': 'GET',
14 self.assertEqual(request.GET['foo'], ['bar'])
15 self.assertEqual(request.GET['baz'], ['qux'])
17 def test_parameters(self):
18 request = fwx.Request({
20 'REQUEST_METHOD': 'GET',
21 'QUERY_STRING': 'foo=bar&baz=qux',
24 self.assertEqual(request.parameters['foo'], ['bar'])
25 self.assertEqual(request.parameters['baz'], ['qux'])
27 class ResponseTests(unittest.TestCase):
28 def test_content_can_be_positional_argument(self):
29 response = fwx.Response('Hello, world\n', content_type='text/plain')
31 self.assertEqual(response.content, 'Hello, world\n')
33 def test_content_can_be_keyword_argument(self):
34 response = fwx.Response(content='Hello, world\n', content_type='text/plain')
36 self.assertEqual(response.content, 'Hello, world\n')
38 def test_status_defaults_to_200(self):
39 response = fwx.Response(
40 content_type='text/plain',
41 content='Hello, world\n',
44 self.assertEqual(response.status, 200)
46 def test_headers(self):
47 response = fwx.Response(
48 content_type='text/plain',
49 content='Hello, world\n',
55 ('Content-Type', 'text/plain'),
59 class HTMLResponseTests(unittest.TestCase):
60 def test_sets_content_type(self):
61 response = fwx.HTMLResponse('<html><body>Hello, world</body></html>')
62 self.assertEqual(response.content_type, 'text/html')
64 class JSONResponseTests(unittest.TestCase):
65 def test_sets_content_type(self):
66 response = fwx.JSONResponse({ 'foo': 'bar', 'baz': 42 })
67 self.assertEqual(response.content_type, 'application/json')
69 def test_sets_content(self):
70 response = fwx.JSONResponse({ 'foo': 'bar', 'baz': 42 })
71 self.assertEqual(response.content, '{"foo": "bar", "baz": 42}')
73 def test_sets_content_json(self):
74 response = fwx.JSONResponse({ 'foo': 'bar', 'baz': 42 })
75 self.assertEqual(response.content_json, {"foo": "bar", "baz": 42})
77 class TextResponseTests(unittest.TestCase):
78 def test_sets_content_type(self):
79 response = fwx.TextResponse('Hello, world\n')
80 self.assertEqual(response.content_type, 'text/plain')
82 class RedirectResponse(unittest.TestCase):
83 def test_takes_location_as_positional_argument(self):
84 response = fwx.RedirectResponse('/location')
85 self.assertEqual(response.location, '/location')
87 def test_takes_location_as_keyword_argument(self):
88 response = fwx.RedirectResponse(location='/location')
89 self.assertEqual(response.location, '/location')
91 def test_permanent_defaults_to_true(self):
92 response = fwx.RedirectResponse('/location')
93 self.assertEqual(response.permanent, True)
95 def test_status(self):
97 fwx.RedirectResponse('/location', permanent=True).status,
101 fwx.RedirectResponse('/location', permanent=False).status,
105 def test_headers(self):
107 fwx.RedirectResponse('/location').headers,
108 (('Location','/location'),),
111 def test_content(self):
113 fwx.RedirectResponse('/location').content,
117 class _get_status_Tests(unittest.TestCase):
118 def test_basic(self):
119 self.assertEqual(fwx._get_status(mock.MagicMock(status=200)), '200 OK')
120 self.assertEqual(fwx._get_status(mock.MagicMock(status=307)), '307 Temporary Redirect')
121 self.assertEqual(fwx._get_status(mock.MagicMock(status=308)), '308 Permanent Redirect')
123 class _get_content_Tests(unittest.TestCase):
124 def test_bytes(self):
126 fwx._get_content(mock.MagicMock(content=b'Hello, world\n')),
127 (b'Hello, world\n',),
132 fwx._get_content(mock.MagicMock(content='Hello, world\n')),
133 (b'Hello, world\n',),
136 class route_on_subpath_Tests(unittest.TestCase):
137 def test_routes(self):
138 router = fwx.route_on_subpath(
140 'foo': lambda request: fwx.TextResponse('foo'),
141 'bar': lambda request: fwx.TextResponse('bar'),
142 'baz': lambda request: fwx.TextResponse('baz'),
148 'PATH_INFO': '/bar/bara/anne/',
149 'REQUEST_METHOD': 'GET',
154 def test_resets_subpath(self):
155 router = fwx.route_on_subpath(
157 'foo': lambda request: fwx.TextResponse('foo'),
158 'bar': lambda request: fwx.TextResponse(request.subpath),
159 'baz': lambda request: fwx.TextResponse('baz'),
165 'PATH_INFO': '/bar/bara/anne/',
166 'REQUEST_METHOD': 'GET',
171 def test_leaves_path_intact(self):
172 router = fwx.route_on_subpath(
174 'foo': lambda request: fwx.TextResponse('foo'),
175 'bar': lambda request: fwx.TextResponse(request.path),
176 'baz': lambda request: fwx.TextResponse('baz'),
182 'PATH_INFO': '/bar/bara/anne/',
183 'REQUEST_METHOD': 'GET',
188 if __name__ == '__main__':