diff -r 656543a2ad75 Lib/test/test_wsgiref.py --- a/Lib/test/test_wsgiref.py Mon Mar 02 23:32:02 2015 -0800 +++ b/Lib/test/test_wsgiref.py Tue Mar 03 18:54:32 2015 +0200 @@ -60,6 +60,24 @@ ]).encode('iso-8859-1')] +def input_app_factory(func_name, *args): + def app(e,s): + req = getattr(e['wsgi.input'], func_name)(*args) + s("200 OK", [("Content-Type", "text/plain; charset=utf-8")]) + if type(req) is list: + resp = b";".join(req) + else: + resp = req + return [resp] + return app + +def errors_app_factory(func_name, *args): + def app(e,s): + getattr(e['wsgi.errors'], func_name)(*args) + s("200 OK", [("Content-Type", "text/plain; charset=utf-8")]) + return [b"data"] + return app + def run_amock(app=hello_app, data=b"GET / HTTP/1.0\n\n"): server = make_server("", 80, app, MockServer, MockHandler) inp = BufferedReader(BytesIO(data)) @@ -167,11 +185,10 @@ " be of type list: " ) - def test_wsgi_input(self): - def bad_app(e,s): - e["wsgi.input"].read() - s("200 OK", [("Content-Type", "text/plain; charset=utf-8")]) - return [b"data"] + def test_wsgi_input_read(self): + bad_app = input_app_factory("read") + good_app = input_app_factory("read", 5) + out, err = run_amock(validator(bad_app)) self.assertTrue(out.endswith( b"A server error occurred. Please contact the administrator." @@ -180,6 +197,83 @@ err.splitlines()[-2], "AssertionError" ) + out, err = run_amock(validator(good_app), b"GET / HTTP/1.0\n\nTest 1\nTest 2\n") + self.assertTrue(out.endswith(b"Test ")) + + + def test_wsgi_input_readlines(self): + bad_app = input_app_factory("readlines", 3, 5) + good_app = input_app_factory("readlines", 1) + + out, err = run_amock(validator(bad_app)) + self.assertTrue(out.endswith( + b"A server error occurred. Please contact the administrator." + )) + self.assertEqual( + err.splitlines()[-2], "AssertionError" + ) + + out, err = run_amock(validator(good_app), b"GET / HTTP/1.0\n\nTest 1\nTest 2\n") + self.assertTrue(out.endswith(b"Test 1\n")) + + def test_wsgi_input_readline(self): + bad_app = input_app_factory("readline", 3, 4) + good_app = input_app_factory("readline", 2) + + out, err = run_amock(validator(bad_app)) + self.assertTrue(out.endswith( + b"A server error occurred. Please contact the administrator." + )) + self.assertEqual( + err.splitlines()[-2], "AssertionError" + ) + + out, err = run_amock(validator(good_app), b"GET / HTTP/1.0\n\nTest 1\nTest 2\n") + self.assertTrue(out.endswith(b"Te")) + + def test_wsgi_input_close(self): + app = input_app_factory("close") + + out, err = run_amock(validator(app), b"GET / HTTP/1.0\n\nTest 1\nTest 2\n") + self.assertEqual(err.splitlines()[-2], 'AssertionError: input.close() must not be called') + + def test_wsgi_input_iter(self): + def app(e,s): + req = [] + for line in e['wsgi.input']: + req.append(line) + s("200 OK", [('Content-Type', 'text/plain; charser=utf-8')]) + return [b';'.join(req)] + + out, err = run_amock(validator(app), b"GET / HTTP/1.0\n\nTest 1\nTest 2\n") + self.assertTrue(out.endswith(b"Test 1\n;Test 2\n")) + + def test_wsgi_errors_write(self): + bad_app = errors_app_factory("write", b"Test") + good_app = errors_app_factory("write", "Test") + + out, err = run_amock(validator(bad_app), b"GET / HTTP/1.0\n\n") + self.assertEqual(err.splitlines()[-2], 'AssertionError') + + out, err = run_amock(validator(good_app), b"GET / HTTP/1.0\n\n") + self.assertTrue(err.startswith("Test")) + + def test_wsgi_errors_writelines(self): + bad_app = errors_app_factory("writelines", [1, "Test"]) + good_app = errors_app_factory("writelines", ["Test", "Test"]) + + out, err = run_amock(validator(bad_app), b"GET / HTTP/1.0\n\n") + self.assertEqual(err.splitlines()[-2], 'AssertionError') + + out, err = run_amock(validator(good_app), b"GET / HTTP/1.0\n\n") + self.assertTrue(err.startswith("TestTest")) + + def test_wsgi_errors_close(self): + app = errors_app_factory("close") + + out, err = run_amock(validator(app), b"GET / HTTP/1.0\n\n") + self.assertTrue(err.splitlines()[-2], 'AssertionError: errors.close() must not be called') + def test_bytes_validation(self): def app(e, s): s("200 OK", [