2 from unittest import mock
6 class RequestTests(unittest.TestCase):
8 request = phial.Request({
9 'REQUEST_METHOD': 'GET',
10 'QUERY_STRING': 'foo=bar&baz=qux',
13 self.assertEqual(request.GET['foo'], ['bar'])
14 self.assertEqual(request.GET['baz'], ['qux'])
16 def test_parameters(self):
17 request = phial.Request({
18 'REQUEST_METHOD': 'GET',
19 'QUERY_STRING': 'foo=bar&baz=qux',
22 self.assertEqual(request.parameters['foo'], ['bar'])
23 self.assertEqual(request.parameters['baz'], ['qux'])
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')
29 self.assertEqual(response.content, 'Hello, world\n')
31 def test_content_can_be_keyword_argument(self):
32 response = phial.Response(content='Hello, world\n', content_type='text/plain')
34 self.assertEqual(response.content, 'Hello, world\n')
36 def test_status_defaults_to_200(self):
37 response = phial.Response(
38 content_type='text/plain',
39 content='Hello, world\n',
42 self.assertEqual(response.status, 200)
44 def test_headers(self):
45 response = phial.Response(
46 content_type='text/plain',
47 content='Hello, world\n',
53 ('Content-Type', 'text/plain'),
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')
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')
67 def test_sets_content(self):
68 response = phial.JSONResponse({ 'foo': 'bar', 'baz': 42 })
69 self.assertEqual(response.content, '{"foo": "bar", "baz": 42}')
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})
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')
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')
85 def test_takes_location_as_keyword_argument(self):
86 response = phial.RedirectResponse(location='/location')
87 self.assertEqual(response.location, '/location')
89 def test_permanent_defaults_to_true(self):
90 response = phial.RedirectResponse('/location')
91 self.assertEqual(response.permanent, True)
93 def test_status(self):
95 phial.RedirectResponse('/location', permanent=True).status,
99 phial.RedirectResponse('/location', permanent=False).status,
103 def test_headers(self):
105 phial.RedirectResponse('/location').headers,
106 (('Location','/location'),),
109 def test_content(self):
111 phial.RedirectResponse('/location').content,
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')
121 class _get_content_Tests(unittest.TestCase):
122 def test_bytes(self):
124 phial._get_content(mock.MagicMock(content=b'Hello, world\n')),
125 (b'Hello, world\n',),
130 phial._get_content(mock.MagicMock(content='Hello, world\n')),
131 (b'Hello, world\n',),
134 if __name__ == '__main__':