update test for url_for and update routing.md doc
This commit is contained in:
parent
fb419eaa36
commit
4839ede64f
|
@ -11,7 +11,7 @@ from sanic.response import json
|
|||
@app.route("/")
|
||||
async def test(request):
|
||||
return json({ "hello": "world" })
|
||||
```
|
||||
```
|
||||
|
||||
When the url `http://server.url/` is accessed (the base url of the server), the
|
||||
final `/` is matched by the router to the handler function, `test`, which then
|
||||
|
@ -145,6 +145,28 @@ Other things to keep in mind when using `url_for`:
|
|||
url = app.url_for('post_handler', post_id=5, arg_one='one', arg_two='two')
|
||||
# /posts/5?arg_one=one&arg_two=two
|
||||
```
|
||||
- Multivalue argument can be passed to `url_for`. For example:
|
||||
```python
|
||||
url = app.url_for('post_handler', post_id=5, arg_one=['one', 'two'])
|
||||
# /posts/5?arg_one=one&arg_one=two
|
||||
```
|
||||
- Also some special arguments (`_anchor`, `_external`, `_scheme`, `_method`, `_server`) passed to `url_for` will have special url building (`_method` is not support now and will be ignored). For example:
|
||||
```python
|
||||
url = app.url_for('post_handler', post_id=5, arg_one='one', _anchor='anchor')
|
||||
# /posts/5?arg_one=one#anchor
|
||||
|
||||
url = app.url_for('post_handler', post_id=5, arg_one='one', _external=True)
|
||||
# //server/posts/5?arg_one=one
|
||||
# _external requires passed argument _server or SERVER_NAME in app.config or url will be same as no _external
|
||||
|
||||
url = app.url_for('post_handler', post_id=5, arg_one='one', _scheme='http', _external=True)
|
||||
# http://server/posts/5?arg_one=one
|
||||
# when specifying _scheme, _external must be True
|
||||
|
||||
# you can pass all special arguments one time
|
||||
url = app.url_for('post_handler', post_id=5, arg_one=['one', 'two'], arg_two=2, _anchor='anchor', _scheme='http', _external=True, _server='another_server:8888')
|
||||
# http://another_server:8888/posts/5?arg_one=one&arg_one=two&arg_two=2#anchor
|
||||
```
|
||||
- All valid parameters must be passed to `url_for` to build a URL. If a parameter is not supplied, or if a parameter does not match the specified type, a `URLBuildError` will be thrown.
|
||||
|
||||
|
||||
|
|
|
@ -283,10 +283,9 @@ class Sanic:
|
|||
replacement_regex, supplied_param, out)
|
||||
|
||||
# parse the remainder of the keyword arguments into a querystring
|
||||
if kwargs:
|
||||
query_string = urlencode(kwargs, doseq=True)
|
||||
# scheme://netloc/path;parameters?query#fragment
|
||||
out = urlunparse((scheme, netloc, out, '', query_string, anchor))
|
||||
query_string = urlencode(kwargs, doseq=True) if kwargs else ''
|
||||
# scheme://netloc/path;parameters?query#fragment
|
||||
out = urlunparse((scheme, netloc, out, '', query_string, anchor))
|
||||
|
||||
return out
|
||||
|
||||
|
|
|
@ -10,6 +10,14 @@ from sanic.exceptions import URLBuildError
|
|||
|
||||
import string
|
||||
|
||||
URL_FOR_ARGS1 = dict(arg1=['v1', 'v2'])
|
||||
URL_FOR_VALUE1 = '/myurl?arg1=v1&arg1=v2'
|
||||
URL_FOR_ARGS2 = dict(arg1=['v1', 'v2'], _anchor='anchor')
|
||||
URL_FOR_VALUE2 = '/myurl?arg1=v1&arg1=v2#anchor'
|
||||
URL_FOR_ARGS3 = dict(arg1='v1', _anchor='anchor', _scheme='http',
|
||||
_server='localhost:{}'.format(test_port), _external=True)
|
||||
URL_FOR_VALUE3 = 'http://localhost:{}/myurl?arg1=v1#anchor'.format(test_port)
|
||||
|
||||
|
||||
def _generate_handlers_from_names(app, l):
|
||||
for name in l:
|
||||
|
@ -39,28 +47,21 @@ def test_simple_url_for_getting(simple_app):
|
|||
assert response.text == letter
|
||||
|
||||
|
||||
def test_simple_url_for_getting_with_duplicate_params(simple_app):
|
||||
kw = dict(arg1=['value1', 'value2'], _anchor='anchor')
|
||||
for letter in string.ascii_letters:
|
||||
url = simple_app.url_for(letter, **kw)
|
||||
@pytest.mark.parametrize('args,url',
|
||||
[(URL_FOR_ARGS1, URL_FOR_VALUE1),
|
||||
(URL_FOR_ARGS2, URL_FOR_VALUE2),
|
||||
(URL_FOR_ARGS3, URL_FOR_VALUE3)])
|
||||
def test_simple_url_for_getting_with_more_params(args, url):
|
||||
app = Sanic('more_url_build')
|
||||
|
||||
assert url == '/{}?arg1=value1&arg1=value2#anchor'.format(letter)
|
||||
request, response = sanic_endpoint_test(simple_app, uri=url)
|
||||
assert response.status == 200
|
||||
assert response.text == letter
|
||||
@app.route('/myurl')
|
||||
def passes(request):
|
||||
return text('this should pass')
|
||||
|
||||
|
||||
def test_simple_url_for_getting_with_special_params(simple_app):
|
||||
kw = dict(arg1='value1', _anchor='anchor', _scheme='http',
|
||||
_server='localhost:{}'.format(test_port), _external=True)
|
||||
url_fmt = 'http://localhost:{}/{}?arg1=value1#anchor'
|
||||
for letter in string.ascii_letters:
|
||||
url = simple_app.url_for(letter, **kw)
|
||||
|
||||
assert url == url_fmt.format(test_port, letter)
|
||||
request, response = sanic_endpoint_test(simple_app, uri=url)
|
||||
assert response.status == 200
|
||||
assert response.text == letter
|
||||
assert url == app.url_for('passes', **args)
|
||||
request, response = sanic_endpoint_test(app, uri=url)
|
||||
assert response.status == 200
|
||||
assert response.text == 'this should pass'
|
||||
|
||||
|
||||
def test_fails_if_endpoint_not_found():
|
||||
|
|
Loading…
Reference in New Issue
Block a user