]> granicus.if.org Git - python/commitdiff
Merged revisions 85025 via svnmerge from
authorSenthil Kumaran <orsenthil@gmail.com>
Mon, 27 Sep 2010 01:28:10 +0000 (01:28 +0000)
committerSenthil Kumaran <orsenthil@gmail.com>
Mon, 27 Sep 2010 01:28:10 +0000 (01:28 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r85025 | senthil.kumaran | 2010-09-27 06:56:03 +0530 (Mon, 27 Sep 2010) | 6 lines

  Fix Issue1595365 - Adding the req.headers after the un-redirect headers have
  been added. This helps in accidental overwritting of User-Agent header to
  default value. To preserve the old behavior, only headers not in unredirected
  headers will be updated.
........

Lib/test/test_urllib2net.py
Lib/urllib/request.py

index eab9306ab96c29292607463e1b1404daf7f9a23a..ccfbdb2bee2d0f53dcc8d2cd70c2be05d0fc929f 100644 (file)
@@ -159,6 +159,18 @@ class OtherNetworkTests(unittest.TestCase):
         self.assertEqual(res.geturl(),
                 "http://docs.python.org/glossary.html")
 
+    def test_custom_headers(self):
+        url = "http://www.example.com"
+        opener = urllib.request.build_opener()
+        request = urllib.request.Request(url)
+        self.assertFalse(request.header_items())
+        opener.open(request)
+        self.assertTrue(request.header_items())
+        self.assertTrue(request.has_header('User-agent'))
+        request.add_header('User-Agent','Test-Agent')
+        opener.open(request)
+        self.assertEqual(request.get_header('User-agent'),'Test-Agent')
+
     def _test_urls(self, urls, handlers, retry=True):
         import socket
         import time
index c638cfa510639a6528030493f932cda72eeaef5a..464f84709bfef91c5f1898ba375208f1c2e6cc22 100644 (file)
@@ -1063,8 +1063,10 @@ class AbstractHTTPHandler(BaseHandler):
             raise URLError('no host given')
 
         h = http_class(host, timeout=req.timeout) # will parse host:port
-        headers = dict(req.headers)
-        headers.update(req.unredirected_hdrs)
+
+        headers = dict(req.unredirected_hdrs)
+        headers.update(dict((k, v) for k, v in req.headers.items()
+                            if k not in headers))
 
         # TODO(jhylton): Should this be redesigned to handle
         # persistent connections?