Skip to content

Commit

Permalink
feat: RedirectHandler to pass GET query
Browse files Browse the repository at this point in the history
  • Loading branch information
ylt6 committed Jul 16, 2017
1 parent d0fd797 commit e5feb27
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
8 changes: 8 additions & 0 deletions tornado/httputil.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,14 @@ def split_host_and_port(netloc):
return (host, port)


def qs_to_qsl(qs):
"""Generator rewinding a result of ``parse_qs`` back to name-value pairs.
"""
for k, vs in qs.items():
for v in vs:
yield (k, v)


_OctalPatt = re.compile(r"\\[0-3][0-7][0-7]")
_QuotePatt = re.compile(r"[\\].")
_nulljoin = ''.join
Expand Down
18 changes: 17 additions & 1 deletion tornado/test/httputil_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


from __future__ import absolute_import, division, print_function
from tornado.httputil import url_concat, parse_multipart_form_data, HTTPHeaders, format_timestamp, HTTPServerRequest, parse_request_start_line, parse_cookie
from tornado.httputil import url_concat, parse_multipart_form_data, HTTPHeaders, format_timestamp, HTTPServerRequest, parse_request_start_line, parse_cookie, qs_to_qsl, PY3
from tornado.escape import utf8, native_str
from tornado.log import gen_log
from tornado.testing import ExpectLog
Expand All @@ -15,6 +15,11 @@
import pickle
import time

if PY3:
import urllib.parse as urllib_parse
else:
import urlparse as urllib_parse


class TestUrlConcat(unittest.TestCase):
def test_url_concat_no_query_params(self):
Expand Down Expand Up @@ -102,6 +107,17 @@ def test_url_concat_dict_params(self):
self.assertEqual(url, "https://localhost/path?y=y")


class QsParseTest(unittest.TestCase):

def test_parsing(self):
qsstring = "a=1&b=2&a=3"
qs = urllib_parse.parse_qs(qsstring)
qsl = list(qs_to_qsl(qs))
self.assertIn(('a', '1'), qsl)
self.assertIn(('a', '3'), qsl)
self.assertIn(('b', '2'), qsl)


class MultipartFormDataTest(unittest.TestCase):
def test_file_upload(self):
data = b"""\
Expand Down
11 changes: 11 additions & 0 deletions tornado/test/web_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2880,13 +2880,24 @@ class RedirectHandlerTest(WebTestCase):
def get_handlers(self):
return [
('/src', WebRedirectHandler, {'url': '/dst'}),
('/src2', WebRedirectHandler, {'url': '/dst2?foo=bar'}),
(r'/(.*?)/(.*?)/(.*)', WebRedirectHandler, {'url': '/{1}/{0}/{2}'})]

def test_basic_redirect(self):
response = self.fetch('/src', follow_redirects=False)
self.assertEqual(response.code, 301)
self.assertEqual(response.headers['Location'], '/dst')

def test_redirect_with_argument(self):
response = self.fetch('/src?foo=bar', follow_redirects=False)
self.assertEqual(response.code, 301)
self.assertEqual(response.headers['Location'], '/dst?foo=bar')

def test_redirect_with_appending_argument(self):
response = self.fetch('/src2?foo2=bar2', follow_redirects=False)
self.assertEqual(response.code, 301)
self.assertEqual(response.headers['Location'], '/dst2?foo=bar&foo2=bar2')

def test_redirect_pattern(self):
response = self.fetch('/a/b/c', follow_redirects=False)
self.assertEqual(response.code, 301)
Expand Down
6 changes: 5 additions & 1 deletion tornado/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -2279,7 +2279,11 @@ def initialize(self, url, permanent=True):
self._permanent = permanent

def get(self, *args):
self.redirect(self._url.format(*args), permanent=self._permanent)
to_url = self._url.format(*args)
if self.request.query_arguments:
to_url = httputil.url_concat(
to_url, list(httputil.qs_to_qsl(self.request.query_arguments)))
self.redirect(to_url, permanent=self._permanent)


class StaticFileHandler(RequestHandler):
Expand Down

0 comments on commit e5feb27

Please sign in to comment.