From d63cb6a7cb80758dad7f99688824ea8768f650db Mon Sep 17 00:00:00 2001 From: zjjott Date: Wed, 4 May 2016 17:52:33 +0800 Subject: [PATCH] httputil: add __str__ method to HTTPHeader --- tornado/httputil.py | 8 ++++++++ tornado/test/httputil_test.py | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/tornado/httputil.py b/tornado/httputil.py index 866681adf..cb5094b37 100644 --- a/tornado/httputil.py +++ b/tornado/httputil.py @@ -234,6 +234,14 @@ class HTTPHeaders(collections.MutableMapping): # the appearance that HTTPHeaders is a single container. __copy__ = copy + def __str__(self): + lines = [] + for name, value in self.get_all(): + lines.append("%s: %s" % (name, value)) + return "\n%s\n" % ("\n".join(lines)) + + __unicode__ = __str__ + class HTTPServerRequest(object): """A single HTTP request. diff --git a/tornado/test/httputil_test.py b/tornado/test/httputil_test.py index 75b3dfa22..62b8c6d76 100644 --- a/tornado/test/httputil_test.py +++ b/tornado/test/httputil_test.py @@ -318,6 +318,14 @@ Foo: even self.assertEqual(headers['quux'], 'xyzzy') self.assertEqual(sorted(headers.get_all()), [('Foo', 'bar'), ('Quux', 'xyzzy')]) + def test_string(self): + headers = HTTPHeaders() + headers.add("Foo", "1") + headers.add("Foo", "2") + headers.add("Foo", "3") + headers2 = HTTPHeaders.parse(str(headers)) + self.assertEquals(headers, headers2) + class FormatTimestampTest(unittest.TestCase): # Make sure that all the input types are supported. -- 2.47.2