Skip to content

Commit

Permalink
bpo-25068: urllib.request.ProxyHandler now lowercases the dict keys (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
ZackerySpytz authored and matrixise committed Sep 13, 2019
1 parent 693aa80 commit b761e3a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
31 changes: 16 additions & 15 deletions Lib/test/test_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1342,21 +1342,22 @@ def http_open(self, req):
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'
Expand Down
1 change: 1 addition & 0 deletions Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ def __init__(self, proxies=None):
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))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:class:`urllib.request.ProxyHandler` now lowercases the keys of the passed
dictionary.

0 comments on commit b761e3a

Please sign in to comment.