From 3b7238996ad68f3c56a031e72e0b2f9aa0c8662c Mon Sep 17 00:00:00 2001 From: Andrey Zelenkov Date: Tue, 22 May 2018 20:20:14 +0300 Subject: [PATCH] Tests: initial PHP application tests. --- test/php/404/404.html | 6 ++ test/php/404/index.php | 4 + test/php/conditional/index.php | 5 ++ test/php/cookies/index.php | 4 + test/php/get_variables/index.php | 7 ++ test/php/mirror/index.php | 5 ++ test/php/phpinfo/index.php | 3 + test/php/post_variables/index.php | 6 ++ test/php/variables/index.php | 13 ++++ test/test_php_application.py | 122 ++++++++++++++++++++++++++++++ test/unit.py | 18 +++++ 11 files changed, 193 insertions(+) create mode 100644 test/php/404/404.html create mode 100644 test/php/404/index.php create mode 100644 test/php/conditional/index.php create mode 100644 test/php/cookies/index.php create mode 100644 test/php/get_variables/index.php create mode 100644 test/php/mirror/index.php create mode 100644 test/php/phpinfo/index.php create mode 100644 test/php/post_variables/index.php create mode 100644 test/php/variables/index.php create mode 100644 test/test_php_application.py diff --git a/test/php/404/404.html b/test/php/404/404.html new file mode 100644 index 00000000..6d0c635a --- /dev/null +++ b/test/php/404/404.html @@ -0,0 +1,6 @@ + +404 Not Found + +

404 Not Found

+ + diff --git a/test/php/404/index.php b/test/php/404/index.php new file mode 100644 index 00000000..92afdf19 --- /dev/null +++ b/test/php/404/index.php @@ -0,0 +1,4 @@ + diff --git a/test/php/conditional/index.php b/test/php/conditional/index.php new file mode 100644 index 00000000..421e1a2d --- /dev/null +++ b/test/php/conditional/index.php @@ -0,0 +1,5 @@ + +True + +False + diff --git a/test/php/cookies/index.php b/test/php/cookies/index.php new file mode 100644 index 00000000..72a3f092 --- /dev/null +++ b/test/php/cookies/index.php @@ -0,0 +1,4 @@ + diff --git a/test/php/get_variables/index.php b/test/php/get_variables/index.php new file mode 100644 index 00000000..dd7ef985 --- /dev/null +++ b/test/php/get_variables/index.php @@ -0,0 +1,7 @@ + diff --git a/test/php/mirror/index.php b/test/php/mirror/index.php new file mode 100644 index 00000000..ffd2291d --- /dev/null +++ b/test/php/mirror/index.php @@ -0,0 +1,5 @@ + diff --git a/test/php/phpinfo/index.php b/test/php/phpinfo/index.php new file mode 100644 index 00000000..cf608608 --- /dev/null +++ b/test/php/phpinfo/index.php @@ -0,0 +1,3 @@ + diff --git a/test/php/post_variables/index.php b/test/php/post_variables/index.php new file mode 100644 index 00000000..5ea17324 --- /dev/null +++ b/test/php/post_variables/index.php @@ -0,0 +1,6 @@ + diff --git a/test/php/variables/index.php b/test/php/variables/index.php new file mode 100644 index 00000000..8f2e3bfc --- /dev/null +++ b/test/php/variables/index.php @@ -0,0 +1,13 @@ + diff --git a/test/test_php_application.py b/test/test_php_application.py new file mode 100644 index 00000000..9ac063c7 --- /dev/null +++ b/test/test_php_application.py @@ -0,0 +1,122 @@ +import unittest +import unit + +class TestUnitPHPApplication(unit.TestUnitApplicationPHP): + + def setUpClass(): + unit.TestUnit().check_modules('php') + + def test_php_application_variables(self): + self.load('variables') + + body = 'Test body string.' + + resp = self.post(headers={ + 'Host': 'localhost', + 'Content-Type': 'text/html', + 'Custom-Header': 'blah' + }, body=body) + + self.assertEqual(resp['status'], 200, 'status') + headers = resp['headers'] + header_server = headers.pop('Server') + self.assertRegex(header_server, r'Unit/[\d\.]+', 'server header') + self.assertEqual(headers.pop('Server-Software'), header_server, + 'server software header') + + date = headers.pop('Date') + self.assertEqual(date[-4:], ' GMT', 'date header timezone') + self.assertLess(abs(self.date_to_sec_epoch(date) - self.sec_epoch()), 5, + 'date header') + + if 'X-Powered-By' in headers: + headers.pop('X-Powered-By') + + headers.pop('Content-type') + self.assertDictEqual(headers, { + 'Content-Length': str(len(body)), + 'Request-Method': 'POST', + 'Request-Uri': '/', + 'Http-Host': 'localhost', + 'Server-Protocol': 'HTTP/1.1', + 'Custom-Header': 'blah' + }, 'headers') + self.assertEqual(resp['body'], body, 'body') + + def test_php_application_phpinfo(self): + self.load('phpinfo') + + resp = self.get() + + self.assertEqual(resp['status'], 200, 'status') + self.assertNotEqual(resp['body'], '', 'body not empty') + + def test_php_application_404(self): + self.load('404') + + resp = self.get() + + self.assertEqual(resp['status'], 404, '404 status') + self.assertRegex(resp['body'], r'404 Not Found', + '404 body') + + def test_php_application_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_php_application_conditional(self): + self.load('conditional') + + self.assertRegex(self.get()['body'], r'True', 'conditional true') + self.assertRegex(self.post()['body'], r'False', 'conditional false') + + def test_php_application_get_variables(self): + self.load('get_variables') + + resp = self.get(url='/?var1=val1&var2=&var3') + self.assertEqual(resp['headers']['X-Var-1'], 'val1', 'GET variables') + self.assertEqual(resp['headers']['X-Var-2'], '1', 'GET variables 2') + self.assertEqual(resp['headers']['X-Var-3'], '1', 'GET variables 3') + self.assertEqual(resp['headers']['X-Var-4'], '', 'GET variables 4') + + def test_php_application_post_variables(self): + self.load('post_variables') + + resp = self.post(headers={ + 'Content-Type': 'application/x-www-form-urlencoded', + 'Host': 'localhost', + 'Connection': 'close' + }, body='var1=val1&var2=') + self.assertEqual(resp['headers']['X-Var-1'], 'val1', 'POST variables') + self.assertEqual(resp['headers']['X-Var-2'], '1', 'POST variables 2') + self.assertEqual(resp['headers']['X-Var-3'], '', 'POST variables 3') + + def test_php_application_cookies(self): + self.load('cookies') + + resp = self.get(headers={ + 'Cookie': 'var=val; var2=val2', + 'Host': 'localhost', + 'Connection': 'close' + }) + + self.assertEqual(resp['headers']['X-Cookie-1'], 'val', 'cookie') + self.assertEqual(resp['headers']['X-Cookie-2'], 'val2', 'cookie') + +if __name__ == '__main__': + unittest.main() diff --git a/test/unit.py b/test/unit.py index e1bf2374..2f187d91 100644 --- a/test/unit.py +++ b/test/unit.py @@ -382,6 +382,24 @@ class TestUnitApplicationRuby(TestUnitApplicationProto): } }) +class TestUnitApplicationPHP(TestUnitApplicationProto): + def load(self, script, name='index.php'): + self.conf({ + "listeners": { + "*:7080": { + "application": script + } + }, + "applications": { + script: { + "type": "php", + "processes": { "spare": 0 }, + "root": self.current_dir + '/php/' + script, + "index": name + } + } + }) + class TestUnitApplicationPerl(TestUnitApplicationProto): def load(self, script, name='psgi.pl'): self.conf({