Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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
Exemplo n.º 4
0
async def test_dynamic_sort():
    r = TreeUrlDispatcher()
    r.add_get('/a/{b}-{c}', handler)
    route = r.add_get('/a/{b}-{c}.jpg', handler)
    req = make_request('GET', '/a/2-3.jpg')
    mi = await r.resolve(req)
    assert mi.route is route
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
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
Exemplo n.º 10
0
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)
Exemplo n.º 11
0
async 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 = await 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 = await dispatcher.resolve(request)
    assert isinstance(md, MatchInfoError)
Exemplo n.º 12
0
async def test_superrouter():
    request = make_request('GET', '/a/b/c/d')
    router = TreeUrlDispatcher()
    subapp = web.Application()
    route = subapp.router.add_get('/c/d', handler)
    app = web.Application(router=router)
    app.add_subapp('/a/b', subapp)
    m = await app.router.resolve(request)
    assert route == m.route
Exemplo n.º 13
0
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
Exemplo n.º 14
0
async def test_set_content_receiver(loop):
    async def test_receiver(request):
        pass

    r = TreeUrlDispatcher()
    r.set_content_receiver('test', test_receiver)
    r.add_post('/', 'tests.conftest.SimpleView.post')
    req = make_request('POST', '/', headers={'Content-Type': 'test'})
    mi = await r.resolve(req)
    assert mi.route._content_receiver.get('test') is test_receiver
Exemplo n.º 15
0
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
Exemplo n.º 16
0
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
Exemplo n.º 17
0
def test_add_resource(dispatcher: TreeUrlDispatcher):
    location = dispatcher.add_resource('/api/1/dogs', name='dogs')
    assert str(location.url_for()) == '/api/1/dogs'
Exemplo n.º 18
0
def test_similar_patterns():
    dispatcher = TreeUrlDispatcher()
    dispatcher.add_get('/{a}', handler)
    with pytest.raises(ValueError):
        dispatcher.add_get('/{b}', handler)
Exemplo n.º 19
0
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
Exemplo n.º 20
0
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)
Exemplo n.º 21
0
def test_novalid_path():
    r = TreeUrlDispatcher()
    with pytest.raises(ValueError):
        r.add_resource('dfsdf')
    with pytest.raises(ValueError):
        r.add_get('dfsdf', None)
Exemplo n.º 22
0
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
Exemplo n.º 23
0
def test_similar_patterns():
    dispatcher = TreeUrlDispatcher()
    dispatcher.add_get('/{a}', handler)
    with pytest.raises(ValueError):
        dispatcher.add_get('/{b}', handler)
Exemplo n.º 24
0
def test_novalid_path():
    r = TreeUrlDispatcher()
    with pytest.raises(ValueError):
        r.add_resource('dfsdf')
    with pytest.raises(ValueError):
        r.add_get('dfsdf', None)
Exemplo n.º 25
0
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)
Exemplo n.º 26
0
def test_add_resource(dispatcher: TreeUrlDispatcher):
    location = dispatcher.add_resource('/api/1/dogs', name='dogs')
    assert str(location.url_for()) == '/api/1/dogs'
Exemplo n.º 27
0
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