diff options
author | Andrei Zeliankou <zelenkov@nginx.com> | 2020-03-27 15:50:09 +0000 |
---|---|---|
committer | Andrei Zeliankou <zelenkov@nginx.com> | 2020-03-27 15:50:09 +0000 |
commit | 6e5b5d2a0b97f6e981fef749a267103e21898a72 (patch) | |
tree | 4360af3c04f6fe8ddc5090dfad75f8cd456968e8 | |
parent | 5f2d07019c44855deabfc3f0a83ab7506f0d0183 (diff) | |
download | unit-6e5b5d2a0b97f6e981fef749a267103e21898a72.tar.gz unit-6e5b5d2a0b97f6e981fef749a267103e21898a72.tar.bz2 |
Tests: added tests for "return" action.
-rw-r--r-- | test/test_return.py | 103 | ||||
-rw-r--r-- | test/test_routing.py | 161 | ||||
-rw-r--r-- | test/test_routing_tls.py | 26 | ||||
-rw-r--r-- | test/test_share_fallback.py | 171 |
4 files changed, 191 insertions, 270 deletions
diff --git a/test/test_return.py b/test/test_return.py new file mode 100644 index 00000000..f973f05b --- /dev/null +++ b/test/test_return.py @@ -0,0 +1,103 @@ +import re +import unittest +from unit.applications.proto import TestApplicationProto + + +class TestReturn(TestApplicationProto): + prerequisites = {} + + def setUp(self): + super().setUp() + + self._load_conf( + { + "listeners": {"*:7080": {"pass": "routes"}}, + "routes": [{"action": {"return": 200}}], + "applications": {}, + } + ) + + def get_resps_sc(self, req=10): + to_send = b"""GET / HTTP/1.1 +Host: localhost + +""" * ( + req - 1 + ) + + to_send += b"""GET / HTTP/1.1 +Host: localhost +Connection: close + +""" + + return self.http(to_send, raw_resp=True, raw=True) + + def test_return(self): + resp = self.get() + self.assertEqual(resp['status'], 200) + self.assertIn('Server', resp['headers']) + self.assertIn('Date', resp['headers']) + self.assertEqual(resp['headers']['Content-Length'], '0') + self.assertEqual(resp['headers']['Connection'], 'close') + self.assertEqual(resp['body'], '', 'body') + + resp = self.post(body='blah') + self.assertEqual(resp['status'], 200) + self.assertEqual(resp['body'], '', 'body') + + resp = self.get_resps_sc() + self.assertEqual(len(re.findall('200 OK', resp)), 10) + self.assertEqual(len(re.findall('Connection:', resp)), 1) + self.assertEqual(len(re.findall('Connection: close', resp)), 1) + + resp = self.get(http_10=True) + self.assertEqual(resp['status'], 200) + self.assertIn('Server', resp['headers']) + self.assertIn('Date', resp['headers']) + self.assertEqual(resp['headers']['Content-Length'], '0') + self.assertNotIn('Connection', resp['headers']) + self.assertEqual(resp['body'], '', 'body') + + def test_return_update(self): + self.assertIn('success', self.conf('0', 'routes/0/action/return')) + + resp = self.get() + self.assertEqual(resp['status'], 0) + self.assertEqual(resp['body'], '') + + self.assertIn('success', self.conf('404', 'routes/0/action/return')) + + resp = self.get() + self.assertEqual(resp['status'], 404) + self.assertNotEqual(resp['body'], '') + + self.assertIn('success', self.conf('598', 'routes/0/action/return')) + + resp = self.get() + self.assertEqual(resp['status'], 598) + self.assertNotEqual(resp['body'], '') + + self.assertIn('success', self.conf('999', 'routes/0/action/return')) + + resp = self.get() + self.assertEqual(resp['status'], 999) + self.assertEqual(resp['body'], '') + + def test_return_invalid(self): + def check_error(conf): + self.assertIn('error', self.conf(conf, 'routes/0/action')) + + check_error({"return": "200"}) + check_error({"return": []}) + check_error({"return": 80.}) + check_error({"return": 1000}) + check_error({"return": 200, "share": "/blah"}) + + self.assertIn( + 'error', self.conf('001', 'routes/0/action/return'), 'leading zero' + ) + + +if __name__ == '__main__': + TestReturn.main() diff --git a/test/test_routing.py b/test/test_routing.py index 950923d6..bf741706 100644 --- a/test/test_routing.py +++ b/test/test_routing.py @@ -16,27 +16,10 @@ class TestRouting(TestApplicationProto): "routes": [ { "match": {"method": "GET"}, - "action": {"pass": "applications/empty"}, + "action": {"return": 200}, } ], - "applications": { - "empty": { - "type": "python", - "processes": {"spare": 0}, - "path": self.current_dir + '/python/empty', - "working_directory": self.current_dir - + '/python/empty', - "module": "wsgi", - }, - "mirror": { - "type": "python", - "processes": {"spare": 0}, - "path": self.current_dir + '/python/mirror', - "working_directory": self.current_dir - + '/python/mirror', - "module": "wsgi", - }, - }, + "applications": {}, } ), 'routing configure', @@ -48,18 +31,14 @@ class TestRouting(TestApplicationProto): def route_match(self, match): self.assertIn( 'success', - self.route( - {"match": match, "action": {"pass": "applications/empty"}} - ), + self.route({"match": match, "action": {"return": 200}}), 'route match configure', ) def route_match_invalid(self, match): self.assertIn( 'error', - self.route( - {"match": match, "action": {"pass": "applications/empty"}} - ), + self.route({"match": match, "action": {"return": 200}}), 'route match configure invalid', ) @@ -233,24 +212,7 @@ class TestRouting(TestApplicationProto): { "listeners": {"*:7080": {"pass": "routes/main"}}, "routes": {"main": []}, - "applications": { - "empty": { - "type": "python", - "processes": {"spare": 0}, - "path": self.current_dir + '/python/empty', - "working_directory": self.current_dir - + '/python/empty', - "module": "wsgi", - }, - "mirror": { - "type": "python", - "processes": {"spare": 0}, - "path": self.current_dir + '/python/mirror', - "working_directory": self.current_dir - + '/python/mirror', - "module": "wsgi", - }, - }, + "applications": {}, } ), 'route empty configure', @@ -272,7 +234,7 @@ class TestRouting(TestApplicationProto): def test_routes_route_match_absent(self): self.assertIn( 'success', - self.conf([{"action": {"pass": "applications/empty"}}], 'routes'), + self.conf([{"action": {"return": 200}}], 'routes'), 'route match absent configure', ) @@ -349,14 +311,8 @@ class TestRouting(TestApplicationProto): 'success', self.conf( [ - { - "match": {"method": "GET"}, - "action": {"pass": "applications/empty"}, - }, - { - "match": {"method": "POST"}, - "action": {"pass": "applications/mirror"}, - }, + {"match": {"method": "GET"}, "action": {"return": 200}}, + {"match": {"method": "POST"}, "action": {"return": 201}}, ], 'routes', ), @@ -364,18 +320,7 @@ class TestRouting(TestApplicationProto): ) self.assertEqual(self.get()['status'], 200, 'rules two match first') - self.assertEqual( - self.post( - headers={ - 'Host': 'localhost', - 'Content-Type': 'text/html', - 'Connection': 'close', - }, - body='X', - )['status'], - 200, - 'rules two match second', - ) + self.assertEqual(self.post()['status'], 201, 'rules two match second') def test_routes_two(self): self.assertIn( @@ -393,20 +338,11 @@ class TestRouting(TestApplicationProto): "second": [ { "match": {"host": "localhost"}, - "action": {"pass": "applications/empty"}, + "action": {"return": 200}, } ], }, - "applications": { - "empty": { - "type": "python", - "processes": {"spare": 0}, - "path": self.current_dir + '/python/empty', - "working_directory": self.current_dir - + '/python/empty', - "module": "wsgi", - } - }, + "applications": {}, } ), 'routes two configure', @@ -556,7 +492,7 @@ class TestRouting(TestApplicationProto): self.assertIn( 'success', - self.conf([{"action": {"pass": "applications/empty"}}], 'routes'), + self.conf([{"action": {"return": 200}}], 'routes'), 'redefine 2', ) self.assertEqual(self.get()['status'], 200, 'redefine request 2') @@ -569,19 +505,8 @@ class TestRouting(TestApplicationProto): self.conf( { "listeners": {"*:7080": {"pass": "routes/main"}}, - "routes": { - "main": [{"action": {"pass": "applications/empty"}}] - }, - "applications": { - "empty": { - "type": "python", - "processes": {"spare": 0}, - "path": self.current_dir + '/python/empty', - "working_directory": self.current_dir - + '/python/empty', - "module": "wsgi", - } - }, + "routes": {"main": [{"action": {"return": 200}}]}, + "applications": {}, } ), 'redefine 4', @@ -595,25 +520,19 @@ class TestRouting(TestApplicationProto): self.assertIn( 'success', - self.conf_post( - {"action": {"pass": "applications/empty"}}, 'routes/main' - ), + self.conf_post({"action": {"return": 200}}, 'routes/main'), 'redefine 6', ) self.assertEqual(self.get()['status'], 200, 'redefine request 6') self.assertIn( 'error', - self.conf( - {"action": {"pass": "applications/empty"}}, 'routes/main/2' - ), + self.conf({"action": {"return": 200}}, 'routes/main/2'), 'redefine 7', ) self.assertIn( 'success', - self.conf( - {"action": {"pass": "applications/empty"}}, 'routes/main/1' - ), + self.conf({"action": {"return": 201}}, 'routes/main/1'), 'redefine 8', ) @@ -631,10 +550,7 @@ class TestRouting(TestApplicationProto): self.assertIn( 'success', self.conf_post( - { - "match": {"method": "POST"}, - "action": {"pass": "applications/empty"}, - }, + {"match": {"method": "POST"}, "action": {"return": 200}}, 'routes', ), 'routes edit configure 2', @@ -654,9 +570,7 @@ class TestRouting(TestApplicationProto): self.assertEqual(self.post()['status'], 200, 'routes edit POST 2') self.assertIn( - 'success', - self.conf_delete('routes/0'), - 'routes edit configure 3', + 'success', self.conf_delete('routes/0'), 'routes edit configure 3', ) self.assertEqual(self.get()['status'], 404, 'routes edit GET 3') @@ -682,9 +596,7 @@ class TestRouting(TestApplicationProto): self.assertEqual(self.post()['status'], 200, 'routes edit POST 4') self.assertIn( - 'success', - self.conf_delete('routes/0'), - 'routes edit configure 5', + 'success', self.conf_delete('routes/0'), 'routes edit configure 5', ) self.assertEqual(self.get()['status'], 404, 'routes edit GET 5') @@ -693,10 +605,7 @@ class TestRouting(TestApplicationProto): self.assertIn( 'success', self.conf_post( - { - "match": {"method": "POST"}, - "action": {"pass": "applications/empty"}, - }, + {"match": {"method": "POST"}, "action": {"return": 200},}, 'routes', ), 'routes edit configure 6', @@ -710,19 +619,8 @@ class TestRouting(TestApplicationProto): self.conf( { "listeners": {"*:7080": {"pass": "routes/main"}}, - "routes": { - "main": [{"action": {"pass": "applications/empty"}}] - }, - "applications": { - "empty": { - "type": "python", - "processes": {"spare": 0}, - "path": self.current_dir + '/python/empty', - "working_directory": self.current_dir - + '/python/empty', - "module": "wsgi", - } - }, + "routes": {"main": [{"action": {"return": 200}}]}, + "applications": {}, } ), 'route edit configure 7', @@ -1838,20 +1736,11 @@ class TestRouting(TestApplicationProto): "second": [ { "match": {"destination": ["127.0.0.1:7081"]}, - "action": {"pass": "applications/empty"}, + "action": {"return": 200}, } ], }, - "applications": { - "empty": { - "type": "python", - "processes": {"spare": 0}, - "path": self.current_dir + "/python/empty", - "working_directory": self.current_dir - + "/python/empty", - "module": "wsgi", - } - }, + "applications": {}, } ), 'proxy configure', diff --git a/test/test_routing_tls.py b/test/test_routing_tls.py index c6648095..36bd9057 100644 --- a/test/test_routing_tls.py +++ b/test/test_routing_tls.py @@ -2,9 +2,9 @@ from unit.applications.tls import TestApplicationTLS class TestRoutingTLS(TestApplicationTLS): - prerequisites = {'modules': ['python', 'openssl']} + prerequisites = {'modules': ['openssl']} - def test_routes_match_scheme(self): + def test_routes_match_scheme_tls(self): self.certificate() self.assertIn( @@ -21,35 +21,21 @@ class TestRoutingTLS(TestApplicationTLS): "routes": [ { "match": {"scheme": "http"}, - "action": {"pass": "applications/empty"}, + "action": {"return": 200}, }, { "match": {"scheme": "https"}, - "action": {"pass": "applications/204_no_content"}, + "action": {"return": 201}, }, ], - "applications": { - "empty": { - "type": "python", - "processes": {"spare": 0}, - "path": self.current_dir + "/python/empty", - "module": "wsgi", - }, - "204_no_content": { - "type": "python", - "processes": {"spare": 0}, - "path": self.current_dir - + "/python/204_no_content", - "module": "wsgi", - }, - }, + "applications": {}, } ), 'scheme configure', ) self.assertEqual(self.get()['status'], 200, 'http') - self.assertEqual(self.get_ssl(port=7081)['status'], 204, 'https') + self.assertEqual(self.get_ssl(port=7081)['status'], 201, 'https') if __name__ == '__main__': diff --git a/test/test_share_fallback.py b/test/test_share_fallback.py index 1a5d4e4b..c51e43ee 100644 --- a/test/test_share_fallback.py +++ b/test/test_share_fallback.py @@ -1,10 +1,10 @@ import os import unittest -from unit.applications.lang.python import TestApplicationPython +from unit.applications.proto import TestApplicationProto -class TestStatic(TestApplicationPython): - prerequisites = {'modules': ['python']} +class TestStatic(TestApplicationProto): + prerequisites = {} def setUp(self): super().setUp() @@ -20,19 +20,10 @@ class TestStatic(TestApplicationPython): { "listeners": { "*:7080": {"pass": "routes"}, - "*:7081": {"pass": "applications/empty"}, + "*:7081": {"pass": "routes"}, }, "routes": [{"action": {"share": self.testdir + "/assets"}}], - "applications": { - "empty": { - "type": "python", - "processes": {"spare": 0}, - "path": self.current_dir + "/python/empty", - "working_directory": self.current_dir - + "/python/empty", - "module": "wsgi", - } - }, + "applications": {}, } ) @@ -41,37 +32,22 @@ class TestStatic(TestApplicationPython): super().tearDown() + def action_update(self, conf): + self.assertIn('success', self.conf(conf, 'routes/0/action')) + def test_fallback(self): - self.assertIn( - 'success', - self.conf({"share": "/blah"}, 'routes/0/action'), - 'configure bad path no fallback', - ) + self.action_update({"share": "/blah"}) self.assertEqual(self.get()['status'], 404, 'bad path no fallback') - self.assertIn( - 'success', - self.conf( - {"share": "/blah", "fallback": {"pass": "applications/empty"}}, - 'routes/0/action', - ), - 'configure bad path fallback', - ) + self.action_update({"share": "/blah", "fallback": {"return": 200}}) + resp = self.get() self.assertEqual(resp['status'], 200, 'bad path fallback status') self.assertEqual(resp['body'], '', 'bad path fallback') def test_fallback_valid_path(self): - self.assertIn( - 'success', - self.conf( - { - "share": self.testdir + "/assets", - "fallback": {"pass": "applications/empty"}, - }, - 'routes/0/action', - ), - 'configure fallback', + self.action_update( + {"share": self.testdir + "/assets", "fallback": {"return": 200}} ) resp = self.get() self.assertEqual(resp['status'], 200, 'fallback status') @@ -90,36 +66,28 @@ class TestStatic(TestApplicationPython): ) def test_fallback_nested(self): - self.assertIn( - 'success', - self.conf( - { - "share": "/blah", - "fallback": { - "share": "/blah/blah", - "fallback": {"pass": "applications/empty"}, - }, + self.action_update( + { + "share": "/blah", + "fallback": { + "share": "/blah/blah", + "fallback": {"return": 200}, }, - 'routes/0/action', - ), - 'configure fallback nested', + } ) + resp = self.get() self.assertEqual(resp['status'], 200, 'fallback nested status') self.assertEqual(resp['body'], '', 'fallback nested') def test_fallback_share(self): - self.assertIn( - 'success', - self.conf( - { - "share": "/blah", - "fallback": {"share": self.testdir + "/assets"}, - }, - 'routes/0/action', - ), - 'configure fallback share', + self.action_update( + { + "share": "/blah", + "fallback": {"share": self.testdir + "/assets"}, + } ) + resp = self.get() self.assertEqual(resp['status'], 200, 'fallback share status') self.assertEqual(resp['body'], '0123456789', 'fallback share') @@ -136,76 +104,51 @@ class TestStatic(TestApplicationPython): self.assertIn( 'success', self.conf( - { - "share": "/blah", - "fallback": {"proxy": "http://127.0.0.1:7081"}, - }, - 'routes/0/action', + [ + { + "match": {"destination": "*:7081"}, + "action": {"return": 200}, + }, + { + "action": { + "share": "/blah", + "fallback": {"proxy": "http://127.0.0.1:7081"}, + } + }, + ], + 'routes', ), - 'configure fallback proxy', + 'configure fallback proxy route', ) + resp = self.get() self.assertEqual(resp['status'], 200, 'fallback proxy status') self.assertEqual(resp['body'], '', 'fallback proxy') @unittest.skip('not yet') def test_fallback_proxy_cycle(self): - self.assertIn( - 'success', - self.conf( - { - "share": "/blah", - "fallback": {"proxy": "http://127.0.0.1:7080"}, - }, - 'routes/0/action', - ), - 'configure fallback cycle', + self.action_update( + { + "share": "/blah", + "fallback": {"proxy": "http://127.0.0.1:7080"}, + } ) self.assertNotEqual(self.get()['status'], 200, 'fallback cycle') - self.assertIn( - 'success', self.conf_delete('listeners/*:7081'), 'delete listener' - ) + self.assertIn('success', self.conf_delete('listeners/*:7081')) self.assertNotEqual(self.get()['status'], 200, 'fallback cycle 2') def test_fallback_invalid(self): - self.assertIn( - 'error', - self.conf({"share": "/blah", "fallback": {}}, 'routes/0/action'), - 'configure fallback empty', - ) - self.assertIn( - 'error', - self.conf({"share": "/blah", "fallback": ""}, 'routes/0/action'), - 'configure fallback not object', - ) - self.assertIn( - 'error', - self.conf( - { - "proxy": "http://127.0.0.1:7081", - "fallback": {"share": "/blah"}, - }, - 'routes/0/action', - ), - 'configure fallback proxy invalid', - ) - self.assertIn( - 'error', - self.conf( - { - "pass": "applications/empty", - "fallback": {"share": "/blah"}, - }, - 'routes/0/action', - ), - 'configure fallback pass invalid', - ) - self.assertIn( - 'error', - self.conf({"fallback": {"share": "/blah"}}, 'routes/0/action'), - 'configure fallback only', + def check_error(conf): + self.assertIn('error', self.conf(conf, 'routes/0/action')) + + check_error({"share": "/blah", "fallback": {}}) + check_error({"share": "/blah", "fallback": ""}) + check_error({"return": 200, "fallback": {"share": "/blah"}}) + check_error( + {"proxy": "http://127.0.0.1:7081", "fallback": {"share": "/blah"}} ) + check_error({"fallback": {"share": "/blah"}}) if __name__ == '__main__': |