From aea465074e34f5ece32027a19c4a95bf6d482fbd Mon Sep 17 00:00:00 2001 From: tzickel Date: Wed, 10 Aug 2016 14:20:18 +0300 Subject: [PATCH] Added supports for non-get redirect-to with other status_code (#284) --- httpbin/core.py | 8 ++++++-- test_httpbin.py | 9 +++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/httpbin/core.py b/httpbin/core.py index 9438cda..a5571fb 100644 --- a/httpbin/core.py +++ b/httpbin/core.py @@ -224,9 +224,9 @@ def _redirect(kind, n, external): return redirect(url_for('{0}_redirect_n_times'.format(kind), n=n - 1, _external=external)) -@app.route('/redirect-to') +@app.route('/redirect-to', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'TRACE']) def redirect_to(): - """302 Redirects to the given URL.""" + """302/3XX Redirects to the given URL.""" args = CaseInsensitiveDict(request.args.items()) @@ -235,6 +235,10 @@ def redirect_to(): # header to the exact string supplied. response = app.make_response('') response.status_code = 302 + if 'status_code' in args: + status_code = int(args['status_code']) + if status_code >= 300 and status_code < 400: + response.status_code = status_code response.headers['Location'] = args['url'].encode('utf-8') return response diff --git a/test_httpbin.py b/test_httpbin.py index 9893308..8457974 100755 --- a/test_httpbin.py +++ b/test_httpbin.py @@ -316,6 +316,15 @@ class HttpbinTestCase(unittest.TestCase): response.headers.get('Location'), '/relative-redirect/4' ) + def test_redirect_to_post(self): + response = self.app.post('/redirect-to?url=/post&status_code=307', + data=b'\x01\x02\x03\x81\x82\x83', + content_type='application/octet-stream') + self.assertEqual(response.status_code, 307) + self.assertEqual( + response.headers.get('Location'), '/post' + ) + def test_redirect_absolute_param_n_higher_than_1(self): response = self.app.get('/redirect/5?absolute=true') self.assertEqual(