From 770a8fb28889a4bdcffaed5b3e6902ebff800bf6 Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Mon, 9 Oct 2017 07:54:39 -0700 Subject: [PATCH] raise exception for invalid param syntax --- sanic/router.py | 2 ++ tests/test_routes.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/sanic/router.py b/sanic/router.py index f943bc19..c57d808f 100644 --- a/sanic/router.py +++ b/sanic/router.py @@ -93,6 +93,8 @@ class Router: pattern = 'string' if ':' in parameter_string: name, pattern = parameter_string.split(':', 1) + if not name: + raise ValueError("Invalid parameter syntax: {}".format(parameter_string)) default = (str, pattern) # Pull from pre-configured types diff --git a/tests/test_routes.py b/tests/test_routes.py index b7228d29..b4ed7cf3 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -71,6 +71,16 @@ def test_route_strict_slash(): request, response = app.test_client.post('/post') assert response.status == 404 +def test_route_invalid_parameter_syntax(): + with pytest.raises(ValueError): + app = Sanic('test_route_invalid_param_syntax') + + @app.get('/get/<:string>', strict_slashes=True) + def handler(request): + return text('OK') + + request, response = app.test_client.get('/get') + def test_route_strict_slash_default_value(): app = Sanic('test_route_strict_slash', strict_slashes=True)