Tests: more Python tests.

This commit is contained in:
Andrey Zelenkov
2018-04-02 17:03:41 +03:00
parent efb71121b9
commit a01a98ce36
17 changed files with 215 additions and 69 deletions

View File

@@ -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 []

View File

@@ -0,0 +1,3 @@
def application(env, start_response):
start_response('200', [('Content-Length', '10')])
return [b'0123', b'4567', b'89']

View File

@@ -0,0 +1,6 @@
import io
def application(env, start_response):
start_response('200', [('Content-Length', '10')])
f = io.BytesIO(b'0123456789')
return f

View File

@@ -0,0 +1 @@
body

View File

@@ -0,0 +1,4 @@
def application(env, start_response):
start_response('200', [('Content-Length', '5')])
f = open('file', 'rb')
return f

10
test/python/close/wsgi.py Normal file
View File

@@ -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.')

View File

@@ -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.')

View File

@@ -0,0 +1,5 @@
def application(environ, start_response):
environ['wsgi.errors'].write('Error in application.')
start_response('200', [('Content-Length', '0')])
return []

View File

@@ -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]

View File

@@ -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]

View File

@@ -0,0 +1,4 @@
def application(env, start_response):
start_response('200', [('Content-Length', '1')])
exit()
return [b'X']

View File

@@ -0,0 +1,3 @@
def application(env, start_response)
start_response('200', [('Content-Length', '0')])
return []

View File

@@ -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]

View File

@@ -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()

View File

@@ -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()

View File

@@ -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()

View File

@@ -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"
}
}