mirror of
https://github.com/python/cpython.git
synced 2024-11-25 02:44:06 +08:00
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.
This commit is contained in:
parent
cb15988114
commit
42ef4b1f4c
@ -156,6 +156,18 @@ class OtherNetworkTests(unittest.TestCase):
|
|||||||
self.assertEqual(res.geturl(),
|
self.assertEqual(res.geturl(),
|
||||||
"http://docs.python.org/glossary.html")
|
"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):
|
def _test_urls(self, urls, handlers, retry=True):
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
|
@ -1063,8 +1063,10 @@ class AbstractHTTPHandler(BaseHandler):
|
|||||||
raise URLError('no host given')
|
raise URLError('no host given')
|
||||||
|
|
||||||
h = http_class(host, timeout=req.timeout) # will parse host:port
|
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
|
# TODO(jhylton): Should this be redesigned to handle
|
||||||
# persistent connections?
|
# persistent connections?
|
||||||
|
Loading…
Reference in New Issue
Block a user