diff options
author | Andrey Zelenkov <zelenkov@nginx.com> | 2018-04-02 17:03:41 +0300 |
---|---|---|
committer | Andrey Zelenkov <zelenkov@nginx.com> | 2018-04-02 17:03:41 +0300 |
commit | a01a98ce36dd47c1be1df37470cac1656efac31e (patch) | |
tree | a2b962d568e14562b9274d142b239eedc6eae1df /test | |
parent | efb71121b98c4dfded212e8b6fe33873cfd83376 (diff) | |
download | unit-a01a98ce36dd47c1be1df37470cac1656efac31e.tar.gz unit-a01a98ce36dd47c1be1df37470cac1656efac31e.tar.bz2 |
Tests: more Python tests.
Diffstat (limited to 'test')
-rw-r--r-- | test/python/atexit/wsgi.py | 8 | ||||
-rw-r--r-- | test/python/body_array/wsgi.py | 3 | ||||
-rw-r--r-- | test/python/body_io/wsgi.py | 6 | ||||
-rw-r--r-- | test/python/body_io_file/file | 1 | ||||
-rw-r--r-- | test/python/body_io_file/wsgi.py | 4 | ||||
-rw-r--r-- | test/python/close/wsgi.py | 10 | ||||
-rw-r--r-- | test/python/close_error/wsgi.py | 10 | ||||
-rw-r--r-- | test/python/errors_write/wsgi.py | 5 | ||||
-rw-r--r-- | test/python/input_iter/wsgi.py | 5 | ||||
-rw-r--r-- | test/python/input_read_length/wsgi.py | 7 | ||||
-rw-r--r-- | test/python/start_response_exit/wsgi.py | 4 | ||||
-rw-r--r-- | test/python/syntax_error/wsgi.py | 3 | ||||
-rw-r--r-- | test/python/variables/wsgi.py | 7 | ||||
-rw-r--r-- | test/test_python_application.py | 146 | ||||
-rw-r--r-- | test/test_python_atexit.py | 31 | ||||
-rw-r--r-- | test/test_python_keepalive.py | 29 | ||||
-rw-r--r-- | test/unit.py | 5 |
17 files changed, 215 insertions, 69 deletions
diff --git a/test/python/atexit/wsgi.py b/test/python/atexit/wsgi.py index 9ad300b9..561d8049 100644 --- a/test/python/atexit/wsgi.py +++ b/test/python/atexit/wsgi.py @@ -1,12 +1,10 @@ import atexit def application(environ, start_response): - test_dir = environ.get('HTTP_TEST_DIR') + def at_exit(): + environ['wsgi.errors'].write('At exit called.') - def create_file(): - open(test_dir + '/atexit', 'w') - - atexit.register(create_file) + atexit.register(at_exit) start_response('200', [('Content-Length', '0')]) return [] diff --git a/test/python/body_array/wsgi.py b/test/python/body_array/wsgi.py new file mode 100644 index 00000000..2390f222 --- /dev/null +++ b/test/python/body_array/wsgi.py @@ -0,0 +1,3 @@ +def application(env, start_response): + start_response('200', [('Content-Length', '10')]) + return [b'0123', b'4567', b'89'] diff --git a/test/python/body_io/wsgi.py b/test/python/body_io/wsgi.py new file mode 100644 index 00000000..14303b5f --- /dev/null +++ b/test/python/body_io/wsgi.py @@ -0,0 +1,6 @@ +import io + +def application(env, start_response): + start_response('200', [('Content-Length', '10')]) + f = io.BytesIO(b'0123456789') + return f diff --git a/test/python/body_io_file/file b/test/python/body_io_file/file new file mode 100644 index 00000000..273a402f --- /dev/null +++ b/test/python/body_io_file/file @@ -0,0 +1 @@ +body diff --git a/test/python/body_io_file/wsgi.py b/test/python/body_io_file/wsgi.py new file mode 100644 index 00000000..713e341b --- /dev/null +++ b/test/python/body_io_file/wsgi.py @@ -0,0 +1,4 @@ +def application(env, start_response): + start_response('200', [('Content-Length', '5')]) + f = open('file', 'rb') + return f diff --git a/test/python/close/wsgi.py b/test/python/close/wsgi.py new file mode 100644 index 00000000..f80a34d7 --- /dev/null +++ b/test/python/close/wsgi.py @@ -0,0 +1,10 @@ +class application: + def __init__(self, environ, start_response): + self.environ = environ + self.start = start_response + + def __iter__(self): + self.start('200', [(('Content-Length', '0'))]) + + def close(self): + self.environ['wsgi.errors'].write('Close called.') diff --git a/test/python/close_error/wsgi.py b/test/python/close_error/wsgi.py new file mode 100644 index 00000000..bd9d4d36 --- /dev/null +++ b/test/python/close_error/wsgi.py @@ -0,0 +1,10 @@ +class application: + def __init__(self, environ, start_response): + self.environ = environ + self.start = start_response + + def __iter__(self): + self.start('200', [(('!', '0'))]) + + def close(self): + self.environ['wsgi.errors'].write('Close called.') diff --git a/test/python/errors_write/wsgi.py b/test/python/errors_write/wsgi.py new file mode 100644 index 00000000..b1a9d2ee --- /dev/null +++ b/test/python/errors_write/wsgi.py @@ -0,0 +1,5 @@ +def application(environ, start_response): + environ['wsgi.errors'].write('Error in application.') + + start_response('200', [('Content-Length', '0')]) + return [] diff --git a/test/python/input_iter/wsgi.py b/test/python/input_iter/wsgi.py new file mode 100644 index 00000000..d3bf437f --- /dev/null +++ b/test/python/input_iter/wsgi.py @@ -0,0 +1,5 @@ +def application(environ, start_response): + body = bytes(environ['wsgi.input'].__iter__()) + + start_response('200', [('Content-Length', str(len(body)))]) + return [body] diff --git a/test/python/input_read_length/wsgi.py b/test/python/input_read_length/wsgi.py new file mode 100644 index 00000000..9d209556 --- /dev/null +++ b/test/python/input_read_length/wsgi.py @@ -0,0 +1,7 @@ +def application(environ, start_response): + + input_length = int(environ.get('HTTP_INPUT_LENGTH')) + body = bytes(environ['wsgi.input'].read(input_length)) + + start_response('200', [('Content-Length', str(len(body)))]) + return [body] diff --git a/test/python/start_response_exit/wsgi.py b/test/python/start_response_exit/wsgi.py new file mode 100644 index 00000000..37c3acfe --- /dev/null +++ b/test/python/start_response_exit/wsgi.py @@ -0,0 +1,4 @@ +def application(env, start_response): + start_response('200', [('Content-Length', '1')]) + exit() + return [b'X'] diff --git a/test/python/syntax_error/wsgi.py b/test/python/syntax_error/wsgi.py new file mode 100644 index 00000000..0ac759de --- /dev/null +++ b/test/python/syntax_error/wsgi.py @@ -0,0 +1,3 @@ +def application(env, start_response) + start_response('200', [('Content-Length', '0')]) + return [] diff --git a/test/python/variables/wsgi.py b/test/python/variables/wsgi.py index 1d4d397d..60fa3401 100644 --- a/test/python/variables/wsgi.py +++ b/test/python/variables/wsgi.py @@ -10,6 +10,11 @@ def application(environ, start_response): ('Request-Uri', environ.get('REQUEST_URI')), ('Http-Host', environ.get('HTTP_HOST')), ('Server-Protocol', environ.get('SERVER_PROTOCOL')), - ('Custom-Header', environ.get('HTTP_CUSTOM_HEADER')) + ('Custom-Header', environ.get('HTTP_CUSTOM_HEADER')), + ('Wsgi-Version', str(environ['wsgi.version'])), + ('Wsgi-Url-Scheme', environ['wsgi.url_scheme']), + ('Wsgi-Multithread', str(environ['wsgi.multithread'])), + ('Wsgi-Multiprocess', str(environ['wsgi.multiprocess'])), + ('Wsgi-Run-Once', str(environ['wsgi.run_once'])) ]) return [body] diff --git a/test/test_python_application.py b/test/test_python_application.py index 0307a8f6..7e1df887 100644 --- a/test/test_python_application.py +++ b/test/test_python_application.py @@ -34,7 +34,12 @@ class TestUnitPythonApplication(unit.TestUnitApplicationPython): 'Request-Uri': '/', 'Http-Host': 'localhost', 'Server-Protocol': 'HTTP/1.1', - 'Custom-Header': 'blah' + 'Custom-Header': 'blah', + 'Wsgi-Version': '(1, 0)', + 'Wsgi-Url-Scheme': 'http', + 'Wsgi-Multithread': 'False', + 'Wsgi-Multiprocess': 'True', + 'Wsgi-Run-Once': 'False' }, 'headers') self.assertEqual(resp['body'], body, 'body') @@ -83,5 +88,144 @@ class TestUnitPythonApplication(unit.TestUnitApplicationPython): self.assertIsNotNone(self.search_in_log(r'RuntimeError'), 'ctx iter atexit') + def test_python_keepalive_body(self): + self.load('mirror') + + (resp, sock) = self.post(headers={ + 'Connection': 'keep-alive', + 'Content-Type': 'text/html', + 'Host': 'localhost' + }, start=True, body='0123456789' * 500) + + self.assertEqual(resp['body'], '0123456789' * 500, 'keep-alive 1') + + resp = self.post(headers={ + 'Connection': 'close', + 'Content-Type': 'text/html', + 'Host': 'localhost' + }, sock=sock, body='0123456789') + + self.assertEqual(resp['body'], '0123456789', 'keep-alive 2') + + def test_python_atexit(self): + self.skip_alerts.append(r'sendmsg.+failed') + self.load('atexit') + + self.get() + + self.conf({ + "listeners": {}, + "applications": {} + }) + + self.stop() + + self.assertIsNotNone(self.search_in_log(r'At exit called\.'), 'atexit') + + @unittest.expectedFailure + def test_python_application_start_response_exit(self): + self.load('start_response_exit') + + self.assertEqual(self.get()['status'], 500, 'start response exit') + + @unittest.expectedFailure + def test_python_application_input_iter(self): + self.load('input_iter') + + body = '0123456789' + + self.assertEqual(self.post(body=body)['body'], body, 'input iter') + + @unittest.expectedFailure + def test_python_application_input_read_length(self): + self.load('input_read_length') + + body = '0123456789' + + resp = self.post(headers={ + 'Host': 'localhost', + 'Input-Length': '5', + 'Connection': 'close' + }, body=body) + + self.assertEqual(resp['body'], body[:5], 'input read length lt body') + + resp = self.post(headers={ + 'Host': 'localhost', + 'Input-Length': '15', + 'Connection': 'close' + }, body=body) + + self.assertEqual(resp['body'], body, 'input read length gt body') + + resp = self.post(headers={ + 'Host': 'localhost', + 'Input-Length': '0', + 'Connection': 'close' + }, body=body) + + self.assertEqual(resp['body'], '', 'input read length zero') + + resp = self.post(headers={ + 'Host': 'localhost', + 'Input-Length': '-1', + 'Connection': 'close' + }, body=body) + + self.assertEqual(resp['body'], body, 'input read length negative') + + @unittest.expectedFailure + def test_python_application_errors_write(self): + self.load('errors_write') + + self.get() + + self.stop() + + self.assertIsNotNone( + self.search_in_log(r'\[error\].+Error in application\.'), + 'errors write') + + def test_python_application_body_array(self): + self.load('body_array') + + self.assertEqual(self.get()['body'], '0123456789', 'body array') + + def test_python_application_body_io(self): + self.load('body_io') + + self.assertEqual(self.get()['body'], '0123456789', 'body io') + + def test_python_application_body_io_file(self): + self.load('body_io_file') + + self.assertEqual(self.get()['body'], 'body\n', 'body io file') + + @unittest.expectedFailure + def test_python_application_syntax_error(self): + self.skip_alerts.append(r'Python failed to import module "wsgi"') + self.load('syntax_error') + + self.assertEqual(self.get()['status'], 500, 'syntax error') + + def test_python_application_close(self): + self.load('close') + + self.get() + + self.stop() + + self.assertIsNotNone(self.search_in_log(r'Close called\.'), 'close') + + def test_python_application_close_error(self): + self.load('close_error') + + self.get() + + self.stop() + + self.assertIsNotNone(self.search_in_log(r'Close called\.'), + 'close error') + if __name__ == '__main__': unittest.main() diff --git a/test/test_python_atexit.py b/test/test_python_atexit.py deleted file mode 100644 index a115a192..00000000 --- a/test/test_python_atexit.py +++ /dev/null @@ -1,31 +0,0 @@ -import os -import time -import unittest -import unit - -class TestUnitPythonAtExit(unit.TestUnitApplicationPython): - - def setUpClass(): - unit.TestUnit().check_modules('python') - - def test_python_atexit(self): - self.load('atexit') - - self.get(headers={ - 'Host': 'localhost', - 'Test-Dir': self.testdir, - 'Connection': 'close' - }) - - self.conf({ - "listeners": {}, - "applications": {} - }) - - time.sleep(0.2) # wait for 'atexit' file - - self.assertEqual(os.path.exists(self.testdir + '/atexit'), True, - 'python atexit') - -if __name__ == '__main__': - unittest.main() diff --git a/test/test_python_keepalive.py b/test/test_python_keepalive.py deleted file mode 100644 index 0472a15b..00000000 --- a/test/test_python_keepalive.py +++ /dev/null @@ -1,29 +0,0 @@ -import unittest -import unit - -class TestUnitPythonKeepalive(unit.TestUnitApplicationPython): - - def setUpClass(): - unit.TestUnit().check_modules('python') - - def test_python_keepalive_body(self): - self.load('mirror') - - (resp, sock) = self.post(headers={ - 'Connection': 'keep-alive', - 'Content-Type': 'text/html', - 'Host': 'localhost' - }, start=True, body='0123456789' * 500) - - self.assertEqual(resp['body'], '0123456789' * 500, 'keep-alive 1') - - resp = self.post(headers={ - 'Connection': 'close', - 'Content-Type': 'text/html', - 'Host': 'localhost' - }, sock=sock, body='0123456789') - - self.assertEqual(resp['body'], '0123456789', 'keep-alive 2') - -if __name__ == '__main__': - unittest.main() diff --git a/test/unit.py b/test/unit.py index e522e2da..01231879 100644 --- a/test/unit.py +++ b/test/unit.py @@ -28,8 +28,8 @@ class TestUnit(unittest.TestCase): errors='ignore') as f: self._check_alerts(f.read()) - if '--leave' not in sys.argv: - shutil.rmtree(self.testdir) + #if '--leave' not in sys.argv: + # shutil.rmtree(self.testdir) def check_modules(self, *modules): self._run() @@ -357,6 +357,7 @@ class TestUnitApplicationPython(TestUnitApplicationProto): "type": "python", "processes": { "spare": 0 }, "path": self.current_dir + '/python/' + script, + "working_directory": self.current_dir + '/python/' + script, "module": "wsgi" } } |