From b6cf4cac14ddceadc82e9aa0b64d369715c70e9f Mon Sep 17 00:00:00 2001 From: Chris Campbell Date: Sat, 6 Dec 2014 10:46:50 +1100 Subject: [PATCH] Unquote response location in assertRedirect By default Flask url encodes query params which makes testing the location of a redirect slightly awkward. This fixes the problem by unquoting the url making for simpler assertions. eg. `self.assertRedirects(res, '/login?next=/account')` --- flask_testing/utils.py | 3 ++- tests/flask_app/__init__.py | 4 ++++ tests/test_utils.py | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/flask_testing/utils.py b/flask_testing/utils.py index 8ab93f1..ac4374a 100644 --- a/flask_testing/utils.py +++ b/flask_testing/utils.py @@ -22,6 +22,7 @@ from urllib.request import urlopen import multiprocessing +from urllib import unquote from werkzeug import cached_property # Use Flask's preferred JSON module so that our runtime behavior matches. @@ -215,7 +216,7 @@ def assertRedirects(self, response, location): :param location: relative URL (i.e. without **http://localhost**) """ self.assertTrue(response.status_code in (301, 302)) - self.assertEqual(response.location, "http://localhost" + location) + self.assertEqual(unquote(response.location), "http://localhost" + location) assert_redirects = assertRedirects diff --git a/tests/flask_app/__init__.py b/tests/flask_app/__init__.py index d270cfe..677d4b5 100644 --- a/tests/flask_app/__init__.py +++ b/tests/flask_app/__init__.py @@ -22,6 +22,10 @@ def bad_url(): def redirect_to_index(): return redirect(url_for("index")) + @app.route("/redirect-next/") + def redirect_from_somewhere(): + return redirect(url_for("index", next='/somewhere/')) + @app.route("/ajax/") def ajax(): return jsonify(name="test") diff --git a/tests/test_utils.py b/tests/test_utils.py index 1b81c80..091b1c9 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -89,6 +89,10 @@ def test_assert_redirects(self): response = self.client.get("/redirect/") self.assertRedirects(response, "/") + def test_assert_redirects_with_query(self): + response = self.client.get("/redirect-next/") + self.assertRedirects(response, "/?next=/somewhere/") + def test_assert_template_used(self): try: self.client.get("/template/")