Tests: added tests for "return" action.

This commit is contained in:
Andrei Zeliankou
2020-03-27 15:50:09 +00:00
parent 5f2d07019c
commit 6e5b5d2a0b
4 changed files with 191 additions and 270 deletions

103
test/test_return.py Normal file
View File

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

View File

@@ -16,27 +16,10 @@ class TestRouting(TestApplicationProto):
"routes": [ "routes": [
{ {
"match": {"method": "GET"}, "match": {"method": "GET"},
"action": {"pass": "applications/empty"}, "action": {"return": 200},
} }
], ],
"applications": { "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",
},
},
} }
), ),
'routing configure', 'routing configure',
@@ -48,18 +31,14 @@ class TestRouting(TestApplicationProto):
def route_match(self, match): def route_match(self, match):
self.assertIn( self.assertIn(
'success', 'success',
self.route( self.route({"match": match, "action": {"return": 200}}),
{"match": match, "action": {"pass": "applications/empty"}}
),
'route match configure', 'route match configure',
) )
def route_match_invalid(self, match): def route_match_invalid(self, match):
self.assertIn( self.assertIn(
'error', 'error',
self.route( self.route({"match": match, "action": {"return": 200}}),
{"match": match, "action": {"pass": "applications/empty"}}
),
'route match configure invalid', 'route match configure invalid',
) )
@@ -233,24 +212,7 @@ class TestRouting(TestApplicationProto):
{ {
"listeners": {"*:7080": {"pass": "routes/main"}}, "listeners": {"*:7080": {"pass": "routes/main"}},
"routes": {"main": []}, "routes": {"main": []},
"applications": { "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",
},
},
} }
), ),
'route empty configure', 'route empty configure',
@@ -272,7 +234,7 @@ class TestRouting(TestApplicationProto):
def test_routes_route_match_absent(self): def test_routes_route_match_absent(self):
self.assertIn( self.assertIn(
'success', 'success',
self.conf([{"action": {"pass": "applications/empty"}}], 'routes'), self.conf([{"action": {"return": 200}}], 'routes'),
'route match absent configure', 'route match absent configure',
) )
@@ -349,14 +311,8 @@ class TestRouting(TestApplicationProto):
'success', 'success',
self.conf( self.conf(
[ [
{ {"match": {"method": "GET"}, "action": {"return": 200}},
"match": {"method": "GET"}, {"match": {"method": "POST"}, "action": {"return": 201}},
"action": {"pass": "applications/empty"},
},
{
"match": {"method": "POST"},
"action": {"pass": "applications/mirror"},
},
], ],
'routes', 'routes',
), ),
@@ -364,18 +320,7 @@ class TestRouting(TestApplicationProto):
) )
self.assertEqual(self.get()['status'], 200, 'rules two match first') self.assertEqual(self.get()['status'], 200, 'rules two match first')
self.assertEqual( self.assertEqual(self.post()['status'], 201, 'rules two match second')
self.post(
headers={
'Host': 'localhost',
'Content-Type': 'text/html',
'Connection': 'close',
},
body='X',
)['status'],
200,
'rules two match second',
)
def test_routes_two(self): def test_routes_two(self):
self.assertIn( self.assertIn(
@@ -393,20 +338,11 @@ class TestRouting(TestApplicationProto):
"second": [ "second": [
{ {
"match": {"host": "localhost"}, "match": {"host": "localhost"},
"action": {"pass": "applications/empty"}, "action": {"return": 200},
} }
], ],
}, },
"applications": { "applications": {},
"empty": {
"type": "python",
"processes": {"spare": 0},
"path": self.current_dir + '/python/empty',
"working_directory": self.current_dir
+ '/python/empty',
"module": "wsgi",
}
},
} }
), ),
'routes two configure', 'routes two configure',
@@ -556,7 +492,7 @@ class TestRouting(TestApplicationProto):
self.assertIn( self.assertIn(
'success', 'success',
self.conf([{"action": {"pass": "applications/empty"}}], 'routes'), self.conf([{"action": {"return": 200}}], 'routes'),
'redefine 2', 'redefine 2',
) )
self.assertEqual(self.get()['status'], 200, 'redefine request 2') self.assertEqual(self.get()['status'], 200, 'redefine request 2')
@@ -569,19 +505,8 @@ class TestRouting(TestApplicationProto):
self.conf( self.conf(
{ {
"listeners": {"*:7080": {"pass": "routes/main"}}, "listeners": {"*:7080": {"pass": "routes/main"}},
"routes": { "routes": {"main": [{"action": {"return": 200}}]},
"main": [{"action": {"pass": "applications/empty"}}] "applications": {},
},
"applications": {
"empty": {
"type": "python",
"processes": {"spare": 0},
"path": self.current_dir + '/python/empty',
"working_directory": self.current_dir
+ '/python/empty',
"module": "wsgi",
}
},
} }
), ),
'redefine 4', 'redefine 4',
@@ -595,25 +520,19 @@ class TestRouting(TestApplicationProto):
self.assertIn( self.assertIn(
'success', 'success',
self.conf_post( self.conf_post({"action": {"return": 200}}, 'routes/main'),
{"action": {"pass": "applications/empty"}}, 'routes/main'
),
'redefine 6', 'redefine 6',
) )
self.assertEqual(self.get()['status'], 200, 'redefine request 6') self.assertEqual(self.get()['status'], 200, 'redefine request 6')
self.assertIn( self.assertIn(
'error', 'error',
self.conf( self.conf({"action": {"return": 200}}, 'routes/main/2'),
{"action": {"pass": "applications/empty"}}, 'routes/main/2'
),
'redefine 7', 'redefine 7',
) )
self.assertIn( self.assertIn(
'success', 'success',
self.conf( self.conf({"action": {"return": 201}}, 'routes/main/1'),
{"action": {"pass": "applications/empty"}}, 'routes/main/1'
),
'redefine 8', 'redefine 8',
) )
@@ -631,10 +550,7 @@ class TestRouting(TestApplicationProto):
self.assertIn( self.assertIn(
'success', 'success',
self.conf_post( self.conf_post(
{ {"match": {"method": "POST"}, "action": {"return": 200}},
"match": {"method": "POST"},
"action": {"pass": "applications/empty"},
},
'routes', 'routes',
), ),
'routes edit configure 2', 'routes edit configure 2',
@@ -654,9 +570,7 @@ class TestRouting(TestApplicationProto):
self.assertEqual(self.post()['status'], 200, 'routes edit POST 2') self.assertEqual(self.post()['status'], 200, 'routes edit POST 2')
self.assertIn( self.assertIn(
'success', 'success', self.conf_delete('routes/0'), 'routes edit configure 3',
self.conf_delete('routes/0'),
'routes edit configure 3',
) )
self.assertEqual(self.get()['status'], 404, 'routes edit GET 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.assertEqual(self.post()['status'], 200, 'routes edit POST 4')
self.assertIn( self.assertIn(
'success', 'success', self.conf_delete('routes/0'), 'routes edit configure 5',
self.conf_delete('routes/0'),
'routes edit configure 5',
) )
self.assertEqual(self.get()['status'], 404, 'routes edit GET 5') self.assertEqual(self.get()['status'], 404, 'routes edit GET 5')
@@ -693,10 +605,7 @@ class TestRouting(TestApplicationProto):
self.assertIn( self.assertIn(
'success', 'success',
self.conf_post( self.conf_post(
{ {"match": {"method": "POST"}, "action": {"return": 200},},
"match": {"method": "POST"},
"action": {"pass": "applications/empty"},
},
'routes', 'routes',
), ),
'routes edit configure 6', 'routes edit configure 6',
@@ -710,19 +619,8 @@ class TestRouting(TestApplicationProto):
self.conf( self.conf(
{ {
"listeners": {"*:7080": {"pass": "routes/main"}}, "listeners": {"*:7080": {"pass": "routes/main"}},
"routes": { "routes": {"main": [{"action": {"return": 200}}]},
"main": [{"action": {"pass": "applications/empty"}}] "applications": {},
},
"applications": {
"empty": {
"type": "python",
"processes": {"spare": 0},
"path": self.current_dir + '/python/empty',
"working_directory": self.current_dir
+ '/python/empty',
"module": "wsgi",
}
},
} }
), ),
'route edit configure 7', 'route edit configure 7',
@@ -1838,20 +1736,11 @@ class TestRouting(TestApplicationProto):
"second": [ "second": [
{ {
"match": {"destination": ["127.0.0.1:7081"]}, "match": {"destination": ["127.0.0.1:7081"]},
"action": {"pass": "applications/empty"}, "action": {"return": 200},
} }
], ],
}, },
"applications": { "applications": {},
"empty": {
"type": "python",
"processes": {"spare": 0},
"path": self.current_dir + "/python/empty",
"working_directory": self.current_dir
+ "/python/empty",
"module": "wsgi",
}
},
} }
), ),
'proxy configure', 'proxy configure',

View File

@@ -2,9 +2,9 @@ from unit.applications.tls import TestApplicationTLS
class TestRoutingTLS(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.certificate()
self.assertIn( self.assertIn(
@@ -21,35 +21,21 @@ class TestRoutingTLS(TestApplicationTLS):
"routes": [ "routes": [
{ {
"match": {"scheme": "http"}, "match": {"scheme": "http"},
"action": {"pass": "applications/empty"}, "action": {"return": 200},
}, },
{ {
"match": {"scheme": "https"}, "match": {"scheme": "https"},
"action": {"pass": "applications/204_no_content"}, "action": {"return": 201},
}, },
], ],
"applications": { "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",
},
},
} }
), ),
'scheme configure', 'scheme configure',
) )
self.assertEqual(self.get()['status'], 200, 'http') 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__': if __name__ == '__main__':

View File

@@ -1,10 +1,10 @@
import os import os
import unittest import unittest
from unit.applications.lang.python import TestApplicationPython from unit.applications.proto import TestApplicationProto
class TestStatic(TestApplicationPython): class TestStatic(TestApplicationProto):
prerequisites = {'modules': ['python']} prerequisites = {}
def setUp(self): def setUp(self):
super().setUp() super().setUp()
@@ -20,19 +20,10 @@ class TestStatic(TestApplicationPython):
{ {
"listeners": { "listeners": {
"*:7080": {"pass": "routes"}, "*:7080": {"pass": "routes"},
"*:7081": {"pass": "applications/empty"}, "*:7081": {"pass": "routes"},
}, },
"routes": [{"action": {"share": self.testdir + "/assets"}}], "routes": [{"action": {"share": self.testdir + "/assets"}}],
"applications": { "applications": {},
"empty": {
"type": "python",
"processes": {"spare": 0},
"path": self.current_dir + "/python/empty",
"working_directory": self.current_dir
+ "/python/empty",
"module": "wsgi",
}
},
} }
) )
@@ -41,37 +32,22 @@ class TestStatic(TestApplicationPython):
super().tearDown() super().tearDown()
def action_update(self, conf):
self.assertIn('success', self.conf(conf, 'routes/0/action'))
def test_fallback(self): def test_fallback(self):
self.assertIn( self.action_update({"share": "/blah"})
'success',
self.conf({"share": "/blah"}, 'routes/0/action'),
'configure bad path no fallback',
)
self.assertEqual(self.get()['status'], 404, 'bad path no fallback') self.assertEqual(self.get()['status'], 404, 'bad path no fallback')
self.assertIn( self.action_update({"share": "/blah", "fallback": {"return": 200}})
'success',
self.conf(
{"share": "/blah", "fallback": {"pass": "applications/empty"}},
'routes/0/action',
),
'configure bad path fallback',
)
resp = self.get() resp = self.get()
self.assertEqual(resp['status'], 200, 'bad path fallback status') self.assertEqual(resp['status'], 200, 'bad path fallback status')
self.assertEqual(resp['body'], '', 'bad path fallback') self.assertEqual(resp['body'], '', 'bad path fallback')
def test_fallback_valid_path(self): def test_fallback_valid_path(self):
self.assertIn( self.action_update(
'success', {"share": self.testdir + "/assets", "fallback": {"return": 200}}
self.conf(
{
"share": self.testdir + "/assets",
"fallback": {"pass": "applications/empty"},
},
'routes/0/action',
),
'configure fallback',
) )
resp = self.get() resp = self.get()
self.assertEqual(resp['status'], 200, 'fallback status') self.assertEqual(resp['status'], 200, 'fallback status')
@@ -90,36 +66,28 @@ class TestStatic(TestApplicationPython):
) )
def test_fallback_nested(self): def test_fallback_nested(self):
self.assertIn( self.action_update(
'success',
self.conf(
{ {
"share": "/blah", "share": "/blah",
"fallback": { "fallback": {
"share": "/blah/blah", "share": "/blah/blah",
"fallback": {"pass": "applications/empty"}, "fallback": {"return": 200},
}, },
}, }
'routes/0/action',
),
'configure fallback nested',
) )
resp = self.get() resp = self.get()
self.assertEqual(resp['status'], 200, 'fallback nested status') self.assertEqual(resp['status'], 200, 'fallback nested status')
self.assertEqual(resp['body'], '', 'fallback nested') self.assertEqual(resp['body'], '', 'fallback nested')
def test_fallback_share(self): def test_fallback_share(self):
self.assertIn( self.action_update(
'success',
self.conf(
{ {
"share": "/blah", "share": "/blah",
"fallback": {"share": self.testdir + "/assets"}, "fallback": {"share": self.testdir + "/assets"},
}, }
'routes/0/action',
),
'configure fallback share',
) )
resp = self.get() resp = self.get()
self.assertEqual(resp['status'], 200, 'fallback share status') self.assertEqual(resp['status'], 200, 'fallback share status')
self.assertEqual(resp['body'], '0123456789', 'fallback share') self.assertEqual(resp['body'], '0123456789', 'fallback share')
@@ -136,76 +104,51 @@ class TestStatic(TestApplicationPython):
self.assertIn( self.assertIn(
'success', 'success',
self.conf( self.conf(
[
{ {
"match": {"destination": "*:7081"},
"action": {"return": 200},
},
{
"action": {
"share": "/blah", "share": "/blah",
"fallback": {"proxy": "http://127.0.0.1:7081"}, "fallback": {"proxy": "http://127.0.0.1:7081"},
}
}, },
'routes/0/action', ],
'routes',
), ),
'configure fallback proxy', 'configure fallback proxy route',
) )
resp = self.get() resp = self.get()
self.assertEqual(resp['status'], 200, 'fallback proxy status') self.assertEqual(resp['status'], 200, 'fallback proxy status')
self.assertEqual(resp['body'], '', 'fallback proxy') self.assertEqual(resp['body'], '', 'fallback proxy')
@unittest.skip('not yet') @unittest.skip('not yet')
def test_fallback_proxy_cycle(self): def test_fallback_proxy_cycle(self):
self.assertIn( self.action_update(
'success',
self.conf(
{ {
"share": "/blah", "share": "/blah",
"fallback": {"proxy": "http://127.0.0.1:7080"}, "fallback": {"proxy": "http://127.0.0.1:7080"},
}, }
'routes/0/action',
),
'configure fallback cycle',
) )
self.assertNotEqual(self.get()['status'], 200, 'fallback cycle') self.assertNotEqual(self.get()['status'], 200, 'fallback cycle')
self.assertIn( self.assertIn('success', self.conf_delete('listeners/*:7081'))
'success', self.conf_delete('listeners/*:7081'), 'delete listener'
)
self.assertNotEqual(self.get()['status'], 200, 'fallback cycle 2') self.assertNotEqual(self.get()['status'], 200, 'fallback cycle 2')
def test_fallback_invalid(self): def test_fallback_invalid(self):
self.assertIn( def check_error(conf):
'error', self.assertIn('error', self.conf(conf, 'routes/0/action'))
self.conf({"share": "/blah", "fallback": {}}, 'routes/0/action'),
'configure fallback empty', check_error({"share": "/blah", "fallback": {}})
) check_error({"share": "/blah", "fallback": ""})
self.assertIn( check_error({"return": 200, "fallback": {"share": "/blah"}})
'error', check_error(
self.conf({"share": "/blah", "fallback": ""}, 'routes/0/action'), {"proxy": "http://127.0.0.1:7081", "fallback": {"share": "/blah"}}
'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',
) )
check_error({"fallback": {"share": "/blah"}})
if __name__ == '__main__': if __name__ == '__main__':