def test_options_request_with_acrh(self): "Test a OPTIONS request w/ supplied Access-Control-Request-Headers header." route_map = [(r"^/example/", dummy_api)] api_config = {"arg1": True, "arg2": 42} router = urldispatch.APIRouter(route_map, config=api_config, cross_origin_domains="*") request = DummyRequest(api_mode="", api_version="", api_name="") request.setHeader("Access-Control-Request-Headers", "Content-Type") resource = urldispatch.CORSInterrogation(request, api_router=router) body = resource.render_OPTIONS(request) self.assertEquals(request.response_code, 200) self.assertEquals(request.getHeader("Content-Type"), "application/json; charset=utf-8") self.assertEquals(request.getHeader("Access-Control-Allow-Origin"), "*") self.assertEquals( request.getHeader("Access-Control-Allow-Credentials"), "true") self.assertEquals(request.getHeader("Access-Control-Allow-Headers"), "Content-Type") self.assertEquals( request.getHeader("Access-Control-Allow-Methods"), "PUT,GET,DELETE,POST,HEAD,TRACE,CONNECT,PROPFIND,PROPPATCH,MKCOL,COPY,MOVE,LOCK,UNLOCK" ) self.assertEquals(body, "")
def test_get_options(self): "Test OPTIONS request." route_map = [(r"^/example/", dummy_api)] api_config = {"arg1": True, "arg2": 42} request = DummyRequest(api_mode="prod", api_version="1.0", api_name="dummy_api") request.method = "OPTIONS" request.setHeader("Access-Control-Request-Headers", "Content-Type") router = urldispatch.APIRouter(route_map, config=api_config, cross_origin_domains="*") resource = router.getChild("/example/", request) self.assertTrue(isinstance(resource, urldispatch.CORSInterrogation)) resource.render_OPTIONS(request) self.assertEquals(request.getHeader("Content-Type"), "application/json; charset=utf-8") self.assertEquals(request.getHeader("Access-Control-Allow-Origin"), "*") self.assertEquals( request.getHeader("Access-Control-Allow-Credentials"), "true") self.assertEquals(request.getHeader("Access-Control-Allow-Headers"), "Content-Type") self.assertEquals( request.getHeader("Access-Control-Allow-Methods"), "PUT,GET,DELETE,POST,HEAD,TRACE,CONNECT,PROPFIND,PROPPATCH,MKCOL,COPY,MOVE,LOCK,UNLOCK" )
def test_json_validate_success(self): "Request URL arguments successfully validated." test_request = DummyRequest() test_request.setHeader("Content-Type", "application/json; charset=utf-8") test_request.args["arg1"] = "test" res = self.dummy_render_func2(test_request) self.assertEqual(res, "okey dokey")
class UtilityFunctionsTestCase(unittest.TestCase): def setUp(self): self.request = DummyRequest(api_mode="", api_version="", api_name="") def test_get_version_no_header_no_queryarg(self): "Version extraction - no version supplied" result = urldispatch.get_version(self.request) self.assertEquals(result["api"], "") self.assertEquals(result["version"], "") self.assertEquals(result["mode"], "") def test_get_version_in_query_arg(self): "Version extraction - version in query string" self.request.args["X-DigiTar-API-Version"] = ["dummy_api-1.0+prod"] result = urldispatch.get_version(self.request) self.assertEquals(result["api"], "dummy_api") self.assertEquals(result["version"], "1.0") self.assertEquals(result["mode"], "prod") def test_get_version_in_header(self): "Version extraction - version in header" self.request.setHeader("X-DigiTar-API-Version", "dummy_api-1.0+prod") result = urldispatch.get_version(self.request) self.assertEquals(result["api"], "dummy_api") self.assertEquals(result["version"], "1.0") self.assertEquals(result["mode"], "prod") def test_get_version_bad_version(self): "Version extraction - malformed version" self.request.setHeader("X-DigiTar-API-Version", "badmojo") self.assertRaises(IndexError, urldispatch.get_version, self.request) def tearDown(self): del (self.request)
def test_content_type_no_charset(self): "Request Content-Type does not have a character set specified" test_request = DummyRequest() test_request.setHeader("Content-Type", "application/json") outer_wrap = webapi.json_arguments([("arg1", unicode)]) inner_wrap = outer_wrap(self.dummy_render_func) self.assertEqual(str(webapi.CharsetNotUTF8Error(test_request)), inner_wrap(self, test_request))
def test_get_child_version_unknown(self): "Test version extraction with bad header" request = DummyRequest(api_mode="", api_version="", api_name="") request.setHeader("X-DigiTar-API-Version", "dummyapi") route_map = [(r"^/example/", dummy_api)] router = urldispatch.APIRouter(route_map) resource = router.getChild("/example/", request) self.assertTrue(isinstance(resource, urldispatch.UnknownVersion))
def test_get_child_route_no_api_match(self): "Test route match with no API name match in version header" request = DummyRequest(api_mode="", api_version="", api_name="") request.setHeader("X-DigiTar-API-Version", "badmojoapi-1.0+prod") route_map = [(r"^/example/", dummy_api)] router = urldispatch.APIRouter(route_map) resource = router.getChild("/example/", request) self.assertTrue(isinstance(resource, urldispatch.UnknownVersion))
def test_content_type_charset_not_utf8(self): "Request Content-Type characters set is not UTF-8" test_request = DummyRequest() test_request.setHeader("Content-Type", "application/json; charset=iso-1022-jp") outer_wrap = webapi.json_arguments([("arg1", unicode)]) inner_wrap = outer_wrap(self.dummy_render_func) self.assertEqual(str(webapi.CharsetNotUTF8Error(test_request)), inner_wrap(self, test_request))
def test_get_child_no_route_match(self): "Test no route match" route_map = [(r"^/example/", dummy_api)] api_config = {"arg1" : True, "arg2" : 42} request = DummyRequest(api_mode="prod", api_version="1.0", api_name="dummy_api") request.setHeader("X-DigiTar-API-Version", "dummy_api-1.0+prod") router = urldispatch.APIRouter(route_map, config=api_config) resource = router.getChild("/badmojo/", request) self.assertTrue(isinstance(resource, urldispatch.UnknownAPI))
def test_content_type_content_type_mismatch(self): "Request Content-Type doesn't match expected type." test_request = DummyRequest() test_request.setHeader("Content-Type", "image/jpeg; charset=utf-8") outer_wrap = webapi.json_arguments([("arg1", unicode)], content_type="application/json") inner_wrap = outer_wrap(self.dummy_render_func) self.assertEqual(str(webapi.ContentTypeError(test_request)), inner_wrap(self, test_request))
def test_content_required_arg_missing_optional_flag_false(self): "Request JSON arguments missing required argument." test_request = DummyRequest() test_request.setHeader("Content-Type", "application/json; charset=utf-8") test_request.write(json.dumps({"arg2" : 1})) outer_wrap = webapi.json_arguments([("arg1", unicode, False)]) inner_wrap = outer_wrap(self.dummy_render_func) self.assertEqual(str(webapi.ValueError(test_request, "arg1", "Argument is missing.")), inner_wrap(self, test_request))
def test_json_validate_success(self): "Request JSON arguments successfully validated." test_request = DummyRequest() test_request.setHeader("Content-Type", "application/json; charset=utf-8") test_request.write(json.dumps({"arg1" : unicode("hi")})) self.dummy_render_func2(test_request) self.assertTrue(hasattr(test_request, "jsonArgs")) self.assertTrue(test_request.jsonArgs.has_key("arg1")) self.assertEqual(test_request.jsonArgs["arg1"], unicode("hi"))
def test_content_arg_type_mismatch_optional_arg(self): "Request JSON optional arguments contain data which doesn't match expected type." test_request = DummyRequest() test_request.setHeader("Content-Type", "application/json; charset=utf-8") test_request.write(json.dumps({"arg1" : 1})) outer_wrap = webapi.json_arguments([("arg1", unicode, True)]) inner_wrap = outer_wrap(self.dummy_render_func) self.assertEqual(str(webapi.ValueError(test_request, "arg1", "Must be of type unicode")), inner_wrap(self, test_request))
def test_content_not_dict(self): "Request content contained in a dictionary/hash." test_request = DummyRequest() test_request.setHeader("Content-Type", "application/json; charset=utf-8") test_request.write(json.dumps("not a dict")) outer_wrap = webapi.json_arguments([("arg1", unicode)]) inner_wrap = outer_wrap(self.dummy_render_func) self.assertEqual(str(webapi.RequestNotHashError(test_request)), inner_wrap(self, test_request))
def test_content_invalid_json(self): "Request content not valid JSON." test_request = DummyRequest() test_request.setHeader("Content-Type", "application/json; charset=utf-8") test_request.write("boingo! bad json!") outer_wrap = webapi.json_arguments([("arg1", unicode)]) inner_wrap = outer_wrap(self.dummy_render_func) self.assertEqual(str(webapi.JSONDecodeError(test_request)), inner_wrap(self, test_request))
def test_json_validate_success(self): "Request JSON arguments successfully validated." test_request = DummyRequest() test_request.setHeader("Content-Type", "application/json; charset=utf-8") test_request.write(json.dumps({"arg1": unicode("hi")})) self.dummy_render_func2(test_request) self.assertTrue(hasattr(test_request, "jsonArgs")) self.assertTrue(test_request.jsonArgs.has_key("arg1")) self.assertEqual(test_request.jsonArgs["arg1"], unicode("hi"))
def test_get_child_route_match_api_match(self): "Test route match with good API name match in version header" route_map = [(r"^/example/", dummy_api)] api_config = {"arg1" : True, "arg2" : 42} request = DummyRequest(api_mode="prod", api_version="1.0", api_name="dummy_api") request.setHeader("X-DigiTar-API-Version", "dummy_api-1.0+prod") router = urldispatch.APIRouter(route_map, config=api_config) resource = router.getChild("/example/", request) self.assertTrue(isinstance(resource, urldispatch.VersionRouter)) self.assertEquals(resource.api_router, router)
def test_get_bad_header(self): "Test unknown version return error code & message via GET" request = DummyRequest(api_mode="", api_version="", api_name="") request.setHeader("X-DigiTar-API-Version", "dummyapi") resource = urldispatch.UnknownVersion() body = resource.render_GET(request) self.assertEquals(request.response_code, 406) self.assertEquals(request.getHeader("Content-Type"), "application/json; charset=utf-8") self.assertEquals( body, str(webapi.UnknownAPIVersionError(request, "dummyapi")))
def test_get_child_no_route_match(self): "Test no route match" route_map = [(r"^/example/", dummy_api)] api_config = {"arg1": True, "arg2": 42} request = DummyRequest(api_mode="prod", api_version="1.0", api_name="dummy_api") request.setHeader("X-DigiTar-API-Version", "dummy_api-1.0+prod") router = urldispatch.APIRouter(route_map, config=api_config) resource = router.getChild("/badmojo/", request) self.assertTrue(isinstance(resource, urldispatch.UnknownAPI))
def test_get_bad_header(self): "Test unknown version return error code & message via GET" request = DummyRequest(api_mode="", api_version="", api_name="") request.setHeader("X-DigiTar-API-Version", "dummyapi") resource = urldispatch.UnknownVersion() body = resource.render_GET(request) self.assertEquals(request.response_code, 406) self.assertEquals(request.getHeader("Content-Type"), "application/json; charset=utf-8") self.assertEquals(body, str(webapi.UnknownAPIVersionError(request, "dummyapi")))
def test_post(self): "Test unknown API call return error code & message via POST" request = DummyRequest(api_mode="", api_version="", api_name="") request.setHeader("X-DigiTar-API-Version", "dummyapi") request.uri = "dummy.com/dummyapi/call" resource = urldispatch.UnknownCall() body = resource.render_POST(request) self.assertEquals(request.response_code, 404) self.assertEquals(request.getHeader("Content-Type"), "application/json; charset=utf-8") self.assertEquals(body, str(webapi.UnknownAPICallError(request, "call")))
def test_get_child_route_match_api_match(self): "Test route match with good API name match in version header" route_map = [(r"^/example/", dummy_api)] api_config = {"arg1": True, "arg2": 42} request = DummyRequest(api_mode="prod", api_version="1.0", api_name="dummy_api") request.setHeader("X-DigiTar-API-Version", "dummy_api-1.0+prod") router = urldispatch.APIRouter(route_map, config=api_config) resource = router.getChild("/example/", request) self.assertTrue(isinstance(resource, urldispatch.VersionRouter)) self.assertEquals(resource.api_router, router)
def test_content_required_arg_missing_optional_flag_false(self): "Request JSON arguments missing required argument." test_request = DummyRequest() test_request.setHeader("Content-Type", "application/json; charset=utf-8") test_request.write(json.dumps({"arg2": 1})) outer_wrap = webapi.json_arguments([("arg1", unicode, False)]) inner_wrap = outer_wrap(self.dummy_render_func) self.assertEqual( str(webapi.ValueError(test_request, "arg1", "Argument is missing.")), inner_wrap(self, test_request))
def test_content_arg_type_mismatch_optional_arg(self): "Request JSON optional arguments contain data which doesn't match expected type." test_request = DummyRequest() test_request.setHeader("Content-Type", "application/json; charset=utf-8") test_request.write(json.dumps({"arg1": 1})) outer_wrap = webapi.json_arguments([("arg1", unicode, True)]) inner_wrap = outer_wrap(self.dummy_render_func) self.assertEqual( str( webapi.ValueError(test_request, "arg1", "Must be of type unicode")), inner_wrap(self, test_request))
def test_get_options(self): "Test OPTIONS request." route_map = [(r"^/example/", dummy_api)] api_config = {"arg1" : True, "arg2" : 42} request = DummyRequest(api_mode="prod", api_version="1.0", api_name="dummy_api") request.method = "OPTIONS" request.setHeader("Access-Control-Request-Headers", "Content-Type") router = urldispatch.APIRouter(route_map, config=api_config, cross_origin_domains="*") resource = router.getChild("/example/", request) self.assertTrue(isinstance(resource, urldispatch.CORSInterrogation)) resource.render_OPTIONS(request) self.assertEquals(request.getHeader("Content-Type"), "application/json; charset=utf-8") self.assertEquals(request.getHeader("Access-Control-Allow-Origin"), "*") self.assertEquals(request.getHeader("Access-Control-Allow-Credentials"), "true") self.assertEquals(request.getHeader("Access-Control-Allow-Headers"), "Content-Type") self.assertEquals(request.getHeader("Access-Control-Allow-Methods"), "PUT,GET,DELETE,POST,HEAD,TRACE,CONNECT,PROPFIND,PROPPATCH,MKCOL,COPY,MOVE,LOCK,UNLOCK")
def test_options_request_with_acrh(self): "Test a OPTIONS request w/ supplied Access-Control-Request-Headers header." route_map = [(r"^/example/", dummy_api)] api_config = {"arg1" : True, "arg2" : 42} router = urldispatch.APIRouter(route_map, config=api_config, cross_origin_domains="*") request = DummyRequest(api_mode="", api_version="", api_name="") request.setHeader("Access-Control-Request-Headers", "Content-Type") resource = urldispatch.CORSInterrogation(request, api_router=router) body = resource.render_OPTIONS(request) self.assertEquals(request.response_code, 200) self.assertEquals(request.getHeader("Content-Type"), "application/json; charset=utf-8") self.assertEquals(request.getHeader("Access-Control-Allow-Origin"), "*") self.assertEquals(request.getHeader("Access-Control-Allow-Credentials"), "true") self.assertEquals(request.getHeader("Access-Control-Allow-Headers"), "Content-Type") self.assertEquals(request.getHeader("Access-Control-Allow-Methods"), "PUT,GET,DELETE,POST,HEAD,TRACE,CONNECT,PROPFIND,PROPPATCH,MKCOL,COPY,MOVE,LOCK,UNLOCK") self.assertEquals(body, "")
class UtilityFunctionsTestCase(unittest.TestCase): def setUp(self): self.request = DummyRequest(api_mode="", api_version="", api_name="") def test_get_version_no_header_no_queryarg(self): "Version extraction - no version supplied" result = urldispatch.get_version(self.request) self.assertEquals(result["api"], "") self.assertEquals(result["version"], "") self.assertEquals(result["mode"], "") def test_get_version_in_query_arg(self): "Version extraction - version in query string" self.request.args["X-DigiTar-API-Version"] = ["dummy_api-1.0+prod"] result = urldispatch.get_version(self.request) self.assertEquals(result["api"], "dummy_api") self.assertEquals(result["version"], "1.0") self.assertEquals(result["mode"], "prod") def test_get_version_in_header(self): "Version extraction - version in header" self.request.setHeader("X-DigiTar-API-Version", "dummy_api-1.0+prod") result = urldispatch.get_version(self.request) self.assertEquals(result["api"], "dummy_api") self.assertEquals(result["version"], "1.0") self.assertEquals(result["mode"], "prod") def test_get_version_bad_version(self): "Version extraction - malformed version" self.request.setHeader("X-DigiTar-API-Version", "badmojo") self.assertRaises(IndexError, urldispatch.get_version, self.request) def tearDown(self): del(self.request)
class VersionRouterTestCase(unittest.TestCase): def setUp(self): self.request = DummyRequest(api_mode="", api_version="1.0", api_name="") self.route_map = [("^/test/me/(?P<group1>.+)", calls.PingCall)] self.api_router = urldispatch.APIRouter(self.route_map) self.version_map = {"1.0": (r"1.0", v1_0)} self.version_router = urldispatch.VersionRouter( self.version_map, self.api_router) def test_router_init(self): "Validate version router initialization." self.assertEqual(self.api_router, self.version_router.api_router) for key in self.version_map.keys(): self.assertTrue(self.version_router.version_map.has_key(key)) self.assertEqual(self.version_router.version_map[key][0].pattern, self.version_map[key][0] + r"$") def test_get_child_malformed_version(self): "Validate retrieving resource with malformed version header." self.request.setHeader("X-DigiTar-API-Version", "bobbledygook") res = self.version_router.getChild("noname", self.request) self.assertTrue(isinstance(res, urldispatch.UnknownVersion)) def test_get_child_no_version_match(self): "Validate retrieving resource with invalid version." self.request.setHeader("X-DigiTar-API-Version", "testapi-2.0+prod") res = self.version_router.getChild("noname", self.request) self.assertTrue(isinstance(res, urldispatch.UnknownVersion)) def test_get_child_version_match(self): "Validate retrieving valid resource." self.request.setHeader("X-DigiTar-API-Version", "testapi-1.0+prod") self.request.uri = "/level1/ping" res = self.version_router.getChild("noname", self.request) self.assertTrue(isinstance(res, calls.PingCall)) def test_get_version_map(self): "Validate correct version map is returned." self.assertEqual(self.version_router.version_map, self.version_router.get_version_map())
class VersionRouterTestCase(unittest.TestCase): def setUp(self): self.request = DummyRequest(api_mode="", api_version="1.0", api_name="") self.route_map = [("^/test/me/(?P<group1>.+)", calls.PingCall)] self.api_router = urldispatch.APIRouter(self.route_map) self.version_map = {"1.0" : (r"1.0", v1_0)} self.version_router = urldispatch.VersionRouter(self.version_map, self.api_router) def test_router_init(self): "Validate version router initialization." self.assertEqual(self.api_router, self.version_router.api_router) for key in self.version_map.keys(): self.assertTrue(self.version_router.version_map.has_key(key)) self.assertEqual(self.version_router.version_map[key][0].pattern, self.version_map[key][0] + r"$") def test_get_child_malformed_version(self): "Validate retrieving resource with malformed version header." self.request.setHeader("X-DigiTar-API-Version", "bobbledygook") res = self.version_router.getChild("noname", self.request) self.assertTrue(isinstance(res, urldispatch.UnknownVersion)) def test_get_child_no_version_match(self): "Validate retrieving resource with invalid version." self.request.setHeader("X-DigiTar-API-Version", "testapi-2.0+prod") res = self.version_router.getChild("noname", self.request) self.assertTrue(isinstance(res, urldispatch.UnknownVersion)) def test_get_child_version_match(self): "Validate retrieving valid resource." self.request.setHeader("X-DigiTar-API-Version", "testapi-1.0+prod") self.request.uri = "/level1/ping" res = self.version_router.getChild("noname", self.request) self.assertTrue(isinstance(res, calls.PingCall)) def test_get_version_map(self): "Validate correct version map is returned." self.assertEqual(self.version_router.version_map, self.version_router.get_version_map())
class CallRouterTestCase(unittest.TestCase): def setUp(self): self.request = DummyRequest(api_mode="", api_version="1.0", api_name="") self.route_map = [("^/test/me/(?P<group1>.+)$", calls.PingCall)] self.api_router = urldispatch.APIRouter(self.route_map) self.version_router = urldispatch.VersionRouter( { "1.0": (r"1.0", object()), "0.9": (r"0.9", object()) }, self.api_router) def test_call_router_init_no_list_versions(self): "Validate call router initialization w/o version listing resource." call_router = urldispatch.CallRouter(calls, self.version_router) self.assertEqual(call_router.version_router, self.version_router) for route in call_router.route_map: self.assertEqual(route[0].pattern, route[1].routes + "$") def test_call_router_init_list_versions(self): "Validate call router initialization w/ version listing resource." call_router = urldispatch.CallRouter(calls, self.version_router, True) self.assertEqual(call_router.version_router, self.version_router) found_list_versions = False for route in call_router.route_map: self.assertEqual(route[0].pattern, route[1].routes + "$") if route[0].pattern == "list_versions$" and \ route[1].__name__ == "ListVersionsCall": found_list_versions = True self.assertEqual(found_list_versions, True) def test_create_route_map_from_string(self): "Validate building route map from single route string." call_router = urldispatch.CallRouter(calls, self.version_router) self.assertEqual(len(call_router.route_map), 1) route = call_router.route_map[0] self.assertEqual(route[0].pattern, "ping$") self.assertEqual(route[1].__name__, "PingCall") def test_create_route_map_from_unicode(self): "Validate building route map from single route unicode string." call_router = urldispatch.CallRouter(calls_unicode, self.version_router) self.assertEqual(len(call_router.route_map), 1) route = call_router.route_map[0] self.assertEqual(route[0].pattern, "ping$") self.assertEqual(route[1].__name__, "PingCall") def test_create_route_map_from_list(self): "Validate building route map from list of route strings." call_router = urldispatch.CallRouter(calls_list, self.version_router) self.assertEqual(len(call_router.route_map), 2) ping_route_found = False me_route_found = False for route in call_router.route_map: if route[0].pattern == "ping(?P<group1>.*)$" and \ route[1].__name__ == "PingCall": ping_route_found = True if route[0].pattern == "me$" and \ route[1].__name__ == "PingCall": me_route_found = True self.assertEqual(ping_route_found, True) self.assertEqual(me_route_found, True) def test_get_child_malformed_version(self): "Validate retrieving resource with a malformed version header." call_router = urldispatch.CallRouter(calls_list, self.version_router) self.request.setHeader("X-DigiTar-API-Version", "bobbledygook") res = call_router.getChild("noname", self.request) self.assertTrue(isinstance(res, urldispatch.UnknownVersion)) def test_get_child_unknown_api_mode(self): "Validate retrieving resource with an unknown API mode." call_router = urldispatch.CallRouter(calls_list, self.version_router) self.request.setHeader("X-DigiTar-API-Version", "test-1.0+badmode") res = call_router.getChild("noname", self.request) self.assertTrue(isinstance(res, urldispatch.UnknownVersion)) def test_get_child_no_route_match(self): "Validate retrieving non-existent resource." call_router = urldispatch.CallRouter(calls_list, self.version_router) self.request.setHeader("X-DigiTar-API-Version", "test-1.0+prod") self.request.uri = "/myroute" res = call_router.getChild("noname", self.request) self.assertTrue(isinstance(res, urldispatch.UnknownCall)) def test_get_child_route_match(self): "Validate retrieving valid resource." call_router = urldispatch.CallRouter(calls_list, self.version_router) self.request.setHeader("X-DigiTar-API-Version", "test-1.0+prod") self.request.uri = "/level1/ping%26io" res = call_router.getChild("noname", self.request) self.assertTrue(isinstance(res, calls_list.PingCall)) self.assertTrue(res.url_matches.has_key("group1")) self.assertEqual(res.url_matches["group1"], "&io") def test_get_child_route_match_url_arguments(self): "Validate that URL arguments do not interfere with matching a valid resource." call_router = urldispatch.CallRouter(calls_list, self.version_router) self.request.setHeader("X-DigiTar-API-Version", "test-1.0+prod") self.request.uri = "/level1/ping%26io?arg1=ignored" res = call_router.getChild("noname", self.request) self.assertTrue(isinstance(res, calls_list.PingCall)) self.assertTrue(res.url_matches.has_key("group1")) self.assertEqual(res.url_matches["group1"], "&io")
class CallRouterTestCase(unittest.TestCase): def setUp(self): self.request = DummyRequest(api_mode="", api_version="1.0", api_name="") self.route_map = [("^/test/me/(?P<group1>.+)$", calls.PingCall)] self.api_router = urldispatch.APIRouter(self.route_map) self.version_router = urldispatch.VersionRouter({"1.0" : (r"1.0", object()), "0.9" : (r"0.9", object())}, self.api_router) def test_call_router_init_no_list_versions(self): "Validate call router initialization w/o version listing resource." call_router = urldispatch.CallRouter(calls, self.version_router) self.assertEqual(call_router.version_router, self.version_router) for route in call_router.route_map: self.assertEqual(route[0].pattern, route[1].routes + "$") def test_call_router_init_list_versions(self): "Validate call router initialization w/ version listing resource." call_router = urldispatch.CallRouter(calls, self.version_router, True) self.assertEqual(call_router.version_router, self.version_router) found_list_versions = False for route in call_router.route_map: self.assertEqual(route[0].pattern, route[1].routes + "$") if route[0].pattern == "list_versions$" and \ route[1].__name__ == "ListVersionsCall": found_list_versions = True self.assertEqual(found_list_versions, True) def test_create_route_map_from_string(self): "Validate building route map from single route string." call_router = urldispatch.CallRouter(calls, self.version_router) self.assertEqual(len(call_router.route_map), 1) route = call_router.route_map[0] self.assertEqual(route[0].pattern, "ping$") self.assertEqual(route[1].__name__, "PingCall") def test_create_route_map_from_unicode(self): "Validate building route map from single route unicode string." call_router = urldispatch.CallRouter(calls_unicode, self.version_router) self.assertEqual(len(call_router.route_map), 1) route = call_router.route_map[0] self.assertEqual(route[0].pattern, "ping$") self.assertEqual(route[1].__name__, "PingCall") def test_create_route_map_from_list(self): "Validate building route map from list of route strings." call_router = urldispatch.CallRouter(calls_list, self.version_router) self.assertEqual(len(call_router.route_map), 2) ping_route_found = False me_route_found = False for route in call_router.route_map: if route[0].pattern == "ping(?P<group1>.*)$" and \ route[1].__name__ == "PingCall": ping_route_found = True if route[0].pattern == "me$" and \ route[1].__name__ == "PingCall": me_route_found = True self.assertEqual(ping_route_found, True) self.assertEqual(me_route_found, True) def test_get_child_malformed_version(self): "Validate retrieving resource with a malformed version header." call_router = urldispatch.CallRouter(calls_list, self.version_router) self.request.setHeader("X-DigiTar-API-Version", "bobbledygook") res = call_router.getChild("noname", self.request) self.assertTrue(isinstance(res, urldispatch.UnknownVersion)) def test_get_child_unknown_api_mode(self): "Validate retrieving resource with an unknown API mode." call_router = urldispatch.CallRouter(calls_list, self.version_router) self.request.setHeader("X-DigiTar-API-Version", "test-1.0+badmode") res = call_router.getChild("noname", self.request) self.assertTrue(isinstance(res, urldispatch.UnknownVersion)) def test_get_child_no_route_match(self): "Validate retrieving non-existent resource." call_router = urldispatch.CallRouter(calls_list, self.version_router) self.request.setHeader("X-DigiTar-API-Version", "test-1.0+prod") self.request.uri = "/myroute" res = call_router.getChild("noname", self.request) self.assertTrue(isinstance(res, urldispatch.UnknownCall)) def test_get_child_route_match(self): "Validate retrieving valid resource." call_router = urldispatch.CallRouter(calls_list, self.version_router) self.request.setHeader("X-DigiTar-API-Version", "test-1.0+prod") self.request.uri = "/level1/ping%26io" res = call_router.getChild("noname", self.request) self.assertTrue(isinstance(res, calls_list.PingCall)) self.assertTrue(res.url_matches.has_key("group1")) self.assertEqual(res.url_matches["group1"], "&io") def test_get_child_route_match_url_arguments(self): "Validate that URL arguments do not interfere with matching a valid resource." call_router = urldispatch.CallRouter(calls_list, self.version_router) self.request.setHeader("X-DigiTar-API-Version", "test-1.0+prod") self.request.uri = "/level1/ping%26io?arg1=ignored" res = call_router.getChild("noname", self.request) self.assertTrue(isinstance(res, calls_list.PingCall)) self.assertTrue(res.url_matches.has_key("group1")) self.assertEqual(res.url_matches["group1"], "&io")