Merge pull request #1091 from yunstanford/patch-router-fix

Patch router fix
This commit is contained in:
Raphael Deem 2018-01-17 00:15:33 -08:00 committed by GitHub
commit 226a73141b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 5 deletions

View File

@ -234,11 +234,11 @@ class Router:
if properties['unhashable']: if properties['unhashable']:
routes_to_check = self.routes_always_check routes_to_check = self.routes_always_check
ndx, route = self.check_dynamic_route_exists( ndx, route = self.check_dynamic_route_exists(
pattern, routes_to_check) pattern, routes_to_check, parameters)
else: else:
routes_to_check = self.routes_dynamic[url_hash(uri)] routes_to_check = self.routes_dynamic[url_hash(uri)]
ndx, route = self.check_dynamic_route_exists( ndx, route = self.check_dynamic_route_exists(
pattern, routes_to_check) pattern, routes_to_check, parameters)
if ndx != -1: if ndx != -1:
# Pop the ndx of the route, no dups of the same route # Pop the ndx of the route, no dups of the same route
routes_to_check.pop(ndx) routes_to_check.pop(ndx)
@ -285,9 +285,9 @@ class Router:
self.routes_static[uri] = route self.routes_static[uri] = route
@staticmethod @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): for ndx, route in enumerate(routes_to_check):
if route.pattern == pattern: if route.pattern == pattern and route.parameters == parameters:
return ndx, route return ndx, route
else: else:
return -1, None return -1, None

View File

@ -2,7 +2,7 @@ import asyncio
import pytest import pytest
from sanic import Sanic from sanic import Sanic
from sanic.response import text from sanic.response import text, json
from sanic.router import RouteExists, RouteDoesNotExist from sanic.router import RouteExists, RouteDoesNotExist
from sanic.constants import HTTP_METHODS from sanic.constants import HTTP_METHODS
@ -907,3 +907,27 @@ def test_unicode_routes():
request, response = app.test_client.get('/overload/你好') request, response = app.test_client.get('/overload/你好')
assert response.text == 'OK2 你好' assert response.text == 'OK2 你好'
def test_uri_with_different_method_and_different_params():
app = Sanic('test_uri')
@app.route('/ads/<ad_id>', methods=['GET'])
async def ad_get(request, ad_id):
return json({'ad_id': ad_id})
@app.route('/ads/<action>', 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': '1234'
}
request, response = app.test_client.post('/ads/post')
assert response.status == 200
assert response.json == {
'action': 'post'
}