def test_capture_path_complex(template): router = DefaultRouter() with pytest.raises( ValueError, match='Cannot use converter "path" of variable "bar" in a template ' ): router.add_route(template, ResourceWithId(1))
def test_match_entire_path(uri_template, path): router = DefaultRouter() router.add_route(uri_template, ResourceWithId(1)) route = router.find(path) assert route is None
def test_capture_path_no_match(): router = DefaultRouter() router.add_route('/foo/bar/baz', ResourceWithId(1)) router.add_route('/foo/{bar:path}', ResourceWithId(2)) router.add_route('/foo/bar/{foo:path}', ResourceWithId(3)) assert router.find('/foo') is None
def capture_path_router(): router = DefaultRouter() router.add_route('/foo/bar/baz', ResourceWithId(1)) router.add_route('/foo/{bar:path}', ResourceWithId(2)) router.add_route('/foo/bar/{foo:path}', ResourceWithId(3)) router.add_route('/{baz:path}', ResourceWithId(4)) router.add_route('/x/{v1:int}/{v2}/{other:path}', ResourceWithId(5)) router.add_route('/y/{v1:int}/{v2:int}/{other:path}', ResourceWithId(6)) return router
def test_user_regression_special_chars(uri_template, path, expected_params): router = DefaultRouter() router.add_route(uri_template, ResourceWithId(1)) route = router.find(path) assert route is not None resource, __, params, __ = route assert resource.resource_id == 1 assert params == expected_params
def param_router(): r = DefaultRouter() r.add_route('/c/foo/{bar}/baz', ResourceWithId(1)) r.add_route('/c/{foo}/bar/other', ResourceWithId(2)) r.add_route('/c/foo/{a:int}-{b}/a', ResourceWithId(3)) r.add_route('/upload/{service}/auth/token', ResourceWithId(4)) r.add_route('/upload/youtube/{project_id}/share', ResourceWithId(5)) r.add_route('/x/y/{a}.{b}/z', ResourceWithId(6)) r.add_route('/x/{y}/o.o/w', ResourceWithId(7)) return r
def router(): router = DefaultRouter() router.add_route('/repos', {}, ResourceWithId(1)) router.add_route('/repos/{org}', {}, ResourceWithId(2)) router.add_route('/repos/{org}/{repo}', {}, ResourceWithId(3)) router.add_route('/repos/{org}/{repo}/commits', {}, ResourceWithId(4)) router.add_route( u'/repos/{org}/{repo}/compare/{usr0}:{branch0}...{usr1}:{branch1}', {}, ResourceWithId(5)) router.add_route('/teams/{id}', {}, ResourceWithId(6)) router.add_route('/teams/{id}/members', {}, ResourceWithId(7)) router.add_route('/teams/default', {}, ResourceWithId(19)) router.add_route('/teams/default/members/thing', {}, ResourceWithId(19)) router.add_route('/user/memberships', {}, ResourceWithId(8)) router.add_route('/emojis', {}, ResourceWithId(9)) router.add_route( '/repos/{org}/{repo}/compare/{usr0}:{branch0}...{usr1}:{branch1}/full', {}, ResourceWithId(10)) router.add_route('/repos/{org}/{repo}/compare/all', {}, ResourceWithId(11)) # NOTE(kgriffs): The ordering of these calls is significant; we # need to test that the {id} field does not match the other routes, # regardless of the order they are added. router.add_route('/emojis/signs/0', {}, ResourceWithId(12)) router.add_route('/emojis/signs/{id}', {}, ResourceWithId(13)) router.add_route('/emojis/signs/42', {}, ResourceWithId(14)) router.add_route('/emojis/signs/42/small.jpg', {}, ResourceWithId(23)) router.add_route('/emojis/signs/78/small.png', {}, ResourceWithId(24)) # Test some more special chars router.add_route('/emojis/signs/78/small(png)', {}, ResourceWithId(25)) router.add_route('/emojis/signs/78/small_png', {}, ResourceWithId(26)) router.add_route('/images/{id}.gif', {}, ResourceWithId(27)) router.add_route( '/repos/{org}/{repo}/compare/{usr0}:{branch0}...{usr1}:{branch1}/part', {}, ResourceWithId(15)) router.add_route('/repos/{org}/{repo}/compare/{usr0}:{branch0}', {}, ResourceWithId(16)) router.add_route('/repos/{org}/{repo}/compare/{usr0}:{branch0}/full', {}, ResourceWithId(17)) router.add_route('/gists/{id}/{representation}', {}, ResourceWithId(21)) router.add_route('/gists/{id}/raw', {}, ResourceWithId(18)) router.add_route('/gists/first', {}, ResourceWithId(20)) router.add_route('/item/{q}', {}, ResourceWithId(28)) return router
def before(self): self.router = DefaultRouter() self.router.add_route('/repos', {}, ResourceWithId(1)) self.router.add_route('/repos/{org}', {}, ResourceWithId(2)) self.router.add_route('/repos/{org}/{repo}', {}, ResourceWithId(3)) self.router.add_route('/repos/{org}/{repo}/commits', {}, ResourceWithId(4)) self.router.add_route( '/repos/{org}/{repo}/compare/{usr0}:{branch0}...{usr1}:{branch1}', {}, ResourceWithId(5)) self.router.add_route('/teams/{id}', {}, ResourceWithId(6)) self.router.add_route('/teams/{id}/members', {}, ResourceWithId(7)) self.router.add_route('/teams/default', {}, ResourceWithId(19)) self.router.add_route('/teams/default/members/thing', {}, ResourceWithId(19)) self.router.add_route('/user/memberships', {}, ResourceWithId(8)) self.router.add_route('/emojis', {}, ResourceWithId(9)) self.router.add_route( '/repos/{org}/{repo}/compare/{usr0}:{branch0}...{usr1}:{branch1}/full', {}, ResourceWithId(10)) self.router.add_route('/repos/{org}/{repo}/compare/all', {}, ResourceWithId(11)) # NOTE(kgriffs): The ordering of these calls is significant; we # need to test that the {id} field does not match the other routes, # regardless of the order they are added. self.router.add_route('/emojis/signs/0', {}, ResourceWithId(12)) self.router.add_route('/emojis/signs/{id}', {}, ResourceWithId(13)) self.router.add_route('/emojis/signs/42', {}, ResourceWithId(14)) self.router.add_route('/emojis/signs/42/small', {}, ResourceWithId(14.1)) self.router.add_route('/emojis/signs/78/small', {}, ResourceWithId(22)) self.router.add_route( '/repos/{org}/{repo}/compare/{usr0}:{branch0}...{usr1}:{branch1}/part', {}, ResourceWithId(15)) self.router.add_route('/repos/{org}/{repo}/compare/{usr0}:{branch0}', {}, ResourceWithId(16)) self.router.add_route( '/repos/{org}/{repo}/compare/{usr0}:{branch0}/full', {}, ResourceWithId(17)) self.router.add_route('/gists/{id}/{representation}', {}, ResourceWithId(21)) self.router.add_route('/gists/{id}/raw', {}, ResourceWithId(18)) self.router.add_route('/gists/first', {}, ResourceWithId(20)) self.router.add_route('/item/{q}', {}, ResourceWithId(22))
def test_user_regression_recipes(): router = DefaultRouter() router.add_route('/recipes/{activity}/{type_id}', ResourceWithId(1)) router.add_route('/recipes/baking', ResourceWithId(2)) resource, __, __, __ = router.find('/recipes/baking/4242') assert resource.resource_id == 1 resource, __, __, __ = router.find('/recipes/baking') assert resource.resource_id == 2 route = router.find('/recipes/grilling') assert route is None
def test_capture_path_no_children(): router = DefaultRouter() router.add_route('/foo/{bar:path}', ResourceWithId(1)) res = router.finder_src with pytest.raises( ValueError, match='Cannot add route with template "/foo/{bar:path}/child". ' 'Field name "bar" uses the converter "path"', ): router.add_route('/foo/{bar:path}/child', ResourceWithId(1)) with pytest.raises( ValueError, match='Cannot add route with template "/{bar:path}/child". ' 'Field name "bar" uses the converter "path"', ): router.add_route('/{bar:path}/child', ResourceWithId(1)) assert res == router.finder_src
def test_root_path(): router = DefaultRouter() router.add_route('/', ResourceWithId(42)) resource, __, __, __ = router.find('/') assert resource.resource_id == 42 expected_src = textwrap.dedent(""" def find(path, return_values, patterns, converters, params): path_len = len(path) if path_len > 0: if path[0] == '': if path_len == 1: return return_values[0] return None return None return None """).strip() assert router.finder_src == expected_src
def test_user_regression_versioned_url(): router = DefaultRouter() router.add_route('/{version}/messages', ResourceWithId(2)) resource, __, __, __ = router.find('/v2/messages') assert resource.resource_id == 2 router.add_route('/v2', ResourceWithId(1)) resource, __, __, __ = router.find('/v2') assert resource.resource_id == 1 resource, __, __, __ = router.find('/v2/messages') assert resource.resource_id == 2 resource, __, __, __ = router.find('/v1/messages') assert resource.resource_id == 2 route = router.find('/v1') assert route is None
def before(self): from falcon.routing import DefaultRouter self.router = DefaultRouter() setup_routes(self.router)
def test_duplicate_field_names(uri_template): router = DefaultRouter() with pytest.raises(ValueError): router.add_route(uri_template, ResourceWithId(1))
def router(): router = DefaultRouter() router.add_route("/browse/{id:int}/", lambda x: x) return router
def before(self): self.router = DefaultRouter()
def test_root_path(): router = DefaultRouter() router.add_route('/', {}, ResourceWithId(42)) resource, __, __, __ = router.find('/') assert resource.resource_id == 42