本文整理汇总了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'
示例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'
示例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
示例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
示例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
示例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'
示例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)
示例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'