2 from unittest import mock
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')
10 self.assertEqual(response.content, 'Hello, world\n')
12 def test_content_can_be_keyword_argument(self):
13 response = phial.Response(content='Hello, world\n', content_type='text/plain')
15 self.assertEqual(response.content, 'Hello, world\n')
17 def test_status_defaults_to_200(self):
18 response = phial.Response(
19 content_type='text/plain',
20 content='Hello, world\n',
23 self.assertEqual(response.status, 200)
25 def test_headers(self):
26 response = phial.Response(
27 content_type='text/plain',
28 content='Hello, world\n',
34 ('Content-Type', 'text/plain'),
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')
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')
48 def test_sets_content(self):
49 response = phial.JSONResponse({ 'foo': 'bar', 'baz': 42 })
50 self.assertEqual(response.content, '{"foo": "bar", "baz": 42}')
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})
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')
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')
66 def test_takes_location_as_keyword_argument(self):
67 response = phial.RedirectResponse(location='/location')
68 self.assertEqual(response.location, '/location')
70 def test_permanent_defaults_to_true(self):
71 response = phial.RedirectResponse('/location')
72 self.assertEqual(response.permanent, True)
74 def test_status(self):
76 phial.RedirectResponse('/location', permanent=True).status,
80 phial.RedirectResponse('/location', permanent=False).status,
84 def test_headers(self):
86 phial.RedirectResponse('/location').headers,
87 (('Location','/location'),),
90 def test_content(self):
92 phial.RedirectResponse('/location').content,
96 class _get_status_Tests(unittest.TestCase):
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')
102 class _get_content_Tests(unittest.TestCase):
103 def test_bytes(self):
105 phial._get_content(mock.MagicMock(content=b'Hello, world\n')),
106 (b'Hello, world\n',),
111 phial._get_content(mock.MagicMock(content='Hello, world\n')),
112 (b'Hello, world\n',),
115 if __name__ == '__main__':