def test_no_match(self): """make sure a controller module that imports a class with the same as one of the query args doesen't get picked up as the controller class""" controller_prefix = "nomodcontroller" contents = { "{}.nomod".format(controller_prefix): [ "class Nomodbar(object): pass", "" ], controller_prefix: [ "from endpoints import Controller", "from .nomod import Nomodbar", "class Default(Controller):", " def GET(): pass", "" ] } m = testdata.create_modules(contents) path = '/nomodbar' # same name as one of the non controller classes r = Router([controller_prefix]) info = r.find(*self.get_http_instances(path)) self.assertEqual('Default', info['class_name']) self.assertEqual('nomodcontroller', info['module_name']) self.assertEqual('nomodbar', info['method_args'][0])
def test_routing(self): """there was a bug that caused errors raised after the yield to return another iteration of a body instead of raising them""" controller_prefix = "routing1" contents = [ "from endpoints import Controller", "class Default(Controller):", " def GET(self): pass", "", "class Foo(Controller):", " def GET(self): pass", "", "class Bar(Controller):", " def GET(self): pass", ] testdata.create_module(controller_prefix, contents=contents) r = Router([controller_prefix]) info = r.find(*self.get_http_instances()) self.assertEqual(info['module_name'], controller_prefix) self.assertEqual(info['class_name'], "Default") r = Router([controller_prefix]) info = r.find(*self.get_http_instances("/foo/che/baz")) self.assertEqual(2, len(info['method_args'])) self.assertEqual(info['class_name'], "Foo")
def test_get_controller_info_default(self): """I introduced a bug on 1-12-14 that caused default controllers to fail to be found, this makes sure that bug is squashed""" controller_prefix = "controller_info_default" r = testdata.create_modules({ controller_prefix: os.linesep.join([ "from endpoints import Controller", "class Default(Controller):", " def GET(): pass", "" ]) }) r = Router([controller_prefix]) info = r.find(*self.get_http_instances("/")) self.assertEqual('Default', info['class_name']) self.assertTrue(issubclass(info['class'], Controller))
def test_multiple_controller_prefixes_1(self): r = testdata.create_modules({ "foo": os.linesep.join([ "from endpoints import Controller", "class Default(Controller): pass", ]), "bar": os.linesep.join([ "from endpoints import Controller", "class User(Controller): pass", ]), }) r = Router(["foo", "bar"]) t = r.find(*self.get_http_instances("/user")) self.assertTrue("bar", t["module_name"]) t = r.find(*self.get_http_instances("/che")) self.assertTrue("foo", t["module_name"])
def test_callback_info(self): controller_prefix = "callback_info" req, res = self.get_http_instances("/foo/bar") req.query_kwargs = {'foo': 'bar', 'che': 'baz'} r = Router([controller_prefix]) with self.assertRaises(ImportError): d = r.find(req, res) contents = [ "from endpoints import Controller", "class Bar(Controller):", " def GET(*args, **kwargs): pass" ] testdata.create_module("{}.foo".format(controller_prefix), contents=contents) # if it succeeds, then it passed the test :) d = r.find(req, res)
def test_routing_module(self): controller_prefix = "callback_info" contents = [ "from endpoints import Controller", "class Bar(Controller):", " def GET(*args, **kwargs): pass" ] testdata.create_module("{}.foo".format(controller_prefix), contents=contents) r = Router(controller_prefix) self.assertTrue(controller_prefix in r.module_names) self.assertEqual(2, len(r.module_names))
def test_no_match(self): """make sure a controller module that imports a class with the same as one of the query args doesen't get picked up as the controller class""" controller_prefix = "nomodcontroller" contents = { "{}.nomod".format(controller_prefix): ["class Nomodbar(object): pass", ""], controller_prefix: [ "from endpoints import Controller", "from .nomod import Nomodbar", "class Default(Controller):", " def GET(): pass", "" ] } m = testdata.create_modules(contents) path = '/nomodbar' # same name as one of the non controller classes r = Router([controller_prefix]) info = r.find(*self.get_http_instances(path)) self.assertEqual('Default', info['class_name']) self.assertEqual('nomodcontroller', info['module_name']) self.assertEqual('nomodbar', info['method_args'][0])
def test_mixed_modules_packages(self): # make sure a package with modules and other packages will resolve correctly controller_prefix = "mmp" r = testdata.create_modules({ controller_prefix: os.linesep.join([ "from endpoints import Controller", "class Default(Controller): pass", ]), "{}.foo".format(controller_prefix): os.linesep.join([ "from endpoints import Controller", "class Default(Controller): pass", ]), "{}.foo.bar".format(controller_prefix): os.linesep.join([ "from endpoints import Controller", "class Default(Controller): pass", ]), "{}.che".format(controller_prefix): os.linesep.join([ "from endpoints import Controller", "class Default(Controller): pass", ]), }) r = Router(controller_prefix) self.assertEqual(set(['mmp.foo', 'mmp', 'mmp.foo.bar', 'mmp.che']), r.module_names) # make sure just a file will resolve correctly controller_prefix = "mmp2" testdata.create_module( controller_prefix, os.linesep.join([ "from endpoints import Controller", "class Bar(Controller): pass", ])) r = Router(controller_prefix) self.assertEqual(set(['mmp2']), r.module_names)
def test_routing_package(self): controller_prefix = "routepack" contents = [ "from endpoints import Controller", "", "class Default(Controller):", " def GET(self): pass", "", ] f = testdata.create_package(controller_prefix, contents=contents) r = Router(controller_prefix) self.assertTrue(controller_prefix in r.module_names) self.assertEqual(1, len(r.module_names))
def test_controllers(self): controller_prefix = "get_controllers" d = { controller_prefix: [ "from endpoints import Controller", "class Default(Controller):", " def GET(*args, **kwargs): pass", "" ], "{}.default".format(controller_prefix): [ "from endpoints import Controller", "class Default(Controller):", " def GET(*args, **kwargs): pass", "" ], "{}.foo".format(controller_prefix): [ "from endpoints import Controller", "class Default(Controller):", " def GET(*args, **kwargs): pass", "", "class Bar(Controller):", " def GET(*args, **kwargs): pass", " def POST(*args, **kwargs): pass", "" ], "{}.foo.baz".format(controller_prefix): [ "from endpoints import Controller", "class Default(Controller):", " def GET(*args, **kwargs): pass", "", "class Che(Controller):", " def GET(*args, **kwargs): pass", "" ], "{}.foo.boom".format(controller_prefix): [ "from endpoints import Controller", "", "class Bang(Controller):", " def GET(*args, **kwargs): pass", "" ], } r = testdata.create_modules(d) s = set(d.keys()) r = Router(controller_prefix) controllers = r.module_names self.assertEqual(s, controllers) # just making sure it always returns the same list controllers = r.module_names self.assertEqual(s, controllers)
def test_get_controller_info(self): controller_prefix = "controller_info_advanced" r = testdata.create_modules({ controller_prefix: [ "from endpoints import Controller", "class Default(Controller):", " def GET(*args, **kwargs): pass", "" ], "{}.default".format(controller_prefix): [ "from endpoints import Controller", "class Default(Controller):", " def GET(*args, **kwargs): pass", "" ], "{}.foo".format(controller_prefix): [ "from endpoints import Controller", "class Default(Controller):", " def GET(*args, **kwargs): pass", "", "class Bar(Controller):", " def GET(*args, **kwargs): pass", " def POST(*args, **kwargs): pass", "" ], "{}.foo.baz".format(controller_prefix): [ "from endpoints import Controller", "class Default(Controller):", " def GET(*args, **kwargs): pass", "", "class Che(Controller):", " def GET(*args, **kwargs): pass", "" ], "{}.foo.boom".format(controller_prefix): [ "from endpoints import Controller", "", "class Bang(Controller):", " def GET(*args, **kwargs): pass", "" ], }) ts = [ { 'in': dict(method="GET", path="/foo/bar/happy/sad"), 'out': { 'module_name': "controller_info_advanced.foo", 'class_name': 'Bar', 'method_args': ['happy', 'sad'], #'method_name': "GET", } }, { 'in': dict(method="GET", path="/"), 'out': { 'module_name': "controller_info_advanced", 'class_name': 'Default', 'method_args': [], #'method_name': "GET", } }, { 'in': dict(method="GET", path="/happy"), 'out': { 'module_name': "controller_info_advanced", 'class_name': 'Default', 'method_args': ["happy"], #'method_name': "GET", } }, { 'in': dict(method="GET", path="/foo/baz"), 'out': { 'module_name': "controller_info_advanced.foo.baz", 'class_name': 'Default', 'method_args': [], #'method_name': "GET", } }, { 'in': dict(method="GET", path="/foo/baz/che"), 'out': { 'module_name': "controller_info_advanced.foo.baz", 'class_name': 'Che', 'method_args': [], #'method_name': "GET", } }, { 'in': dict(method="GET", path="/foo/baz/happy"), 'out': { 'module_name': "controller_info_advanced.foo.baz", 'class_name': 'Default', 'method_args': ["happy"], #'method_name': "GET", } }, { 'in': dict(method="GET", path="/foo/happy"), 'out': { 'module_name': "controller_info_advanced.foo", 'class_name': 'Default', 'method_args': ["happy"], #'method_name': "GET", } }, ] for t in ts: req, res = self.get_http_instances(**t['in']) # r = Request() # for key, val in t['in'].items(): # setattr(r, key, val) r = Router([controller_prefix]) d = r.find(req, res) for key, val in t['out'].items(): self.assertEqual(val, d[key])
def create_reflect(self, controller_prefix): rou = Router(controller_prefix) return Reflect(rou)