summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSenthil Kumaran <orsenthil@gmail.com>2010-08-02 12:01:21 (GMT)
committerSenthil Kumaran <orsenthil@gmail.com>2010-08-02 12:01:21 (GMT)
commit3357840bafd2b118d19d0319cd9183a11cb4cd6b (patch)
tree7262ae7b01a6fff1a522df55babdfd1dbba9035e
parentfc499bcfd3c0d974ecbf9df2dd5017ed81bfae0b (diff)
downloadcpython-3357840bafd2b118d19d0319cd9183a11cb4cd6b.zip
cpython-3357840bafd2b118d19d0319cd9183a11cb4cd6b.tar.gz
cpython-3357840bafd2b118d19d0319cd9183a11cb4cd6b.tar.bz2
Merged revisions 83521 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r83521 | senthil.kumaran | 2010-08-02 16:34:58 +0530 (Mon, 02 Aug 2010) | 3 lines Fix Issue8572 - httplib getheader() throws error instead of default ........
-rw-r--r--Doc/library/http.client.rst3
-rw-r--r--Lib/http/client.py6
-rw-r--r--Lib/test/test_httplib.py38
3 files changed, 44 insertions, 3 deletions
diff --git a/Doc/library/http.client.rst b/Doc/library/http.client.rst
index 737ba15..3a44788 100644
--- a/Doc/library/http.client.rst
+++ b/Doc/library/http.client.rst
@@ -449,7 +449,8 @@ statement.
.. method:: HTTPResponse.getheader(name, default=None)
Get the contents of the header *name*, or *default* if there is no matching
- header.
+ header. If *default* is an iterator other than a string, then the return
+ value will be a string consisting of items of the iterator joined by comma.
.. method:: HTTPResponse.getheaders()
diff --git a/Lib/http/client.py b/Lib/http/client.py
index 612fa37..d490b3d 100644
--- a/Lib/http/client.py
+++ b/Lib/http/client.py
@@ -602,7 +602,11 @@ class HTTPResponse(io.RawIOBase):
def getheader(self, name, default=None):
if self.headers is None:
raise ResponseNotReady()
- return ', '.join(self.headers.get_all(name, default))
+ headers = self.headers.get_all(name) or default
+ if isinstance(headers, str) or not hasattr(headers, '__iter__'):
+ return headers
+ else:
+ return ', '.join(headers)
def getheaders(self):
"""Return list of (header, value) tuples."""
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
index f615c5d..831d5ae 100644
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -405,9 +405,45 @@ class RequestBodyTest(TestCase):
self.assertEqual("5", message.get("content-length"))
self.assertEqual(b'body\xc1', f.read())
+
+class HTTPResponseTest(TestCase):
+
+ def setUp(self):
+ body = "HTTP/1.1 200 Ok\r\nMy-Header: first-value\r\nMy-Header: \
+ second-value\r\n\r\nText"
+ sock = FakeSocket(body)
+ self.resp = client.HTTPResponse(sock)
+ self.resp.begin()
+
+ def test_getting_header(self):
+ header = self.resp.getheader('My-Header')
+ self.assertEqual(header, 'first-value, second-value')
+
+ header = self.resp.getheader('My-Header', 'some default')
+ self.assertEqual(header, 'first-value, second-value')
+
+ def test_getting_nonexistent_header_with_string_default(self):
+ header = self.resp.getheader('No-Such-Header', 'default-value')
+ self.assertEqual(header, 'default-value')
+
+ def test_getting_nonexistent_header_with_iterable_default(self):
+ header = self.resp.getheader('No-Such-Header', ['default', 'values'])
+ self.assertEqual(header, 'default, values')
+
+ header = self.resp.getheader('No-Such-Header', ('default', 'values'))
+ self.assertEqual(header, 'default, values')
+
+ def test_getting_nonexistent_header_without_default(self):
+ header = self.resp.getheader('No-Such-Header')
+ self.assertEqual(header, None)
+
+ def test_getting_header_defaultint(self):
+ header = self.resp.getheader('No-Such-Header',default=42)
+ self.assertEqual(header, 42)
+
def test_main(verbose=None):
support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest,
- HTTPSTimeoutTest, RequestBodyTest)
+ HTTPSTimeoutTest, RequestBodyTest, HTTPResponseTest)
if __name__ == '__main__':
test_main()