summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Bukachi <michaelbukachi@gmail.com>2020-07-08 00:09:57 +0300
committerDavid Lord <davidism@gmail.com>2020-10-15 14:17:34 -0700
commitd34e5c47514473f9164c3a27104a8f31092cdc42 (patch)
tree9ad2c7d9b71517bdc336ea6adca2c654db4e113e
parent543d1340706741a6107535f93d734395caba7082 (diff)
downloadwerkzeug-d34e5c47514473f9164c3a27104a8f31092cdc42.tar.gz
track redirects in test response
-rw-r--r--CHANGES.rst2
-rw-r--r--src/werkzeug/test.py32
-rw-r--r--tests/test_test.py6
3 files changed, 39 insertions, 1 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 5c55b40d..05ad09be 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -65,6 +65,8 @@ Unreleased
attributes modern browsers expect. :pr:`1889`
- Use ``request.headers`` instead of ``request.environ`` to look up
header attributes. :pr:`1808`
+- Responses from the test client have a ``redirect_chain`` attribute
+ to track intermediate redirect responses. :issue:`763`
Version 1.0.2
diff --git a/src/werkzeug/test.py b/src/werkzeug/test.py
index 8fb97c84..86f694c6 100644
--- a/src/werkzeug/test.py
+++ b/src/werkzeug/test.py
@@ -45,6 +45,7 @@ from .urls import url_unparse
from .urls import url_unquote
from .utils import get_content_type
from .wrappers import BaseRequest
+from .wrappers.response import Response
from .wsgi import ClosingIterator
from .wsgi import get_current_url
from werkzeug.debug import DebuggedApplication
@@ -1049,9 +1050,17 @@ class Client:
)
if self.response_wrapper is not None:
- response = self.response_wrapper(*response) # type: ignore
+
+ class UserTestResponse(TestResponse, self.response_wrapper):
+ pass
+
+ response = UserTestResponse(*response, redirect_chain=redirect_chain)
+ else:
+ response = TestResponse(*response, redirect_chain=redirect_chain)
+
if as_tuple:
return environ, response
+
return response
def get(
@@ -1184,3 +1193,24 @@ def run_wsgi_app(
app_iter = ClosingIterator(app_iter, close_func)
return app_iter, response[0], Headers(response[1])
+
+
+class TestResponse(Response):
+ """This class extends the Response class, allowing you to track redirects in a list.
+ """
+
+ def __init__(self, *args, **kwargs):
+ self.redirect_chain = kwargs.pop("redirect_chain", [])
+ super().__init__(*args, *kwargs)
+ self.args = args
+
+ #
+ def __iter__(self):
+ return iter(self.args)
+
+ #
+ def __getitem__(self, item):
+ try:
+ return self.args.__getitem__(item)
+ except TypeError:
+ return super().__getitem__(item)
diff --git a/tests/test_test.py b/tests/test_test.py
index 7052f346..13befb6c 100644
--- a/tests/test_test.py
+++ b/tests/test_test.py
@@ -517,6 +517,12 @@ def test_follow_redirect_exhaust_intermediate():
assert not app.active
+def test_redirects_are_tracked():
+ c = Client(redirect_with_get_app, response_wrapper=BaseResponse)
+ resp = c.get("/first/request", follow_redirects=True)
+ assert resp.redirect_chain == [("http://localhost/some/redirect/", 302)]
+
+
def test_cookie_across_redirect():
@Request.application
def app(request):