From 8c75ef19dce2c3119b4ffb1d172d81065aeac4ed Mon Sep 17 00:00:00 2001 From: Brett Randall Date: Thu, 5 Jul 2018 13:21:02 +1000 Subject: [PATCH] /redirect-to POST, added support for url/status_code in form-data. #476. url (required) and status_code can still appear in the query-string for GET or POST, but for POST/PATCH/PUT if they appear in the body form-data then the values from there are used in-favour of any in the query-string. Added tests. Signed-off-by: Brett Randall --- httpbin/core.py | 2 ++ test_httpbin.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/httpbin/core.py b/httpbin/core.py index 305c9882..05f750db 100644 --- a/httpbin/core.py +++ b/httpbin/core.py @@ -629,6 +629,8 @@ def redirect_to(): args_dict = request.args.items() args = CaseInsensitiveDict(args_dict) + if request.method in ('POST', 'PATCH', 'PUT') and request.form: + args.update(request.form.to_dict(flat=True)) # We need to build the response manually and convert to UTF-8 to prevent # werkzeug from "fixing" the URL. This endpoint should set the Location diff --git a/test_httpbin.py b/test_httpbin.py index b7104ffc..686f01b9 100755 --- a/test_httpbin.py +++ b/test_httpbin.py @@ -598,6 +598,26 @@ def test_redirect_to_post(self): response.headers.get('Location'), '/post' ) + def test_redirect_to_post_with_form_data(self): + """url and status_code parameters can appear as form data """ + response = self.app.post('/redirect-to', + data='url=/get&status_code=302', + content_type='application/x-www-form-urlencoded') + self.assertEqual(response.status_code, 302) + self.assertEqual( + response.headers.get('Location'), '/get' + ) + + def test_redirect_to_post_with_overriding_form_data(self): + """form data parameters override query string""" + response = self.app.post('/redirect-to?url=/post&status_code=307', + data='url=/get&status_code=302', + content_type='application/x-www-form-urlencoded') + self.assertEqual(response.status_code, 302) + self.assertEqual( + response.headers.get('Location'), '/get' + ) + def test_redirect_absolute_param_n_higher_than_1(self): response = self.app.get('/redirect/5?absolute=true') self.assertEqual(