routing: Correctly redirect when path info is empty (fixes #7)

When the application path info is empty (e.g. the WSGI script is mounted
somewhere other than the root), appending a trailing slash and redirecting
causes the new location to be calculated incorrectly. This is because the part
of the request URL before the application path is not taken into account, so
the new path always ends up being a literal `/`. This commit changes the
behavior of the `redir` function that is returned by
`milla.dispatch.routing.Router.resolve` to calculate the new path info
correctly in the redirect response.
master
Dustin C. Hatch 2015-02-19 19:58:03 -06:00
parent cf94a4d600
commit c69dbed7ee
2 changed files with 10 additions and 5 deletions

View File

@ -1,4 +1,4 @@
# Copyright 2011 Dustin C. Hatch
# Copyright 2011, 2012, 2015 Dustin C. Hatch
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -121,8 +121,9 @@ class Router(object):
# Return a dummy function that just raises
# HTTPMovedPermanently to redirect the client to
# the canonical URL
def redir(*args, **kwargs):
raise milla.HTTPMovedPermanently(location=new_path_info)
def redir(request, *args, **kwargs):
raise milla.HTTPMovedPermanently(
location=request.create_href(new_path_info))
return redir
elif func and self.trailing_slash is Router.SILENT:
# Return the function found at the alternate path

View File

@ -113,7 +113,6 @@ def test_string_controller():
func = router.resolve('/test')
assert func.func == fake_controller
@nose.tools.raises(milla.HTTPMovedPermanently)
def test_trailing_slash_redir():
'''Paths that match except the trailing slash return a HTTP redirect
'''
@ -125,7 +124,12 @@ def test_trailing_slash_redir():
router.add_route('/test/', controller)
func = router.resolve('/test')
assert func is not controller
func()
try:
func(milla.Request.blank('/test'))
except milla.HTTPMovedPermanently as e:
assert e.location == '/test/'
else:
raise AssertionError('Redirect not raised')
@nose.tools.raises(milla.dispatch.routing.UnresolvedPath)
def test_trailing_slash_none():