From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Fri, 13 Sep 2019 14:25:51 +0000 (-0700) Subject: bpo-25068: urllib.request.ProxyHandler now lowercases the dict keys (GH-13489) X-Git-Tag: v3.8.0rc1~115 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=590ed09a5b422d59cc1f049c64ac30733545eef0;p=python bpo-25068: urllib.request.ProxyHandler now lowercases the dict keys (GH-13489) (cherry picked from commit b761e3aed1fbada4572a776f6a0d3c4be491d595) Co-authored-by: Zackery Spytz --- diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 810ee5e6ea..e7e80bc5da 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -1340,21 +1340,22 @@ class HandlerTests(unittest.TestCase): self.assertTrue(request.startswith(expected), repr(request)) def test_proxy(self): - o = OpenerDirector() - ph = urllib.request.ProxyHandler(dict(http="proxy.example.com:3128")) - o.add_handler(ph) - meth_spec = [ - [("http_open", "return response")] - ] - handlers = add_ordered_mock_handlers(o, meth_spec) - - req = Request("http://acme.example.com/") - self.assertEqual(req.host, "acme.example.com") - o.open(req) - self.assertEqual(req.host, "proxy.example.com:3128") - - self.assertEqual([(handlers[0], "http_open")], - [tup[0:2] for tup in o.calls]) + u = "proxy.example.com:3128" + for d in dict(http=u), dict(HTTP=u): + o = OpenerDirector() + ph = urllib.request.ProxyHandler(d) + o.add_handler(ph) + meth_spec = [ + [("http_open", "return response")] + ] + handlers = add_ordered_mock_handlers(o, meth_spec) + + req = Request("http://acme.example.com/") + self.assertEqual(req.host, "acme.example.com") + o.open(req) + self.assertEqual(req.host, u) + self.assertEqual([(handlers[0], "http_open")], + [tup[0:2] for tup in o.calls]) def test_proxy_no_proxy(self): os.environ['no_proxy'] = 'python.org' diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index f6ce9cb6d5..92c6a5d015 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -800,6 +800,7 @@ class ProxyHandler(BaseHandler): assert hasattr(proxies, 'keys'), "proxies must be a mapping" self.proxies = proxies for type, url in proxies.items(): + type = type.lower() setattr(self, '%s_open' % type, lambda r, proxy=url, type=type, meth=self.proxy_open: meth(r, proxy, type)) diff --git a/Misc/NEWS.d/next/Library/2019-05-22-04-52-35.bpo-25068.vR_rC-.rst b/Misc/NEWS.d/next/Library/2019-05-22-04-52-35.bpo-25068.vR_rC-.rst new file mode 100644 index 0000000000..89de83bce0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-05-22-04-52-35.bpo-25068.vR_rC-.rst @@ -0,0 +1,2 @@ +:class:`urllib.request.ProxyHandler` now lowercases the keys of the passed +dictionary.