async def test_json():
    def handler(request):
        assert request.content_type == 'application/json'
        assert 'jso' in request
        return dict(request)

    sd = {'parameters': parameters}
    r = SwaggerValidationRoute('GET',
                               handler=handler,
                               resource=None,
                               swagger_data=sd)
    r.build_swagger_data(None)
    request = make_mocked_request(
        'POST',
        '/',
        headers=multidict.CIMultiDict({hdrs.CONTENT_TYPE: 'application/json'}),
    )
    request.json = asyncio.coroutine(lambda: {'f': ''})
    request._match_info = {}
    resp = await r.handler(request)
    assert isinstance(resp, dict), resp
    assert 'road_id' in resp, resp

    # not valid data
    request.json = asyncio.coroutine(lambda: {'f': 1})
    with pytest.raises(web.HTTPBadRequest):
        await r.handler(request)

    try:
        await r.handler(request)
    except web.HTTPBadRequest as e:
        resp = e

    assert resp['jso', 'f'], resp
Exemple #2
0
def test_route():
    sd = {'parameters': parameters}
    r = SwaggerValidationRoute('GET', handler=handler, resource=None)
    r.set_swagger(sd)
    request = make_mocked_request('GET', '/?road_id=1&road_id=2')
    request._match_info = {}
    resp = yield from r.handler(request)
    assert isinstance(resp, dict), resp
    assert 'road_id' in resp, resp
def test_route():
    sd = {'parameters': parameters}
    r = SwaggerValidationRoute(
        'GET', handler=handler, resource=None,
        swagger_data=sd)
    r.build_swagger_data({})
    query = ('/?road_id=1&road_id=2'
             '&road_id_csv=1,2'
             '&road_id_ssv=1%202'
             '&road_id_brackets[]=1&road_id_brackets[]=2')
    request = make_mocked_request('GET', query)
    request._match_info = {}
    resp = yield from r.handler(request)
    assert isinstance(resp, dict), resp
    assert 'road_id' in resp, resp
    assert resp.get('road_id_csv') == [1, 2], resp
    assert resp.get('road_id_ssv') == [1, 2], resp
    assert resp.get('road_id_brackets') == [1, 2], resp
def test_json():

    def handler(request):
        assert request.content_type == 'application/json'
        assert 'jso' in request
        return dict(request)

    sd = {'parameters': parameters}
    r = SwaggerValidationRoute(
        'GET', handler=handler, resource=None,
        swagger_data=sd)
    r.build_swagger_data({})
    request = make_mocked_request(
        'POST', '/',
        headers=multidict.CIMultiDict({
            hdrs.CONTENT_TYPE: 'application/json'
        }),
    )
    request.json = asyncio.coroutine(lambda: {'f': ''})
    request._match_info = {}
    resp = yield from r.handler(request)
    assert isinstance(resp, dict), resp
    assert 'road_id' in resp, resp

    # not valid data
    request.json = asyncio.coroutine(lambda: {'f': 1})
    with pytest.raises(web.HTTPBadRequest):
        yield from r.handler(request)

    try:
        yield from r.handler(request)
    except web.HTTPBadRequest as e:
        resp = e

    assert resp.reason['jso.f'], resp.reason
async def test_route():
    sd = {'parameters': parameters}
    r = SwaggerValidationRoute('GET',
                               handler=handler,
                               resource=None,
                               swagger_data=sd)
    r.build_swagger_data(None)
    query = ('/?road_id=1&road_id=2'
             '&road_id_csv=1,2'
             '&road_id_ssv=1%202'
             '&road_id_brackets[]=1&road_id_brackets[]=2')
    request = make_mocked_request('GET', query)
    request._match_info = {}
    resp = await r.handler(request)
    assert isinstance(resp, dict), resp
    assert 'road_id' in resp, resp
    assert resp.get('road_id_csv') == [1, 2], resp
    assert resp.get('road_id_ssv') == [1, 2], resp
    assert resp.get('road_id_brackets') == [1, 2], resp
    assert resp.get('road_id_default_csv') == [42], resp
    assert resp.get('road_id_default_brackets') == [12], resp
    assert resp.get('road_id_default_multi') == [24], resp