@@ -66,6 +66,26 @@ def header_app(environ, start_response):
6666 ]).encode ('iso-8859-1' )]
6767
6868
69+ def input_app (func_name , * args ):
70+ def app (e ,s ):
71+ req = getattr (e ['wsgi.input' ], func_name )(* args )
72+ s ("200 OK" , [("Content-Type" , "text/plain; charset=utf-8" )])
73+ if type (req ) is list :
74+ resp = b";" .join (req )
75+ else :
76+ resp = req
77+ return [resp ]
78+ return app
79+
80+
81+ def errors_app (func_name , * args ):
82+ def app (e ,s ):
83+ getattr (e ['wsgi.errors' ], func_name )(* args )
84+ s ("200 OK" , [("Content-Type" , "text/plain; charset=utf-8" )])
85+ return [b"data" ]
86+ return app
87+
88+
6989def run_amock (app = hello_app , data = b"GET / HTTP/1.0\n \n " ):
7090 server = make_server ("" , 80 , app , MockServer , MockHandler )
7191 inp = BufferedReader (BytesIO (data ))
@@ -192,6 +212,95 @@ def bad_app(e,s):
192212 err .splitlines ()[- 2 ], "AssertionError"
193213 )
194214
215+ def test_wsgi_input_read (self ):
216+ bad_app = input_app ("read" )
217+ good_app = input_app ("read" , 5 )
218+
219+ out , err = run_amock (validator (bad_app ))
220+ self .assertEndsWith (out ,
221+ b"A server error occurred. Please contact the administrator."
222+ )
223+
224+ self .assertEqual (
225+ err .splitlines ()[- 2 ], "AssertionError"
226+ )
227+
228+ out , err = run_amock (validator (good_app ), b"GET / HTTP/1.0\n \n Test 1\n Test 2\n " )
229+ self .assertEndsWith (out , b"Test " )
230+
231+ def test_wsgi_input_readlines (self ):
232+ bad_app = input_app ("readlines" , 3 , 5 )
233+ good_app = input_app ("readlines" , 1 )
234+
235+ out , err = run_amock (validator (bad_app ))
236+ self .assertEndsWith (out ,
237+ b"A server error occurred. Please contact the administrator."
238+ )
239+ self .assertEqual (
240+ err .splitlines ()[- 2 ], "AssertionError"
241+ )
242+ out , err = run_amock (validator (good_app ), b"GET / HTTP/1.0\n \n Test Line 1\n Test Line 2\n " )
243+ self .assertEndsWith (out , b"Test Line 1\n " )
244+
245+ def test_wsgi_input_readline (self ):
246+ bad_app = input_app ("readline" , 3 , 4 )
247+ good_app = input_app ("readline" , 2 )
248+
249+ out , err = run_amock (validator (bad_app ))
250+ self .assertEndsWith (out ,
251+ b"A server error occurred. Please contact the administrator."
252+ )
253+ self .assertEqual (
254+ err .splitlines ()[- 2 ], "AssertionError"
255+ )
256+
257+ out , err = run_amock (validator (good_app ), b"GET / HTTP/1.0\n \n Test 1\n Test 2\n " )
258+ self .assertEndsWith (out , b"Te" )
259+
260+ def test_wsgi_input_close (self ):
261+ app = input_app ("close" )
262+ out , err = run_amock (validator (app ), b"GET / HTTP/1.0\n \n Test 1\n Test 2\n " )
263+ self .assertEqual (err .splitlines ()[- 2 ], 'AssertionError: input.close() must not be called' )
264+ self .assertEndsWith (out , b"A server error occurred. Please contact the administrator." )
265+
266+ def test_wsgi_input_iter (self ):
267+ def app (e ,s ):
268+ req = []
269+ for line in e ['wsgi.input' ]:
270+ req .append (line )
271+ s ("200 OK" , [('Content-Type' , 'text/plain; charser=utf-8' )])
272+ return [b';' .join (req )]
273+
274+ out , err = run_amock (validator (app ), b"GET / HTTP/1.0\n \n Test 1\n Test 2\n " )
275+ self .assertEndsWith (out , b"Test 1\n ;Test 2\n " )
276+
277+ def test_wsgi_errors_write (self ):
278+ bad_app = errors_app ("write" , b"Test" )
279+ good_app = errors_app ("write" , "Test" )
280+
281+ out , err = run_amock (validator (bad_app ), b"GET / HTTP/1.0\n \n " )
282+ self .assertEqual (err .splitlines ()[- 2 ], 'AssertionError' )
283+
284+ out , err = run_amock (validator (good_app ), b"GET / HTTP/1.0\n \n " )
285+ self .assertStartsWith (err , "Test" )
286+
287+ def test_wsgi_errors_writelines (self ):
288+ bad_app = errors_app ("writelines" , [1 , "Test" ])
289+ good_app = errors_app ("writelines" , ["Test" , "Test" ])
290+
291+ out , err = run_amock (validator (bad_app ), b"GET / HTTP/1.0\n \n " )
292+ self .assertEqual (err .splitlines ()[- 2 ], 'AssertionError' )
293+
294+ out , err = run_amock (validator (good_app ), b"GET / HTTP/1.0\n \n " )
295+ self .assertStartsWith (err , "TestTest" )
296+
297+ def test_wsgi_errors_close (self ):
298+ app = errors_app ("close" )
299+
300+ out , err = run_amock (validator (app ), b"GET / HTTP/1.0\n \n " )
301+ self .assertEqual (err .splitlines ()[- 2 ],
302+ 'AssertionError: errors.close() must not be called' )
303+
195304 @force_not_colorized
196305 def test_bytes_validation (self ):
197306 def app (e , s ):
0 commit comments