From 6d0b30953a079d45a7b35f1646e6e1181be0229e Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Mon, 15 Jan 2018 17:40:44 -0800 Subject: [PATCH 1/3] add unit test which should fail on original code --- tests/test_routes.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_routes.py b/tests/test_routes.py index 84d6b221..969128f8 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -907,3 +907,27 @@ def test_unicode_routes(): request, response = app.test_client.get('/overload/你好') assert response.text == 'OK2 你好' + + +def test_uri_with_different_method_and_different_params(): + app = Sanic('test_uri') + + @app.route('/ads/', methods=['GET']) + async def ad_get(request, ad_id): + return json({'ad_id': ad_id}) + + @app.route('/ads/', methods=['POST']) + async def ad_post(request, action): + return json({'action': action}) + + request, response = app.test_client.get('/ads/1234') + assert response.status == 200 + assert response.json == { + 'ad_id': '/ads/1234' + } + + request, response = app.test_client.post('/ads/post') + assert response.status == 200 + assert response.json == { + 'action': 'post' + } From d9002769cf26be9e36ffb788e2f4e71f03e1ca47 Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Mon, 15 Jan 2018 17:49:11 -0800 Subject: [PATCH 2/3] fix a typo --- tests/test_routes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_routes.py b/tests/test_routes.py index 969128f8..5f3b3376 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -2,7 +2,7 @@ import asyncio import pytest from sanic import Sanic -from sanic.response import text +from sanic.response import text, json from sanic.router import RouteExists, RouteDoesNotExist from sanic.constants import HTTP_METHODS @@ -923,7 +923,7 @@ def test_uri_with_different_method_and_different_params(): request, response = app.test_client.get('/ads/1234') assert response.status == 200 assert response.json == { - 'ad_id': '/ads/1234' + 'ad_id': '1234' } request, response = app.test_client.post('/ads/post') From 7daebc6aea2dad8484a3959494e830e0b2927b65 Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Mon, 15 Jan 2018 17:53:37 -0800 Subject: [PATCH 3/3] fix Router.check_dynamic_route_exists --- sanic/router.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sanic/router.py b/sanic/router.py index bd74c11d..052ff1bf 100644 --- a/sanic/router.py +++ b/sanic/router.py @@ -234,11 +234,11 @@ class Router: if properties['unhashable']: routes_to_check = self.routes_always_check ndx, route = self.check_dynamic_route_exists( - pattern, routes_to_check) + pattern, routes_to_check, parameters) else: routes_to_check = self.routes_dynamic[url_hash(uri)] ndx, route = self.check_dynamic_route_exists( - pattern, routes_to_check) + pattern, routes_to_check, parameters) if ndx != -1: # Pop the ndx of the route, no dups of the same route routes_to_check.pop(ndx) @@ -285,9 +285,9 @@ class Router: self.routes_static[uri] = route @staticmethod - def check_dynamic_route_exists(pattern, routes_to_check): + def check_dynamic_route_exists(pattern, routes_to_check, parameters): for ndx, route in enumerate(routes_to_check): - if route.pattern == pattern: + if route.pattern == pattern and route.parameters == parameters: return ndx, route else: return -1, None