async def test_static(loop, test_client, mocker): f = Path(__file__) dispatcher = TreeUrlDispatcher() dispatcher.add_static('/static', f.parent, name='static') app = web.Application(router=dispatcher, loop=loop) client = await test_client(app) url = dispatcher['static'].url_for(filename=f.name) responce = await client.get(url) assert responce.status == 200 m = mocker.patch('aiohttp_apiset.dispatcher.mimetypes') m.guess_type.return_value = None, None responce = await client.get(url) assert responce.status == 200 url = dispatcher['static'].url_for(filename='..' + f.name) responce = await client.get(url) assert responce.status == 403 url = dispatcher['static'].url_for(filename='1/2/3') responce = await client.get(url) assert responce.status == 404 url = dispatcher['static'].url_for(filename='') responce = await client.get(url) assert responce.status == 404
async def test_init(): r = TreeUrlDispatcher() r.add_get('/', 'tests.conftest.ViewWithInit.get') req = make_request('GET', '/') mi = await r.resolve(req) result = await mi.handler(req) assert result is req, result
async def test_branch_path(): r = TreeUrlDispatcher() h = 'tests.conftest.ViewWithInit.get' r.add_get('/net/ip/', h) route = r.add_get('/net/{ip}/host', h) req = make_request('GET', '/net/ip/host') mi = await r.resolve(req) assert mi.route is route
def dispatcher(): d = TreeUrlDispatcher() d.add_route('GET', '/', handler) d.add_route('GET', '/api/', handler) d.add_route('GET', '/api', handler) d.add_route('*', '/api/1/pets', handler) d.add_route('GET', '/api/1/pet/{id}', handler, name='pet') d.add_route('GET', '/api/1/pet/{id}/', handler) return d
def dispatcher(): d = TreeUrlDispatcher() location = d.add_resource('/', name='root') location.add_route('GET', handler) d.add_route('GET', '/api/', handler) d.add_route('GET', '/api', handler) d.add_route('*', '/api/1/pets', handler) d.add_route('GET', '/api/1/pet/{id}', handler, name='pet') d.add_route('GET', '/api/1/pet/{id}/', handler) return d
async def test_static_with_default(loop, test_client): f = Path(__file__) dispatcher = TreeUrlDispatcher() dispatcher.add_static('/static', f.parent, name='static', default=f.name) dispatcher.add_static('/static2', f.parent, name='static2', default='1234') app = web.Application(router=dispatcher, loop=loop) client = await test_client(app) url = dispatcher['static'].url_for(filename='1/2/3') responce = await client.get(url) assert responce.status == 200 url = dispatcher['static2'].url_for(filename='1/2/3') responce = await client.get(url) assert responce.status == 404 url = dispatcher['static'].url_for(filename='') responce = await client.get(url) assert responce.status == 200
async def test_default_options(test_client): headers = {hdrs.ACCESS_CONTROL_REQUEST_HEADERS: hdrs.AUTHORIZATION} request = make_request('OPTIONS', '/', headers=headers) router = TreeUrlDispatcher() mi = await router.resolve(request) assert isinstance(mi, MatchInfoError) app = web.Application(router=router) router.set_cors(app) router.add_get('/', lambda request: web.Response()) mi = await router.resolve(request) assert not isinstance(mi, MatchInfoError) client = await test_client(app) response = await client.options('/', headers=headers) assert response.status == 200 h = response.headers assert h[hdrs.ACCESS_CONTROL_ALLOW_ORIGIN] == '*' assert h[hdrs.ACCESS_CONTROL_ALLOW_METHODS] == 'GET' assert h[hdrs.ACCESS_CONTROL_ALLOW_HEADERS] == hdrs.AUTHORIZATION
async def test_dispatcher_not_resolve(): r = TreeUrlDispatcher() r.add_put('/', handler) req = make_request('GET', '/') a = await r.resolve(req) assert isinstance(a.http_exception, web.HTTPMethodNotAllowed)
def test_novalid_path(): r = TreeUrlDispatcher() with pytest.raises(ValueError): r.add_resource('dfsdf') with pytest.raises(ValueError): r.add_get('dfsdf', None)
def test_similar_patterns(): dispatcher = TreeUrlDispatcher() dispatcher.add_get('/{a}', handler) with pytest.raises(ValueError): dispatcher.add_get('/{b}', handler)