Add support of custom routers rules
This commit is contained in:
parent
05bb812e2b
commit
51aec250b0
|
@ -65,7 +65,7 @@ class Router:
|
||||||
routes_always_check = None
|
routes_always_check = None
|
||||||
parameter_pattern = re.compile(r'<(.+?)>')
|
parameter_pattern = re.compile(r'<(.+?)>')
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, converters=None):
|
||||||
self.routes_all = {}
|
self.routes_all = {}
|
||||||
self.routes_names = {}
|
self.routes_names = {}
|
||||||
self.routes_static_files = {}
|
self.routes_static_files = {}
|
||||||
|
@ -73,9 +73,11 @@ class Router:
|
||||||
self.routes_dynamic = defaultdict(list)
|
self.routes_dynamic = defaultdict(list)
|
||||||
self.routes_always_check = []
|
self.routes_always_check = []
|
||||||
self.hosts = set()
|
self.hosts = set()
|
||||||
|
self._converters = REGEX_TYPES
|
||||||
|
if converters:
|
||||||
|
self._converters.update(converters)
|
||||||
|
|
||||||
@classmethod
|
def parse_parameter_string(self, parameter_string):
|
||||||
def parse_parameter_string(cls, parameter_string):
|
|
||||||
"""Parse a parameter string into its constituent name, type, and
|
"""Parse a parameter string into its constituent name, type, and
|
||||||
pattern
|
pattern
|
||||||
|
|
||||||
|
@ -100,7 +102,7 @@ class Router:
|
||||||
|
|
||||||
default = (str, pattern)
|
default = (str, pattern)
|
||||||
# Pull from pre-configured types
|
# Pull from pre-configured types
|
||||||
_type, pattern = REGEX_TYPES.get(pattern, default)
|
_type, pattern = self._converters.get(pattern, default)
|
||||||
|
|
||||||
return name, _type, pattern
|
return name, _type, pattern
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ import pytest
|
||||||
|
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
from sanic.response import text
|
from sanic.response import text
|
||||||
from sanic.router import RouteExists, RouteDoesNotExist
|
from sanic.router import RouteExists, RouteDoesNotExist, Router
|
||||||
from sanic.constants import HTTP_METHODS
|
from sanic.constants import HTTP_METHODS
|
||||||
|
|
||||||
|
|
||||||
|
@ -850,3 +850,23 @@ def test_unmergeable_overload_routes():
|
||||||
|
|
||||||
request, response = app.test_client.post('/overload_part')
|
request, response = app.test_client.post('/overload_part')
|
||||||
assert response.status == 405
|
assert response.status == 405
|
||||||
|
|
||||||
|
|
||||||
|
def test_custom_route_converters():
|
||||||
|
regex = 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}'
|
||||||
|
custom_router = Router(converters={
|
||||||
|
"uuid": (uuid.UUID, regex)
|
||||||
|
})
|
||||||
|
app = Sanic('test_dynamic_route', router=custom_router)
|
||||||
|
results = []
|
||||||
|
|
||||||
|
@app.route('/<id:uuid>', methods=None)
|
||||||
|
async def handler(request, id):
|
||||||
|
results.append(id)
|
||||||
|
return text('OK')
|
||||||
|
|
||||||
|
request, response = app.test_client.get('/folder/test123')
|
||||||
|
|
||||||
|
assert response.text == 'OK'
|
||||||
|
assert isinstance(results[0], uuid.UUID)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user