Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxies can be sent as a string #300

Merged
merged 8 commits into from
Jun 13, 2018
Merged
5 changes: 5 additions & 0 deletions geopy/geocoders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class options(object):
If specified, tunnel requests through the specified proxy.
E.g., ``{"https": "192.0.2.0"}``. For more information, see
documentation on :class:`urllib.request.ProxyHandler`.
Can be sent as a string ``"https://192.0.2.0"``, this will
automatically set the scheme to the ``https``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I don't like it 🤔. But I'm not ready to propose a better phrasing for now as well.

Let's just keep it, but I'll get to this later after merging.


default_scheme
Use ``'https'`` or ``'http'`` as the API URL's scheme.
Expand Down Expand Up @@ -185,6 +187,9 @@ def __init__(
else options.default_ssl_context)

if self.proxies:
if isinstance(self.proxies, string_compare):
self.proxies = {'http': self.proxies, 'https': self.proxies}

opener = build_opener_with_context(
self.ssl_context,
ProxyHandler(self.proxies),
Expand Down
17 changes: 17 additions & 0 deletions test/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,20 @@ def test_ssl_context_with_proxy_is_respected(self):
geocoder_dummy.geocode(self.remote_website_https)
self.assertIn('SSL', str(cm.exception))
self.assertEqual(1, len(self.proxy_server.requests))

def test_geocoder_constructor_uses_str_proxy(self):
base_http = urlopen(self.remote_website_http, timeout=self.timeout)
base_html = base_http.read()
geocoder_dummy = DummyGeocoder(proxies=self.proxy_url,
timeout=self.timeout)
self.assertEqual(0, len(self.proxy_server.requests))
self.assertEqual(
base_html,
geocoder_dummy.geocode(self.remote_website_http)
)
self.assertEqual(1, len(self.proxy_server.requests))

def test_geocoder_constructor_have_both_schemes_proxy(self):
g = DummyGeocoder(proxies=self.proxy_url, scheme='http')
self.assertDictEqual(g.proxies, {'http': self.proxy_url,
'https': self.proxy_url})