From c69dbed7eed3d50f35cd59b05f174b81c802b20d Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Thu, 19 Feb 2015 19:58:03 -0600 Subject: [PATCH] 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. --- src/milla/dispatch/routing.py | 7 ++++--- src/milla/tests/test_routing.py | 8 ++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/milla/dispatch/routing.py b/src/milla/dispatch/routing.py index 239ead7..433e798 100644 --- a/src/milla/dispatch/routing.py +++ b/src/milla/dispatch/routing.py @@ -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 diff --git a/src/milla/tests/test_routing.py b/src/milla/tests/test_routing.py index 0f46590..01a6ba4 100644 --- a/src/milla/tests/test_routing.py +++ b/src/milla/tests/test_routing.py @@ -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():