Michael Merickel
2018-10-15 dd3cc81f75dcb5ff96e0751653071722a15f46c2
commit | author | age
c8061e 1 import unittest
dd3cc8 2 from . import dummy
c8061e 3
CM 4 class TestPRequestCommand(unittest.TestCase):
5     def _getTargetClass(self):
6         from pyramid.scripts.prequest import PRequestCommand
7         return PRequestCommand
8
8a3621 9     def _makeOne(self, argv, headers=None):
c8061e 10         cmd = self._getTargetClass()(argv)
49fb77 11
c8061e 12         def helloworld(environ, start_request):
CM 13             self._environ = environ
14             self._path_info = environ['PATH_INFO']
678790 15             start_request('200 OK', headers or [])
c8061e 16             return [b'abc']
678790 17         self.loader = dummy.DummyLoader(app=helloworld)
MM 18         self._out = []
19         cmd._get_config_loader = self.loader
20         cmd.out = self.out
21         return cmd
c8061e 22
CM 23     def out(self, msg):
24         self._out.append(msg)
25
26     def test_command_not_enough_args(self):
27         command = self._makeOne([])
28         command.run()
29         self.assertEqual(self._out, ['You must provide at least two arguments'])
30
31     def test_command_two_args(self):
a897b5 32         command = self._makeOne(['', 'development.ini', '/'],
BJR 33                 [('Content-Type', 'text/html; charset=UTF-8')])
c8061e 34         command.run()
CM 35         self.assertEqual(self._path_info, '/')
3e489b 36         self.assertEqual(self.loader.uri.path, 'development.ini')
678790 37         self.assertEqual(self.loader.calls[0]['op'], 'logging')
MM 38         self.assertEqual(self.loader.calls[1]['op'], 'app')
39         self.assertEqual(self.loader.calls[1]['name'], None)
c8061e 40         self.assertEqual(self._out, ['abc'])
CM 41
42     def test_command_path_doesnt_start_with_slash(self):
a897b5 43         command = self._makeOne(['', 'development.ini', 'abc'],
BJR 44                 [('Content-Type', 'text/html; charset=UTF-8')])
c8061e 45         command.run()
CM 46         self.assertEqual(self._path_info, '/abc')
3e489b 47         self.assertEqual(self.loader.uri.path, 'development.ini')
c8061e 48         self.assertEqual(self._out, ['abc'])
CM 49
50     def test_command_has_bad_config_header(self):
51         command = self._makeOne(
52             ['', '--header=name','development.ini', '/'])
53         command.run()
54         self.assertEqual(
55             self._out[0],
56             ("Bad --header=name option, value must be in the form "
57              "'name:value'"))
58
59     def test_command_has_good_header_var(self):
60         command = self._makeOne(
a897b5 61             ['', '--header=name:value','development.ini', '/'],
BJR 62             [('Content-Type', 'text/html; charset=UTF-8')])
c8061e 63         command.run()
CM 64         self.assertEqual(self._environ['HTTP_NAME'], 'value')
65         self.assertEqual(self._path_info, '/')
66         self.assertEqual(self._out, ['abc'])
67
d3ee79 68     def test_command_w_basic_auth(self):
TS 69         command = self._makeOne(
70             ['', '--login=user:password',
a897b5 71                  '--header=name:value','development.ini', '/'],
BJR 72             [('Content-Type', 'text/html; charset=UTF-8')])
d3ee79 73         command.run()
TS 74         self.assertEqual(self._environ['HTTP_NAME'], 'value')
75         self.assertEqual(self._environ['HTTP_AUTHORIZATION'],
76                         'Basic dXNlcjpwYXNzd29yZA==')
77         self.assertEqual(self._path_info, '/')
78         self.assertEqual(self._out, ['abc'])
79
c8061e 80     def test_command_has_content_type_header_var(self):
CM 81         command = self._makeOne(
a897b5 82             ['', '--header=content-type:app/foo','development.ini', '/'],
BJR 83             [('Content-Type', 'text/html; charset=UTF-8')])
c8061e 84         command.run()
CM 85         self.assertEqual(self._environ['CONTENT_TYPE'], 'app/foo')
86         self.assertEqual(self._path_info, '/')
87         self.assertEqual(self._out, ['abc'])
88
89     def test_command_has_multiple_header_vars(self):
90         command = self._makeOne(
91             ['',
92              '--header=name:value',
93              '--header=name2:value2',
94              'development.ini',
a897b5 95              '/'],
BJR 96             [('Content-Type', 'text/html; charset=UTF-8')]
97             )
c8061e 98         command.run()
CM 99         self.assertEqual(self._environ['HTTP_NAME'], 'value')
100         self.assertEqual(self._environ['HTTP_NAME2'], 'value2')
101         self.assertEqual(self._path_info, '/')
102         self.assertEqual(self._out, ['abc'])
103
104     def test_command_method_get(self):
a897b5 105         command = self._makeOne(['', '--method=GET', 'development.ini', '/'],
BJR 106                 [('Content-Type', 'text/html; charset=UTF-8')])
c8061e 107         command.run()
acc5ec 108         self.assertEqual(self._environ['REQUEST_METHOD'], 'GET')
c8061e 109         self.assertEqual(self._path_info, '/')
CM 110         self.assertEqual(self._out, ['abc'])
111
112     def test_command_method_post(self):
113         from pyramid.compat import NativeIO
a897b5 114         command = self._makeOne(['', '--method=POST', 'development.ini', '/'],
BJR 115                 [('Content-Type', 'text/html; charset=UTF-8')])
c8061e 116         stdin = NativeIO()
CM 117         command.stdin = stdin
118         command.run()
acc5ec 119         self.assertEqual(self._environ['REQUEST_METHOD'], 'POST')
c8061e 120         self.assertEqual(self._environ['CONTENT_LENGTH'], '-1')
CM 121         self.assertEqual(self._environ['wsgi.input'], stdin)
122         self.assertEqual(self._path_info, '/')
123         self.assertEqual(self._out, ['abc'])
124
cd9d7f 125     def test_command_method_put(self):
ÉA 126         from pyramid.compat import NativeIO
a897b5 127         command = self._makeOne(['', '--method=PUT', 'development.ini', '/'],
BJR 128                 [('Content-Type', 'text/html; charset=UTF-8')])
cd9d7f 129         stdin = NativeIO()
ÉA 130         command.stdin = stdin
131         command.run()
acc5ec 132         self.assertEqual(self._environ['REQUEST_METHOD'], 'PUT')
cd9d7f 133         self.assertEqual(self._environ['CONTENT_LENGTH'], '-1')
ÉA 134         self.assertEqual(self._environ['wsgi.input'], stdin)
135         self.assertEqual(self._path_info, '/')
136         self.assertEqual(self._out, ['abc'])
137
138     def test_command_method_patch(self):
139         from pyramid.compat import NativeIO
a897b5 140         command = self._makeOne(['', '--method=PATCH', 'development.ini', '/'],
BJR 141                 [('Content-Type', 'text/html; charset=UTF-8')])
cd9d7f 142         stdin = NativeIO()
ÉA 143         command.stdin = stdin
144         command.run()
acc5ec 145         self.assertEqual(self._environ['REQUEST_METHOD'], 'PATCH')
cd9d7f 146         self.assertEqual(self._environ['CONTENT_LENGTH'], '-1')
ÉA 147         self.assertEqual(self._environ['wsgi.input'], stdin)
148         self.assertEqual(self._path_info, '/')
149         self.assertEqual(self._out, ['abc'])
150
acc5ec 151     def test_command_method_propfind(self):
TS 152         from pyramid.compat import NativeIO
153         command = self._makeOne(['', '--method=PROPFIND', 'development.ini',
a897b5 154                                 '/'],
BJR 155                                 [('Content-Type', 'text/html; charset=UTF-8')])
acc5ec 156         stdin = NativeIO()
TS 157         command.stdin = stdin
158         command.run()
159         self.assertEqual(self._environ['REQUEST_METHOD'], 'PROPFIND')
160         self.assertEqual(self._path_info, '/')
161         self.assertEqual(self._out, ['abc'])
162
163     def test_command_method_options(self):
164         from pyramid.compat import NativeIO
165         command = self._makeOne(['', '--method=OPTIONS', 'development.ini',
a897b5 166                                 '/'],
BJR 167                                 [('Content-Type', 'text/html; charset=UTF-8')])
acc5ec 168         stdin = NativeIO()
TS 169         command.stdin = stdin
170         command.run()
171         self.assertEqual(self._environ['REQUEST_METHOD'], 'OPTIONS')
172         self.assertEqual(self._path_info, '/')
173         self.assertEqual(self._out, ['abc'])
174
61d45f 175     def test_command_with_query_string(self):
a897b5 176         command = self._makeOne(['', 'development.ini', '/abc?a=1&b=2&c'],
BJR 177                 [('Content-Type', 'text/html; charset=UTF-8')])
c8061e 178         command.run()
61d45f 179         self.assertEqual(self._environ['QUERY_STRING'], 'a=1&b=2&c')
CM 180         self.assertEqual(self._path_info, '/abc')
c8061e 181         self.assertEqual(self._out, ['abc'])
CM 182
183     def test_command_display_headers(self):
184         command = self._makeOne(
a897b5 185             ['', '--display-headers', 'development.ini', '/'],
BJR 186             [('Content-Type', 'text/html; charset=UTF-8')])
c8061e 187         command.run()
CM 188         self.assertEqual(self._path_info, '/')
189         self.assertEqual(
190             self._out,
191             ['200 OK', 'Content-Type: text/html; charset=UTF-8', 'abc'])
192
8a3621 193     def test_command_response_has_no_charset(self):
CM 194         command = self._makeOne(['', '--method=GET', 'development.ini', '/'],
195                                 headers=[('Content-Type', 'image/jpeg')])
196         command.run()
197         self.assertEqual(self._path_info, '/')
aa9aef 198
8a3621 199         self.assertEqual(self._out, [b'abc'])
CM 200
aa9aef 201     def test_command_method_configures_logging(self):
JA 202         command = self._makeOne(['', 'development.ini', '/'])
203         command.run()
678790 204         self.assertEqual(self.loader.calls[0]['op'], 'logging')
aa9aef 205
JA 206
c8061e 207 class Test_main(unittest.TestCase):
CM 208     def _callFUT(self, argv):
209         from pyramid.scripts.prequest import main
210         return main(argv, True)
211
212     def test_it(self):
213         result = self._callFUT(['prequest'])
214         self.assertEqual(result, 2)