Permalink
Browse files

Merge pull request #3328 from domenkozar/notfound-307-default

notfound view: use HTTPTemporaryRedirect as default
  • Loading branch information...
domenkozar committed Aug 23, 2018
2 parents 943fc92 + 93406d1 commit b5f4e4937ef43acae17c32444706aac758935aac
View
@@ -102,6 +102,11 @@ Backward Incompatibilities
longer override those set by the ``setup`` function.
See https://github.com/Pylons/pyramid/pull/3318
- ``pyramid.config.Configurator.add_notfound_view`` uses default redirect
class exception ``pyramid.httpexceptions.HTTPTemporaryRedirect`` instead
of previous ``pyramid.httpexceptions.HTTPFound``.
See https://github.com/Pylons/pyramid/pull/3328
Documentation Changes
---------------------
View
@@ -1631,19 +1631,24 @@ def notfound(request):
instead implements :class:`~pyramid.interfaces.IResponse`, the
append_slash logic will behave as if ``append_slash=True`` was passed,
but the provided class will be used as the response class instead of
the default :class:`~pyramid.httpexceptions.HTTPFound` response class
when a redirect is performed. For example:
the default :class:`~pyramid.httpexceptions.HTTPTemporaryRedirect`
response class when a redirect is performed. For example:
.. code-block:: python
from pyramid.httpexceptions import HTTPMovedPermanently
config.add_notfound_view(append_slash=HTTPMovedPermanently)
The above means that a redirect to a slash-appended route will be
attempted, but instead of :class:`~pyramid.httpexceptions.HTTPFound`
attempted, but instead of :class:`~pyramid.httpexceptions.HTTPTemporaryRedirect`
being used, :class:`~pyramid.httpexceptions.HTTPMovedPermanently will
be used` for the redirect response if a slash-appended route is found.
:class:`~pyramid.httpexceptions.HTTPTemporaryRedirect` class is used
as default response, which is equivalent to
:class:`~pyramid.httpexceptions.HTTPFound` with addition of redirecting
with the same HTTP method (useful when doing POST requests).
.. versionadded:: 1.3
.. versionchanged:: 1.6
@@ -1655,6 +1660,12 @@ def notfound(request):
.. versionchanged:: 1.8
The view is created using ``exception_only=True``.
.. versionchanged: 1.10
Default response was changed from :class:`~pyramid.httpexceptions.HTTPFound`
to :class:`~pyramid.httpexceptions.HTTPTemporaryRedirect`.
"""
for arg in (
'name', 'permission', 'context', 'for_', 'require_csrf',
@@ -2238,7 +2238,7 @@ def test_add_notfound_view_append_slash(self):
from pyramid.renderers import null_renderer
from zope.interface import implementedBy
from pyramid.interfaces import IRequest
from pyramid.httpexceptions import HTTPFound, HTTPNotFound
from pyramid.httpexceptions import HTTPTemporaryRedirect, HTTPNotFound
config = self._makeOne(autocommit=True)
config.add_route('foo', '/foo/')
def view(request): return Response('OK')
@@ -2251,7 +2251,7 @@ def view(request): return Response('OK')
exc_iface=implementedBy(HTTPNotFound),
request_iface=IRequest)
result = view(None, request)
self.assertTrue(isinstance(result, HTTPFound))
self.assertTrue(isinstance(result, HTTPTemporaryRedirect))
self.assertEqual(result.location, '/scriptname/foo/?a=1&b=2')
def test_add_notfound_view_append_slash_custom_response(self):
@@ -386,11 +386,11 @@ class TestNotFoundView(IntegrationBase, unittest.TestCase):
def test_it(self):
res = self.testapp.get('/wontbefound', status=200)
self.assertTrue(b'generic_notfound' in res.body)
res = self.testapp.get('/bar', status=302)
res = self.testapp.get('/bar', status=307)
self.assertEqual(res.location, 'http://localhost/bar/')
res = self.testapp.get('/bar/', status=200)
self.assertTrue(b'OK bar' in res.body)
res = self.testapp.get('/foo', status=302)
res = self.testapp.get('/foo', status=307)
self.assertEqual(res.location, 'http://localhost/foo/')
res = self.testapp.get('/foo/', status=200)
self.assertTrue(b'OK foo2' in res.body)
@@ -686,23 +686,23 @@ def test_matches(self):
context = ExceptionResponse()
self._registerMapper(request.registry, True)
response = self._callFUT(context, request)
self.assertEqual(response.status, '302 Found')
self.assertEqual(response.status, '307 Temporary Redirect')
self.assertEqual(response.location, '/abc/')
def test_matches_with_script_name(self):
request = self._makeRequest(PATH_INFO='/abc', SCRIPT_NAME='/foo')
context = ExceptionResponse()
self._registerMapper(request.registry, True)
response = self._callFUT(context, request)
self.assertEqual(response.status, '302 Found')
self.assertEqual(response.status, '307 Temporary Redirect')
self.assertEqual(response.location, '/foo/abc/')
def test_with_query_string(self):
request = self._makeRequest(PATH_INFO='/abc', QUERY_STRING='a=1&b=2')
context = ExceptionResponse()
self._registerMapper(request.registry, True)
response = self._callFUT(context, request)
self.assertEqual(response.status, '302 Found')
self.assertEqual(response.status, '307 Temporary Redirect')
self.assertEqual(response.location, '/abc/?a=1&b=2')
class TestAppendSlashNotFoundViewFactory(BaseTest, unittest.TestCase):
View
@@ -24,8 +24,8 @@
)
from pyramid.httpexceptions import (
HTTPFound,
HTTPNotFound,
HTTPTemporaryRedirect,
default_exceptionresponse_view,
)
@@ -292,7 +292,7 @@ def notfound_view(context, request): return HTTPNotFound('nope')
.. deprecated:: 1.3
"""
def __init__(self, notfound_view=None, redirect_class=HTTPFound):
def __init__(self, notfound_view=None, redirect_class=HTTPTemporaryRedirect):
if notfound_view is None:
notfound_view = default_exceptionresponse_view
self.notfound_view = notfound_view
@@ -377,8 +377,8 @@ def notfound(request):
instead implements :class:`~pyramid.interfaces.IResponse`, the
append_slash logic will behave as if ``append_slash=True`` was passed,
but the provided class will be used as the response class instead of
the default :class:`~pyramid.httpexceptions.HTTPFound` response class
when a redirect is performed. For example:
the default :class:`~pyramid.httpexceptions.HTTPTemporaryRedirect`
response class when a redirect is performed. For example:
.. code-block:: python
@@ -392,7 +392,7 @@ def aview(request):
return HTTPNotFound('not found')
The above means that a redirect to a slash-appended route will be
attempted, but instead of :class:`~pyramid.httpexceptions.HTTPFound`
attempted, but instead of :class:`~pyramid.httpexceptions.HTTPTemporaryRedirect`
being used, :class:`~pyramid.httpexceptions.HTTPMovedPermanently will
be used` for the redirect response if a slash-appended route is found.

0 comments on commit b5f4e49

Please sign in to comment.