This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch db/redir_slash_head in repository https://gitbox.apache.org/repos/asf/allura.git
commit 86293bf8c763feda78c08da94444f21e2e5a063c Author: Dave Brondsema <[email protected]> AuthorDate: Mon May 18 11:34:44 2026 -0400 make with_trailing_slash and without_trailing_slash apply to HEAD requests too --- Allura/allura/lib/patches.py | 8 ++++---- Allura/allura/tests/test_patches.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Allura/allura/lib/patches.py b/Allura/allura/lib/patches.py index 42d9afae5..27732184f 100644 --- a/Allura/allura/lib/patches.py +++ b/Allura/allura/lib/patches.py @@ -71,10 +71,10 @@ def override_template(controller, template): @h.monkeypatch(tg, tg.decorators) @decorator def without_trailing_slash(func, *args, **kwargs): - '''Monkey-patched to use 301 redirects for SEO, and handle query strings''' + '''Monkey-patched to use 301 redirects for SEO, and handle query strings, and HEAD''' __traceback_hide__ = 'before_and_this' # for paste/werkzeug shorter traces response_type = getattr(request, 'response_type', None) - if (request.method == 'GET' and request.path.endswith('/') and not response_type): + if (request.method in ('GET', 'HEAD') and request.path.endswith('/') and not response_type): location = request.path_url[:-1] if request.query_string: location += '?' + request.query_string @@ -84,10 +84,10 @@ def without_trailing_slash(func, *args, **kwargs): @h.monkeypatch(tg, tg.decorators) @decorator def with_trailing_slash(func, *args, **kwargs): - '''Monkey-patched to use 301 redirects for SEO, and handle query strings''' + '''Monkey-patched to use 301 redirects for SEO, and handle query strings, and HEAD''' __traceback_hide__ = 'before_and_this' # for paste/werkzeug shorter traces response_type = getattr(request, 'response_type', None) - if (request.method == 'GET' and not request.path.endswith('/') and not response_type): + if (request.method in ('GET', 'HEAD') and not request.path.endswith('/') and not response_type): location = request.path_url + '/' if request.query_string: location += '?' + request.query_string diff --git a/Allura/allura/tests/test_patches.py b/Allura/allura/tests/test_patches.py index b66957c6f..281afada2 100644 --- a/Allura/allura/tests/test_patches.py +++ b/Allura/allura/tests/test_patches.py @@ -49,6 +49,13 @@ def test_with_trailing_slash_qs(): tg.decorators.with_trailing_slash(empty_func)() assert raised.value.location == 'http://localhost/foo/bar/?foo=bar&baz=bam' [email protected](patches, 'request', webob.Request.blank('/foo/bar', method='HEAD')) +def test_with_trailing_head(): + patches.apply() + with pytest.raises(webob.exc.HTTPMovedPermanently) as raised: + tg.decorators.with_trailing_slash(empty_func)() + assert raised.value.location == 'http://localhost/foo/bar/' + @patch.object(patches, 'request', webob.Request.blank('/foo/bar/')) def test_without_trailing_slash(): @@ -71,3 +78,10 @@ def test_without_trailing_slash_qs(): with pytest.raises(webob.exc.HTTPMovedPermanently) as raised: tg.decorators.without_trailing_slash(empty_func)() assert raised.value.location == 'http://localhost/foo/bar?foo=bar&baz=bam' + [email protected](patches, 'request', webob.Request.blank('/foo/bar/', method='HEAD')) +def test_without_trailing_slash_head(): + patches.apply() + with pytest.raises(webob.exc.HTTPMovedPermanently) as raised: + tg.decorators.without_trailing_slash(empty_func)() + assert raised.value.location == 'http://localhost/foo/bar'
