From 5ff481952db53ac3c9e7695d6146aa6ec0e937b8 Mon Sep 17 00:00:00 2001 From: Volodymyr Maksymiv Date: Sat, 9 Jun 2018 11:16:17 +0300 Subject: [PATCH] add UUID support (#1241) --- sanic/router.py | 3 +++ tests/test_routes.py | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/sanic/router.py b/sanic/router.py index 8b3fd3dc..2864bf84 100644 --- a/sanic/router.py +++ b/sanic/router.py @@ -1,4 +1,5 @@ import re +import uuid from collections import defaultdict, namedtuple from collections.abc import Iterable from functools import lru_cache @@ -18,6 +19,8 @@ REGEX_TYPES = { 'number': (float, r'[0-9\\.]+'), 'alpha': (str, r'[A-Za-z]+'), '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 diff --git a/tests/test_routes.py b/tests/test_routes.py index ed35ea48..146db97c 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -422,6 +422,28 @@ def test_dynamic_route_regex(): assert response.status == 200 +def test_dynamic_route_uuid(): + import uuid + app = Sanic('test_dynamic_route_uuid') + + results = [] + + @app.route('/quirky/') + 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(): app = Sanic('test_dynamic_route_path')