Skip to content
Snippets Groups Projects
Commit 84e849ed authored by slobinger's avatar slobinger
Browse files

Merge branch '7-SAMSApp-urlrules-for-proxies' into 'master'

Resolve "SAMSApp fügt urlRules für SAMSProxies hinzu"

Closes #7

See merge request !3
parents 5113e889 8fe48f64
Branches
Tags
1 merge request!3Resolve "SAMSApp fügt urlRules für SAMSProxies hinzu"
......@@ -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:
......
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
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment