Skip to content
Snippets Groups Projects
Commit aab76a26 authored by Sebastian Lobinger's avatar Sebastian Lobinger
Browse files

add new root url rule to tests in test_sams_proxy.py to fit needs #20

parent 8f30db18
Branches
Tags
1 merge request!15Resolve "Basis-Pfad (/) bei API über Proxy nicht aufrufbar"
......@@ -68,6 +68,9 @@ class TestSAMSHubWithThreadedAPI(unittest.TestCase):
time.sleep(0.01)
self.proxyApp = Flask('test')
self.proxyApp.config['TESTING'] = True
self.basic_url_rule_params = {'endpoint': 'proxy',
'methods': ['GET', 'POST', 'PUT', 'UPDATE', 'PATCH', 'DELETE', 'OPTIONS']}
self.url_rule_properties = ['rootUrlRule', 'urlRule']
def tearDown(self):
response = requests.get('http://localhost:4711/shutdown')
......@@ -78,9 +81,9 @@ class TestSAMSHubWithThreadedAPI(unittest.TestCase):
"""The proxy passes json data """
proxy = SAMSProxy(
proxySpec = {'in': '/proxy', 'out': 'http://localhost:4711'})
self.proxyApp.add_url_rule(rule = proxy.urlRule, endpoint = 'proxy',
view_func = proxy.proxy, methods=['GET', 'POST', 'PUT', 'UPDATE',
'PATCH', 'DELETE', 'OPTIONS'])
for urlRuleProp in self.url_rule_properties:
self.proxyApp.add_url_rule(rule = getattr(proxy, urlRuleProp),
view_func = proxy.proxy, **self.basic_url_rule_params)
thProxyApp = FlaskInThread(self.proxyApp, host="localhost", port=5000)
thProxyApp.start()
time.sleep(0.01)
......@@ -99,9 +102,9 @@ class TestSAMSHubWithThreadedAPI(unittest.TestCase):
'user': {'name': 'u','param-type': 'url'},
'group': {'name': 'g', 'param-type': 'body'},
'language': {'name': 'Lang', 'param-type': 'header'}}}})
self.proxyApp.add_url_rule(rule = proxy.urlRule, endpoint = 'proxy',
view_func = proxy.proxy, methods=['GET', 'POST', 'PUT', 'UPDATE',
'PATCH', 'DELETE', 'OPTIONS'])
for urlRuleProp in self.url_rule_properties:
self.proxyApp.add_url_rule(rule = getattr(proxy, urlRuleProp),
view_func = proxy.proxy, **self.basic_url_rule_params)
self.proxyApp.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
with self.proxyApp.test_client(self) as client:
with client.session_transaction() as sess:
......@@ -125,9 +128,9 @@ class TestSAMSHubWithThreadedAPI(unittest.TestCase):
'user': {'name': 'u','param-type': 'url'},
'group': {'name': 'g', 'param-type': 'body'},
'language': {'name': 'lang', 'param-type': 'header'}}}})
self.proxyApp.add_url_rule(rule = proxy.urlRule, endpoint = 'proxy',
view_func = proxy.proxy, methods=['GET', 'POST', 'PUT', 'UPDATE',
'PATCH', 'DELETE', 'OPTIONS'])
for urlRuleProp in self.url_rule_properties:
self.proxyApp.add_url_rule(rule = getattr(proxy, urlRuleProp),
view_func = proxy.proxy, **self.basic_url_rule_params)
self.proxyApp.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
with self.proxyApp.test_client(self) as client:
with client.session_transaction() as sess:
......@@ -152,9 +155,9 @@ class TestSAMSHubWithThreadedAPI(unittest.TestCase):
'user': {'name': 'u','param-type': 'url'},
'group': {'name': 'g', 'param-type': 'body'},
'language': {'name': 'lang', 'param-type': 'header'}}}})
self.proxyApp.add_url_rule(rule = proxy.urlRule, endpoint = 'proxy',
view_func = proxy.proxy, methods=['GET', 'POST', 'PUT', 'UPDATE',
'PATCH', 'DELETE', 'OPTIONS'])
for urlRuleProp in self.url_rule_properties:
self.proxyApp.add_url_rule(rule = getattr(proxy, urlRuleProp),
view_func = proxy.proxy, **self.basic_url_rule_params)
self.proxyApp.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
with self.proxyApp.test_client(self) as client:
with client.session_transaction() as sess:
......@@ -171,23 +174,25 @@ class TestSAMSHubWithThreadedAPI(unittest.TestCase):
""" The proxy forwards a request to test flaskapp and returns 'hello' """
proxy = SAMSProxy(
proxySpec = {'in': '/proxy', 'out': 'http://localhost:4711'})
self.proxyApp.add_url_rule(rule = proxy.urlRule, endpoint = 'proxy',
view_func = proxy.proxy, methods=['GET', 'POST', 'PUT', 'UPDATE',
'PATCH', 'DELETE', 'OPTIONS'])
for urlRuleProp in self.url_rule_properties:
self.proxyApp.add_url_rule(rule = getattr(proxy, urlRuleProp),
view_func = proxy.proxy, **self.basic_url_rule_params)
with self.proxyApp.test_client(self) as client:
eprint(self.proxyApp.url_map)
for method in ['get', 'post', 'put', 'update', 'patch', 'delete',
'options']:
response = client.open(path='/proxy/hello', method=method.upper())
self.assertIs(response.status_code, 200, method)
self.assertEqual(response.data.decode('utf-8'), method + ' hello')
for path in ('/proxy/hello', '/proxy/'):
response = client.open(path = path, method = method.upper())
with self.subTest('method ' + method + ' path ' + path):
self.assertIs(response.status_code, 200, method)
def test_param_passthrough(self):
"""The proxy passes headers, form / data and url params """
proxy = SAMSProxy(
proxySpec = {'in': '/proxy', 'out': 'http://localhost:4711'})
self.proxyApp.add_url_rule(rule = proxy.urlRule, endpoint = 'proxy',
view_func = proxy.proxy, methods=['GET', 'POST', 'PUT', 'UPDATE',
'PATCH', 'DELETE', 'OPTIONS'])
for urlRuleProp in self.url_rule_properties:
self.proxyApp.add_url_rule(rule = getattr(proxy, urlRuleProp),
view_func = proxy.proxy, **self.basic_url_rule_params)
with self.proxyApp.test_client(self) as client:
res = client.get(path='/proxy/passthrough',
query_string={'u':'foo'}, data={'g':'bar'}, headers={'lang': 'klingon'})
......@@ -214,6 +219,9 @@ class TestSAMSHubWithThreadedAPI(unittest.TestCase):
proxyApp.add_url_rule(rule = proxy.urlRule, endpoint = 'proxy',
view_func = proxy.proxy, methods=['GET', 'POST', 'PUT', 'UPDATE',
'PATCH', 'DELETE', 'OPTIONS'])
proxyApp.add_url_rule(rule = proxy.rootUrlRule, endpoint = 'proxy',
view_func = proxy.proxy, methods=['GET', 'POST', 'PUT', 'UPDATE',
'PATCH', 'DELETE', 'OPTIONS'])
with proxyApp.test_client(self) as client:
res = client.get(path='/proxy/passthrough',
query_string={'u':'foo'}, data={'g':'bar'},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment