add UUID support (#1241)

This commit is contained in:
Volodymyr Maksymiv 2018-06-09 11:16:17 +03:00 committed by Raphael Deem
parent baa689ad43
commit 5ff481952d
2 changed files with 25 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import re import re
import uuid
from collections import defaultdict, namedtuple from collections import defaultdict, namedtuple
from collections.abc import Iterable from collections.abc import Iterable
from functools import lru_cache from functools import lru_cache
@ -18,6 +19,8 @@ REGEX_TYPES = {
'number': (float, r'[0-9\\.]+'), 'number': (float, r'[0-9\\.]+'),
'alpha': (str, r'[A-Za-z]+'), 'alpha': (str, r'[A-Za-z]+'),
'path': (str, r'[^/].*?'), 'path': (str, r'[^/].*?'),
'uuid': (uuid.UUID, r'[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-'
r'[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}')
} }
ROUTER_CACHE_SIZE = 1024 ROUTER_CACHE_SIZE = 1024

View File

@ -422,6 +422,28 @@ def test_dynamic_route_regex():
assert response.status == 200 assert response.status == 200
def test_dynamic_route_uuid():
import uuid
app = Sanic('test_dynamic_route_uuid')
results = []
@app.route('/quirky/<unique_id:uuid>')
async def handler(request, unique_id):
results.append(unique_id)
return text('OK')
request, response = app.test_client.get('/quirky/123e4567-e89b-12d3-a456-426655440000')
assert response.text == 'OK'
assert type(results[0]) is uuid.UUID
request, response = app.test_client.get('/quirky/{}'.format(uuid.uuid4()))
assert response.status == 200
request, response = app.test_client.get('/quirky/non-existing')
assert response.status == 404
def test_dynamic_route_path(): def test_dynamic_route_path():
app = Sanic('test_dynamic_route_path') app = Sanic('test_dynamic_route_path')