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