Merge pull request #632 from messense/feature/path-route
Add path type for router
This commit is contained in:
commit
ff17fc95e6
|
@ -288,7 +288,7 @@ class Sanic:
|
||||||
attach_to=middleware_or_request)
|
attach_to=middleware_or_request)
|
||||||
|
|
||||||
# Static Files
|
# Static Files
|
||||||
def static(self, uri, file_or_directory, pattern='.+',
|
def static(self, uri, file_or_directory, pattern=r'/?.+',
|
||||||
use_modified_since=True, use_content_range=False):
|
use_modified_since=True, use_content_range=False):
|
||||||
"""Register a root to serve files from. The input can either be a
|
"""Register a root to serve files from. The input can either be a
|
||||||
file or a directory. See
|
file or a directory. See
|
||||||
|
|
|
@ -16,6 +16,7 @@ REGEX_TYPES = {
|
||||||
'int': (int, r'\d+'),
|
'int': (int, r'\d+'),
|
||||||
'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'[^/].*?'),
|
||||||
}
|
}
|
||||||
|
|
||||||
ROUTER_CACHE_SIZE = 1024
|
ROUTER_CACHE_SIZE = 1024
|
||||||
|
@ -71,7 +72,8 @@ class Router:
|
||||||
self.routes_always_check = []
|
self.routes_always_check = []
|
||||||
self.hosts = set()
|
self.hosts = set()
|
||||||
|
|
||||||
def parse_parameter_string(self, parameter_string):
|
@classmethod
|
||||||
|
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
|
||||||
|
|
||||||
|
@ -161,10 +163,10 @@ class Router:
|
||||||
parameters.append(parameter)
|
parameters.append(parameter)
|
||||||
|
|
||||||
# Mark the whole route as unhashable if it has the hash key in it
|
# Mark the whole route as unhashable if it has the hash key in it
|
||||||
if re.search('(^|[^^]){1}/', pattern):
|
if re.search(r'(^|[^^]){1}/', pattern):
|
||||||
properties['unhashable'] = True
|
properties['unhashable'] = True
|
||||||
# Mark the route as unhashable if it matches the hash key
|
# Mark the route as unhashable if it matches the hash key
|
||||||
elif re.search(pattern, '/'):
|
elif re.search(r'/', pattern):
|
||||||
properties['unhashable'] = True
|
properties['unhashable'] = True
|
||||||
|
|
||||||
return '({})'.format(pattern)
|
return '({})'.format(pattern)
|
||||||
|
|
|
@ -238,6 +238,30 @@ def test_dynamic_route_regex():
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
|
|
||||||
|
|
||||||
|
def test_dynamic_route_path():
|
||||||
|
app = Sanic('test_dynamic_route_path')
|
||||||
|
|
||||||
|
@app.route('/<path:path>/info')
|
||||||
|
async def handler(request, path):
|
||||||
|
return text('OK')
|
||||||
|
|
||||||
|
request, response = app.test_client.get('/path/1/info')
|
||||||
|
assert response.status == 200
|
||||||
|
|
||||||
|
request, response = app.test_client.get('/info')
|
||||||
|
assert response.status == 404
|
||||||
|
|
||||||
|
@app.route('/<path:path>')
|
||||||
|
async def handler1(request, path):
|
||||||
|
return text('OK')
|
||||||
|
|
||||||
|
request, response = app.test_client.get('/info')
|
||||||
|
assert response.status == 200
|
||||||
|
|
||||||
|
request, response = app.test_client.get('/whatever/you/set')
|
||||||
|
assert response.status == 200
|
||||||
|
|
||||||
|
|
||||||
def test_dynamic_route_unhashable():
|
def test_dynamic_route_unhashable():
|
||||||
app = Sanic('test_dynamic_route_unhashable')
|
app = Sanic('test_dynamic_route_unhashable')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user