diff --git a/sams_classes/sams_app.py b/sams_classes/sams_app.py index 57946e87eab5338a69261d1d91773ee9a09d55a4..07bf687a3ea900c4ee57a63075417ff30f54dd3e 100644 --- a/sams_classes/sams_app.py +++ b/sams_classes/sams_app.py @@ -25,6 +25,7 @@ class SAMSApp: , dict): raise DefaultLanguageDictMissing() self.__add_urls() + self.__add_proxy_urls() @property def module(self): @@ -48,6 +49,15 @@ class SAMSApp: rule = self._generate_url(view.get('url')), endpoint = endpoint, view_func = view_func) + def __add_proxy_urls(self): + i = 0 + for proxy in self.proxies: + self.__blueprint.add_url_rule( + rule = self._generate_url(proxy.urlRule), + endpoint = self.__blueprint.name + '_proxy_' + str(i), + view_func = proxy.proxy) + i += 1 + @staticmethod def _get_attr(thing, attrString): try: diff --git a/test/test_sams_app.py b/test/test_sams_app.py index f414cb5704386f001465ea2c188ffdc39a42bac4..4eb28cbe4216eb461b5aba27026d9586a1fe2332 100644 --- a/test/test_sams_app.py +++ b/test/test_sams_app.py @@ -1,4 +1,4 @@ -import unittest +import unittest, time from sams_classes import SAMSApp, SAMSProxy import sys from types import ModuleType @@ -7,6 +7,8 @@ from sams_classes.exceptions import ( , DefaultLanguageDictMissing ) from flask import Blueprint, Flask +from .helper_classes import FlaskInThread +import requests def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) @@ -65,7 +67,7 @@ class TestSAMSApp(unittest.TestCase): , langDict = {'en':{}}) flaskApp = Flask(__name__) flaskApp.register_blueprint(app.blueprint) - self.assertGreater(len(list(flaskApp.url_map.iter_rules())), 0) + self.assertGreater(len(list(flaskApp.url_map.iter_rules())), 1) client = flaskApp.test_client(self) response = client.get('/test/do_nothing') self.assertIs(response.status_code, 200) @@ -161,6 +163,40 @@ class TestSAMSApp(unittest.TestCase): self.assertIs(len(app.proxies), len(app.manifest.get('proxies'))) for element in app.proxies: self.assertIsInstance(element, SAMSProxy) + +class TestSAMSAppWithThreadedApi(unittest.TestCase): + """Class to test SAMSApp with a threaded API App as backend + + This TestCase provides a setup and tearDown for a API App in a separate + thread which is reachable at http://localhost:4711. + """ + def setUp(self): + from .api_test_server import app as apiApp + thApi = FlaskInThread(apiApp, host="localhost", port=4711) + thApi.start() + time.sleep(0.01) + self.flaskApp = Flask('test') + self.flaskApp.config['TESTING'] = True + + def tearDown(self): + response = requests.get('http://localhost:4711/shutdown') + print(response.content.decode('utf-8')) + time.sleep(0.01) + + def test_app_proxy_registered(self): + """SAMSApp registers the needed url rue for proxy in its bluebprint""" + testApp = SAMSApp(name = 'test', langDict = {'de': {}}, + manifest = {'default_language': 'de', 'proxies': [ + {'in': 'api', 'out': 'http://localhost:4711'}] + } + ) + self.flaskApp.register_blueprint(testApp.blueprint) + self.assertGreater(len(list(self.flaskApp.url_map.iter_rules())), 1) + eprint(self.flaskApp.url_map) + with self.flaskApp.test_client(self) as client: + response = client.get('/api/hello') + self.assertIs(response.status_code, 200) + if __name__ == '__main__': unittest.main() \ No newline at end of file diff --git a/test/test_sams_proxy.py b/test/test_sams_proxy.py index a8086dfaf1231f825761a518c1584f2a54baf64b..1a4c8fe804c8baa50a7875091d9a82c2d9183c33 100644 --- a/test/test_sams_proxy.py +++ b/test/test_sams_proxy.py @@ -17,12 +17,12 @@ class TestSAMSHub(unittest.TestCase): #apiApp.config['TESTING'] = True thApi = FlaskInThread(apiApp, host="localhost", port=4711) thApi.start() - time.sleep(1) + time.sleep(0.01) def tearDown(self): response = requests.get('http://localhost:4711/shutdown') print(response.content.decode('utf-8')) - time.sleep(1) + time.sleep(0.01) def test_proxy_spec_mandatory (self): """ SAMSProxy needs a proxy_spec dict, raies error if not supplied""" @@ -187,11 +187,10 @@ class TestSAMSHub(unittest.TestCase): thProxyApp = FlaskInThread(proxyApp, host="localhost", port=5000) thProxyApp.start() time.sleep(1) - with proxyApp.test_client(self) as client: - res = requests.post(url='http://localhost:5000/proxy/passthrough', - json={'g':'bar'}) - resDict = json.loads(res.content.decode('utf-8')) - eprint('resDict: ' + str(resDict) + '\n') - self.assertEqual(resDict['body']['g'], 'bar') + res = requests.post(url='http://localhost:5000/proxy/passthrough', + json={'g':'bar'}) + resDict = json.loads(res.content.decode('utf-8')) + eprint('resDict: ' + str(resDict) + '\n') + self.assertEqual(resDict['body']['g'], 'bar') requests.get('http://localhost:5000/shutdown') time.sleep(1) \ No newline at end of file