Skip to content

Commit 590ed09

Browse files
bpo-25068: urllib.request.ProxyHandler now lowercases the dict keys (GH-13489)
(cherry picked from commit b761e3a) Co-authored-by: Zackery Spytz <[email protected]>
1 parent bd2e7cc commit 590ed09

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

Lib/test/test_urllib2.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,21 +1340,22 @@ def http_open(self, req):
13401340
self.assertTrue(request.startswith(expected), repr(request))
13411341

13421342
def test_proxy(self):
1343-
o = OpenerDirector()
1344-
ph = urllib.request.ProxyHandler(dict(http="proxy.example.com:3128"))
1345-
o.add_handler(ph)
1346-
meth_spec = [
1347-
[("http_open", "return response")]
1348-
]
1349-
handlers = add_ordered_mock_handlers(o, meth_spec)
1350-
1351-
req = Request("http://acme.example.com/")
1352-
self.assertEqual(req.host, "acme.example.com")
1353-
o.open(req)
1354-
self.assertEqual(req.host, "proxy.example.com:3128")
1355-
1356-
self.assertEqual([(handlers[0], "http_open")],
1357-
[tup[0:2] for tup in o.calls])
1343+
u = "proxy.example.com:3128"
1344+
for d in dict(http=u), dict(HTTP=u):
1345+
o = OpenerDirector()
1346+
ph = urllib.request.ProxyHandler(d)
1347+
o.add_handler(ph)
1348+
meth_spec = [
1349+
[("http_open", "return response")]
1350+
]
1351+
handlers = add_ordered_mock_handlers(o, meth_spec)
1352+
1353+
req = Request("http://acme.example.com/")
1354+
self.assertEqual(req.host, "acme.example.com")
1355+
o.open(req)
1356+
self.assertEqual(req.host, u)
1357+
self.assertEqual([(handlers[0], "http_open")],
1358+
[tup[0:2] for tup in o.calls])
13581359

13591360
def test_proxy_no_proxy(self):
13601361
os.environ['no_proxy'] = 'python.org'

Lib/urllib/request.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ def __init__(self, proxies=None):
800800
assert hasattr(proxies, 'keys'), "proxies must be a mapping"
801801
self.proxies = proxies
802802
for type, url in proxies.items():
803+
type = type.lower()
803804
setattr(self, '%s_open' % type,
804805
lambda r, proxy=url, type=type, meth=self.proxy_open:
805806
meth(r, proxy, type))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:class:`urllib.request.ProxyHandler` now lowercases the keys of the passed
2+
dictionary.

0 commit comments

Comments
 (0)