]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
httputil: add __str__ method to HTTPHeader
authorzjjott <zjjott@gmail.com>
Wed, 4 May 2016 09:52:33 +0000 (17:52 +0800)
committerBen Darnell <ben@bendarnell.com>
Thu, 16 Jun 2016 01:43:24 +0000 (21:43 -0400)
tornado/httputil.py
tornado/test/httputil_test.py

index 866681adf531ab0c8bccda12760639476cb75d37..cb5094b37de9b70fb5e96aea6e76b7f054cb1521 100644 (file)
@@ -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.
index 75b3dfa224c262bcdca218c4ddaa489248cfbdc1..62b8c6d7655b744ecfdf0a0753c6694999eef3af 100644 (file)
@@ -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.