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 = yield from test_client(app)

    url = dispatcher['static'].url_for(filename=f.name)
    responce = yield from client.get(url)
    assert responce.status == 200

    m = mocker.patch('aiohttp_apiset.dispatcher.mimetypes')
    m.guess_type.return_value = None, None
    responce = yield from client.get(url)
    assert responce.status == 200

    url = dispatcher['static'].url_for(filename='..' + f.name)
    responce = yield from client.get(url)
    assert responce.status == 403

    url = dispatcher['static'].url_for(filename='1/2/3')
    responce = yield from client.get(url)
    assert responce.status == 404

    url = dispatcher['static'].url_for(filename='')
    responce = yield from client.get(url)
    assert responce.status == 404
def test_view_locations(dispatcher: TreeUrlDispatcher):
    resources = dispatcher.resources()
    assert list(resources)[0] in resources
    assert len(resources)
    routes = dispatcher.routes()
    assert list(routes)[0] in routes
    assert len(routes)
def test_simple(dispatcher: TreeUrlDispatcher, request: web.Request):
    request = make_request('GET', '/api/1/pet/1')
    md = yield from dispatcher.resolve(request)
    assert md == {'id': '1'}

    request = make_request('GET', '/api/1/pets')
    md = yield from dispatcher.resolve(request)
    assert not md
def test_multisubs(dispatcher: TreeUrlDispatcher):
    url = '/api/1/host/{host}/eth{num}/{ip:[.\d]+}/'
    dispatcher.add_route('GET', url, handler)

    request = make_request('GET', '/api/1/host/myhost/eth0/127.0.0.1/')
    md = yield from dispatcher.resolve(request)
    assert len(md)
    assert 'ip' in md
    assert 'num' in md
    assert 'host' in md

    request = make_request('GET', '/api/1/host/myhost/eth0/127.0.0.1')
    md = yield from dispatcher.resolve(request)
    assert isinstance(md, MatchInfoError)
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 = yield from test_client(app)

    url = dispatcher['static'].url_for(filename='1/2/3')
    responce = yield from client.get(url)
    assert responce.status == 200

    url = dispatcher['static2'].url_for(filename='1/2/3')
    responce = yield from client.get(url)
    assert responce.status == 404

    url = dispatcher['static'].url_for(filename='')
    responce = yield from client.get(url)
    assert responce.status == 200
def test_add_resource(dispatcher: TreeUrlDispatcher):
    location = dispatcher.add_resource('/api/1/dogs', name='dogs')
    assert str(location.url_for()) == '/api/1/dogs'
def test_dispatcher_not_resolve():
    r = TreeUrlDispatcher()
    r.add_put('/', handler)
    req = make_request('GET', '/')
    a = yield from 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 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 test_similar_patterns():
    dispatcher = TreeUrlDispatcher()
    dispatcher.add_get('/{a}', handler)
    with pytest.raises(ValueError):
        dispatcher.add_get('/{b}', handler)