当前位置: 首页>>代码示例>>Python>>正文


Python Sanic.url_for方法代码示例

本文整理汇总了Python中sanic.Sanic.url_for方法的典型用法代码示例。如果您正苦于以下问题:Python Sanic.url_for方法的具体用法?Python Sanic.url_for怎么用?Python Sanic.url_for使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sanic.Sanic的用法示例。


在下文中一共展示了Sanic.url_for方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_fails_url_build_if_params_not_passed

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import url_for [as 别名]
def test_fails_url_build_if_params_not_passed():
    app = Sanic('fail_url_build')

    @app.route('/fail')
    def fail():
        return text('this should fail')

    with pytest.raises(ValueError) as e:
        app.url_for('fail', _scheme='http')

    assert str(e.value) == 'When specifying _scheme, _external must be True'
开发者ID:penelopeia,项目名称:sanic,代码行数:13,代码来源:test_url_building.py

示例2: test_fails_if_endpoint_not_found

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import url_for [as 别名]
def test_fails_if_endpoint_not_found():
    app = Sanic('fail_url_build')

    @app.route('/fail')
    def fail():
        return text('this should fail')

    with pytest.raises(URLBuildError) as e:
        app.url_for('passes')

    assert str(e.value) == 'Endpoint with name `passes` was not found'
开发者ID:penelopeia,项目名称:sanic,代码行数:13,代码来源:test_url_building.py

示例3: test_fails_with_int_message

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import url_for [as 别名]
def test_fails_with_int_message():
    app = Sanic('fail_url_build')

    @app.route(COMPLEX_PARAM_URL)
    def fail():
        return text('this should fail')

    failing_kwargs = dict(PASSING_KWARGS)
    failing_kwargs['foo'] = 'not_int'

    with pytest.raises(URLBuildError) as e:
        app.url_for('fail', **failing_kwargs)

    expected_error = (
        'Value "not_int" for parameter `foo` '
        'does not match pattern for type `int`: \d+')
    assert str(e.value) == expected_error
开发者ID:penelopeia,项目名称:sanic,代码行数:19,代码来源:test_url_building.py

示例4: test_fails_with_number_message

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import url_for [as 别名]
def test_fails_with_number_message():
    app = Sanic('fail_url_build')

    @app.route(COMPLEX_PARAM_URL)
    def fail():
        return text('this should fail')

    failing_kwargs = dict(PASSING_KWARGS)
    failing_kwargs['some_number'] = 'foo'

    with pytest.raises(URLBuildError) as e:
        app.url_for('fail', **failing_kwargs)

    expected_error = (
        'Value "foo" for parameter `some_number` '
        'does not match pattern for type `float`: [0-9\\\\.]+')

    assert str(e.value) == expected_error
开发者ID:penelopeia,项目名称:sanic,代码行数:20,代码来源:test_url_building.py

示例5: test_fails_with_two_letter_string_message

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import url_for [as 别名]
def test_fails_with_two_letter_string_message():
    app = Sanic('fail_url_build')

    @app.route(COMPLEX_PARAM_URL)
    def fail():
        return text('this should fail')

    failing_kwargs = dict(PASSING_KWARGS)
    failing_kwargs['two_letter_string'] = 'foobar'

    with pytest.raises(URLBuildError) as e:
        app.url_for('fail', **failing_kwargs)

    expected_error = (
        'Value "foobar" for parameter `two_letter_string` '
        'does not satisfy pattern [A-z]{2}')

    assert str(e.value) == expected_error
开发者ID:penelopeia,项目名称:sanic,代码行数:20,代码来源:test_url_building.py

示例6: test_simple_url_for_getting_with_more_params

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import url_for [as 别名]
def test_simple_url_for_getting_with_more_params(args, url):
    app = Sanic('more_url_build')

    @app.route('/myurl')
    def passes(request):
        return text('this should pass')

    assert url == app.url_for('passes', **args)
    request, response = app.test_client.get(url)
    assert response.status == 200
    assert response.text == 'this should pass'
开发者ID:penelopeia,项目名称:sanic,代码行数:13,代码来源:test_url_building.py

示例7: test_fails_url_build_if_param_not_passed

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import url_for [as 别名]
def test_fails_url_build_if_param_not_passed():
    url = '/'

    for letter in string.ascii_letters:
        url += '<{}>/'.format(letter)

    app = Sanic('fail_url_build')

    @app.route(url)
    def fail():
        return text('this should fail')

    fail_args = list(string.ascii_letters)
    fail_args.pop()

    fail_kwargs = {l: l for l in fail_args}

    with pytest.raises(URLBuildError) as e:
        app.url_for('fail', **fail_kwargs)

    assert 'Required parameter `Z` was not passed to url_for' in str(e.value)
开发者ID:penelopeia,项目名称:sanic,代码行数:23,代码来源:test_url_building.py

示例8: test_adds_other_supplied_values_as_query_string

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import url_for [as 别名]
def test_adds_other_supplied_values_as_query_string():
    app = Sanic('passes')

    @app.route(COMPLEX_PARAM_URL)
    def passes():
        return text('this should pass')

    new_kwargs = dict(PASSING_KWARGS)
    new_kwargs['added_value_one'] = 'one'
    new_kwargs['added_value_two'] = 'two'

    url = app.url_for('passes', **new_kwargs)

    query = dict(parse_qsl(urlsplit(url).query))

    assert query['added_value_one'] == 'one'
    assert query['added_value_two'] == 'two'
开发者ID:penelopeia,项目名称:sanic,代码行数:19,代码来源:test_url_building.py


注:本文中的sanic.Sanic.url_for方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。