def test_add_route_context_with_name_prefix(self, app): with add_route_context(app, views, name_prefix='api') as route: route('GET', '/', 'index') route('GET', '/projects/', 'list_projects') assert app.router['api.index'].url() == '/' assert app.router['api.list_projects'].url() == '/projects/'
def test_add_route_context_passing_handler_functions(self, app): with add_route_context(app) as route: route('GET', '/', views.index) route('GET', '/projects/', views.list_projects) route('POST', '/projects', views.create_projects) assert app.router['index'].url() == '/' assert app.router['list_projects'].url() == '/projects/' assert app.router['create_projects'].url() == '/projects'
def test_add_route_context_basic(self, app): with add_route_context(app, views) as route: route('GET', '/', 'index') route('GET', '/projects/', 'list_projects') route('POST', '/projects', 'create_projects') assert app.router['index'].url() == '/' assert app.router['list_projects'].url() == '/projects/' assert app.router['create_projects'].url() == '/projects'
def test_passing_module_and_handlers_as_strings(self, app): with add_route_context(app, module='tests.views') as route: route('GET', '/', 'index') route('GET', '/projects/', 'list_projects') route('POST', '/projects', 'create_projects') assert app.router['index'].url() == '/' assert app.router['list_projects'].url() == '/projects/' assert app.router['create_projects'].url() == '/projects'
def test_add_route_context_passing_handler_functions(self, app): with add_route_context(app) as route: route('GET', '/', views.index) route('GET', '/projects/', views.list_projects) route('POST', '/projects', views.create_projects) assert str(app.router['index'].url_for()) == '/' assert str(app.router['list_projects'].url_for()) == '/projects/' assert str(app.router['create_projects'].url_for()) == '/projects'
def test_add_route_context_basic(self, app): with add_route_context(app, views) as route: route('GET', '/', 'index') route('GET', '/projects/', 'list_projects') route('POST', '/projects', 'create_projects') assert str(app.router['index'].url_for()) == '/' assert str(app.router['list_projects'].url_for()) == '/projects/' assert str(app.router['create_projects'].url_for()) == '/projects'
def test_passing_module_and_handlers_as_strings(self, app): with add_route_context(app, module='tests.views') as route: route('GET', '/', 'index') route('GET', '/projects/', 'list_projects') route('POST', '/projects', 'create_projects') assert str(app.router['index'].url_for()) == '/' assert str(app.router['list_projects'].url_for()) == '/projects/' assert str(app.router['create_projects'].url_for()) == '/projects'
def test_add_route_raises_error_if_handler_not_found(self, app): with add_route_context(app, views) as route: with pytest.raises(AttributeError): route('GET', '/', 'notfound')
def test_route_name_override(self, app): with add_route_context(app) as route: route('GET', '/', views.index, name='home') assert app.router['home'].url() == '/'
def test_route_name_override(self, app): with add_route_context(app) as route: route('GET', '/', views.index, name='home') assert str(app.router['home'].url_for()) == '/'
from aiohttp_utils import Response, routing, negotiation, run, path_norm app = web.Application(router=routing.ResourceRouter()) async def index(request): return Response('Hi There') class HelloResource(): async def get(self, request): name = request.GET.get('name', 'World') return response({'message': 'Hello' + name}) with routing.add_route_context(app) as route: route('GET', '/', index) # app.router.add_resource_object('/', HelloResource()) # Content negotiation # negotiation.setup( # app, renderers={ # 'application/json': negotiation.render_json # } # ) path_norm.setup(app) if __name__ == '__main__': # Development server
app = web.Application(router=routing.ResourceRouter()) @coroutine def index(request): return Response('Welcome!') class HelloResource: @coroutine def get(self, request): return Response({ 'message': 'Welcome to the API!' }) with routing.add_route_context(app) as route: route('GET', '/', index) with routing.add_resource_context(app, url_prefix='/api/') as route: route('/', HelloResource()) negotiation.setup(app) path_norm.setup(app) if __name__ == "__main__": run( app, app_uri="examples.kitchen_sink:app", reload=True, port=8000 )