def get_server(self, classpath): """Returns the interface Server class :param modpath: the module path of the interface (eg, endpoints.interface.wsgi) :returns: Server class """ parts = classpath.rsplit(".", 1) modpath = parts[0] classname = parts[1] if len(parts) > 1 else "Server" try: rm = ReflectModule(modpath) s = getattr(rm.module, classname) except (ImportError, AttributeError): rm = ReflectModule("endpoints.interface.{}".format(modpath)) s = getattr(rm.module, classname) return s
def test_routing_module(self): controller_prefix = "routing_module" contents = [ "from endpoints import Controller", "class Bar(Controller):", " def GET(*args, **kwargs): pass" ] testdata.create_module("{}.foo".format(controller_prefix), contents=contents) r = ReflectModule(controller_prefix) self.assertTrue(controller_prefix in r.module_names) self.assertEqual(2, len(r.module_names))
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( { "": [ "from endpoints import Controller", "class Default(Controller): pass", ], "foo": [ "from endpoints import Controller", "class Default(Controller): pass", ], "foo.bar": [ "from endpoints import Controller", "class Default(Controller): pass", ], "che": [ "from endpoints import Controller", "class Default(Controller): pass", ], }, prefix=controller_prefix) r = ReflectModule(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 = ReflectModule(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 = ReflectModule(controller_prefix) self.assertTrue(controller_prefix in r.module_names) self.assertEqual(1, len(r.module_names))
def test_multi_methods(self): controller_prefix = "multi_methods" mp = testdata.create_module(controller_prefix, [ "import endpoints", "from endpoints.decorators import param, version", "class Bar(endpoints.Controller):", " @param('foo', default=1, type=int)", " @param('bar', type=bool, required=False)", " @version('v1')", " def GET_version1(self): pass", "", " @version('v2')", " def GET_version2(self): pass", "" ]) rmod = ReflectModule(mp) rc = ReflectController(rmod, mp.module.Bar, controller_prefix) self.assertEqual(2, len(rc.methods["GET"])) for rm in rc.methods["GET"]: self.assertTrue(rm.version in ["v1", "v2"])
def test__get_methods_info(self): mp = testdata.create_module(contents=[ "from endpoints import param, version, Controller", "class Bar(Controller):", " @version('v1')", " def GET_v1(self, **kwargs): pass", "", " @version('v2')", " def GET_v2(self, *args): pass", "" " def POST(self, *args, **kwargs): pass", ]) rmod = ReflectModule(mp) rc = ReflectController(rmod, mp.module.Bar, mp) r = rc._get_methods_info() self.assertEqual(2, len(r["GET"])) self.assertEqual(1, len(r["POST"]))
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 = ReflectModule(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_metavar(self): # https://github.com/firstopinion/endpoints/issues/58 controller_prefix = "metavar_rct" mp = testdata.create_module(controller_prefix, [ "import endpoints", "from endpoints.decorators import param, version", "class Bar(endpoints.Controller):", " @param(0, metavar='bar')", " @param(1, metavar='che')", " def GET(self, bar, che): pass", "", ]) rmod = ReflectModule(mp) rc = ReflectController(rmod, mp.module.Bar, controller_prefix) for rm in rc.methods["GET"]: for name, pr in rm.params.items(): self.assertTrue("metavar" in pr["options"])
def test_get_info(self): mp = testdata.create_module(contents=[ "from endpoints import param, version, Controller", "class Foo(Controller):", " def POST(self, one, two=2, three=3): pass", ]) rmod = ReflectModule(mp) rc = ReflectController(rmod, mp.module.Foo, mp) r = rc.get_info() self.assertFalse(r["POST"]["POST"]["positionals"]) self.assertFalse(r["POST"]["POST"]["keywords"]) self.assertEqual(3, len(r["POST"]["POST"]["params"])) self.assertEqual(3, r["POST"]["POST"]["params"][2]["default"]) self.assertTrue(r["POST"]["POST"]["params"][0]["required"]) mp = testdata.create_module(contents=[ "from endpoints import param, version, Controller", "class Foo(Controller):", " @param(0, default=1)", " @param(1)", " @param('che')", " def POST(self, zero, one, che): pass", ]) rmod = ReflectModule(mp) rc = ReflectController(rmod, mp.module.Foo, mp) r = rc.get_info() self.assertEqual(3, len(r["POST"]["POST"]["params"])) self.assertEqual(3, len(r["POST"]["POST"]["decorators"])) mp = testdata.create_module(contents=[ "from endpoints import param, version, Controller", "class Foo(Controller):", " def GET(self, *args, **kwargs): pass", ]) rmod = ReflectModule(mp) rc = ReflectController(rmod, mp.module.Foo, mp) r = rc.get_info() self.assertTrue(r["GET"]["GET"]["positionals"]) self.assertTrue(r["GET"]["GET"]["keywords"]) mp = testdata.create_module(contents=[ "from endpoints import param, version, Controller", "class Foo(Controller):", " def GET(self): pass", ]) rmod = ReflectModule(mp) rc = ReflectController(rmod, mp.module.Foo, mp) r = rc.get_info() self.assertFalse(r["GET"]["GET"]["positionals"]) self.assertFalse(r["GET"]["GET"]["keywords"]) mp = testdata.create_module(contents=[ "from endpoints import param, version, Controller", "class Foo(Controller):", " def GET(self, **kwargs): pass", ]) rmod = ReflectModule(mp) rc = ReflectController(rmod, mp.module.Foo, mp) r = rc.get_info() self.assertFalse(r["GET"]["GET"]["positionals"]) self.assertTrue(r["GET"]["GET"]["keywords"]) mp = testdata.create_module(contents=[ "from endpoints import param, version, Controller", "class Foo(Controller):", " def GET(self, *args): pass", ]) rmod = ReflectModule(mp) rc = ReflectController(rmod, mp.module.Foo, mp) r = rc.get_info() self.assertTrue(r["GET"]["GET"]["positionals"]) self.assertFalse(r["GET"]["GET"]["keywords"])
def test_method_required_args(self): mp = testdata.create_module(contents=[ "from endpoints import param, version, Controller", "class Foo(Controller):", " @param(0)", " @param('one')", " def POST(self, *args, **kwargs): pass", ]) rmod = ReflectModule(mp) rm = ReflectHTTPMethod("POST", mp.module.Foo.POST, ReflectController(rmod, mp.module.Foo, mp)) r = rm.required_args self.assertEqual([0], r) mp = testdata.create_module(contents=[ "from endpoints import param, version, Controller", "class Foo(Controller):", " @param(0)", " @param('one')", " def POST(self, zero, one): pass", ]) rmod = ReflectModule(mp) rm = ReflectHTTPMethod("POST", mp.module.Foo.POST, ReflectController(rmod, mp.module.Foo, mp)) r = rm.required_args self.assertEqual(["zero"], r) mp = testdata.create_module(contents=[ "from endpoints import param, version, Controller", "class Foo(Controller):", " @param(0)", " @param(1, default=1)", " def POST(self, *args): pass", ]) rmod = ReflectModule(mp) rm = ReflectHTTPMethod("POST", mp.module.Foo.POST, ReflectController(rmod, mp.module.Foo, mp)) r = rm.required_args self.assertEqual([0], r) mp = testdata.create_module(contents=[ "from endpoints import param, version, Controller", "class Foo(Controller):", " @param(0)", " @param(1, default=1)", " def POST(self, zero, *args): pass", ]) rmod = ReflectModule(mp) rm = ReflectHTTPMethod("POST", mp.module.Foo.POST, ReflectController(rmod, mp.module.Foo, mp)) r = rm.required_args self.assertEqual(["zero"], r) mp = testdata.create_module(contents=[ "from endpoints import param, version, Controller", "class Foo(Controller):", " @param(0)", " @param(1, default=1)", " def POST(self, zero, one): pass", ]) rmod = ReflectModule(mp) rm = ReflectHTTPMethod("POST", mp.module.Foo.POST, ReflectController(rmod, mp.module.Foo, mp)) r = rm.required_args self.assertEqual(["zero"], r) mp = testdata.create_module(contents=[ "from endpoints import param, version, Controller", "class Foo(Controller):", " @param(0, default=0)", " @param(1, default=1)", " def POST(self, zero, one): pass", ]) rmod = ReflectModule(mp) rm = ReflectHTTPMethod("POST", mp.module.Foo.POST, ReflectController(rmod, mp.module.Foo, mp)) r = rm.required_args self.assertEqual([], r) mp = testdata.create_module(contents=[ "from endpoints import param, version, Controller", "class Foo(Controller):", " def POST(self, one, two=2, three=3): pass", ]) rmod = ReflectModule(mp) rm = ReflectHTTPMethod("POST", mp.module.Foo.POST, ReflectController(rmod, mp.module.Foo, mp)) r = rm.required_args self.assertEqual(1, len(r))