Skip to content

Commit

Permalink
/redirect-to POST, added support for url/status_code in form-data. #476.
Browse files Browse the repository at this point in the history
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 <javabrett@gmail.com>
  • Loading branch information
javabrett committed Oct 25, 2018
1 parent 7b3b70d commit 8c75ef1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions httpbin/core.py
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions test_httpbin.py
Expand Up @@ -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(
Expand Down

0 comments on commit 8c75ef1

Please sign in to comment.