def test_add_resource_real_world(self): router = web.UrlDispatcher() with add_resource_context(router, '/api/', 'api') as add_resource: add_resource('/', dummy_handler, name='index') add_resource( '/news', get=dummy_handler, post=dummy_handler, name='news') add_resource( '/user/{user_id:\d+}', get=dummy_handler, patch=dummy_handler, put=dummy_handler, delete=dummy_handler, name='user') self.check_length(router.resources(), 3) self.check_length(router.routes(), 7) self.assertEqual(str(router['api.index'].url_for()), '/api/') self.assertEqual(str(router['api.news'].url_for()), '/api/news') self.assertEqual( str(router['api.user'].url_for(user_id='1')), '/api/user/1')
def test_add_resource_name_prefix_with_dot(self): router = web.UrlDispatcher() ctx = add_resource_context(router, name_prefix='with_dot.') with ctx as add_resource: add_resource('/with.dot', dummy_handler, name='index') self.assertEqual(str(router['with_dot.index'].url_for()), '/with.dot')
def test_add_resource_url_prefix_with_slash(self): router = web.UrlDispatcher() ctx = add_resource_context(router, url_prefix='/api/') with ctx as add_resource: add_resource('/', dummy_handler, name='index') self.assertEqual(str(router['index'].url_for()), '/api/')
def test_add_resource_missed_handler(self): router = web.UrlDispatcher() with add_resource_context(router) as add_resource: add_resource('/', None, post=None) self.check_length(router.resources(), 1) self.check_length(router.routes(), 0)
def test_add_resource_wildcard(self): router = web.UrlDispatcher() with add_resource_context(router) as add_resource: add_resource('/', **{'*': dummy_handler}) self.check_length(router.resources(), 1) self.check_length(router.routes(), 1)
def test_add_resource_name_prefix(self): router = web.UrlDispatcher(self.app) ctx = add_resource_context(router, name_prefix='prefix') with ctx as add_resource: add_resource('/', dummy_handler, name='index') self.assertEqual(router['prefix.index'].url(), '/')
def test_add_resource_name_prefix_with_dot(): router = web.UrlDispatcher() ctx = add_resource_context(router, name_prefix="with_dot.") with ctx as add_resource: add_resource("/with.dot", dummy_handler, name="index") assert str(router["with_dot.index"].url_for()) == "/with.dot"
def test_add_resource_missed_handler(): router = web.UrlDispatcher() with add_resource_context(router) as add_resource: add_resource("/", None, post=None) check_length(router.resources(), 1) check_length(router.routes(), 0)
def test_add_resource_wildcard(): router = web.UrlDispatcher() with add_resource_context(router) as add_resource: add_resource("/", **{"*": dummy_handler}) check_length(router.resources(), 1) check_length(router.routes(), 1)
def test_add_resource_url_prefix_with_slash(): router = web.UrlDispatcher() ctx = add_resource_context(router, url_prefix="/api/") with ctx as add_resource: add_resource("/", dummy_handler, name="index") assert str(router["index"].url_for()) == "/api/"
def test_add_resource(self): router = web.UrlDispatcher() self.check_length(router.resources(), 0) self.check_length(router.routes(), 0) with add_resource_context(router) as add_resource: add_resource('/', dummy_handler, head=dummy_handler) self.check_length(router.resources(), 1) self.check_length(router.routes(), 2)
def test_add_resource(): router = web.UrlDispatcher() check_length(router.resources(), 0) check_length(router.routes(), 0) with add_resource_context(router) as add_resource: add_resource("/", dummy_handler, head=dummy_handler) check_length(router.resources(), 1) check_length(router.routes(), 2)
def test_add_resource_url_prefix(self): router = web.UrlDispatcher(self.app) ctx = add_resource_context(router, url_prefix='/api') with ctx as add_resource: add_resource('/', dummy_handler, name='index') add_resource('/posts', get=dummy_handler, post=dummy_handler, name='posts') self.check_length(router.resources(), 2) self.check_length(router.routes(), 3) self.assertEqual(router['index'].url(), '/api/') self.assertEqual(router['posts'].url(), '/api/posts')
def test_add_resource_url_prefix(): router = web.UrlDispatcher() ctx = add_resource_context(router, url_prefix="/api") with ctx as add_resource: add_resource("/", dummy_handler, name="index") add_resource("/posts", get=dummy_handler, post=dummy_handler, name="posts") check_length(router.resources(), 2) check_length(router.routes(), 3) assert str(router["index"].url_for()) == "/api/" assert str(router["posts"].url_for()) == "/api/posts"
def test_add_resource_real_world(): router = web.UrlDispatcher() with add_resource_context(router, "/api/", "api") as add_resource: add_resource("/", dummy_handler, name="index") add_resource("/news", get=dummy_handler, post=dummy_handler, name="news") add_resource( r"/user/{user_id:\d+}", get=dummy_handler, patch=dummy_handler, put=dummy_handler, delete=dummy_handler, name="user", ) check_length(router.resources(), 3) check_length(router.routes(), 7) assert str(router["api.index"].url_for()) == "/api/" assert str(router["api.news"].url_for()) == "/api/news" assert str(router["api.user"].url_for(user_id="1")) == "/api/user/1"