Skip to content

Commit 5653e55

Browse files
committed
Embedders - fix proxies, default on http, tests
1 parent fabacdf commit 5653e55

File tree

2 files changed

+77
-8
lines changed

2 files changed

+77
-8
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import os
2+
import unittest
3+
4+
from Orange.misc.utils.embedder_utils import get_proxies
5+
6+
7+
class TestProxies(unittest.TestCase):
8+
def setUp(self) -> None:
9+
self.previous_http = os.environ.get("http_proxy")
10+
self.previous_https = os.environ.get("https_proxy")
11+
os.environ.pop("http_proxy", None)
12+
os.environ.pop("https_proxy", None)
13+
14+
def tearDown(self) -> None:
15+
os.environ.pop("http_proxy", None)
16+
os.environ.pop("https_proxy", None)
17+
if self.previous_http is not None:
18+
os.environ["http_proxy"] = self.previous_http
19+
if self.previous_https is not None:
20+
os.environ["https_proxy"] = self.previous_https
21+
22+
def test_add_scheme(self):
23+
os.environ["http_proxy"] = "test1.com"
24+
os.environ["https_proxy"] = "test2.com"
25+
res = get_proxies()
26+
self.assertEqual("http://test1.com", res["http://"])
27+
self.assertEqual("http://test2.com", res["https://"])
28+
29+
os.environ["http_proxy"] = "test1.com/path"
30+
os.environ["https_proxy"] = "test2.com/path"
31+
res = get_proxies()
32+
self.assertEqual("http://test1.com/path", res["http://"])
33+
self.assertEqual("http://test2.com/path", res["https://"])
34+
35+
os.environ["http_proxy"] = "https://test1.com:123"
36+
os.environ["https_proxy"] = "https://test2.com:124"
37+
res = get_proxies()
38+
self.assertEqual("https://test1.com:123", res["http://"])
39+
self.assertEqual("https://test2.com:124", res["https://"])
40+
41+
def test_both_urls(self):
42+
os.environ["http_proxy"] = "http://test1.com:123"
43+
os.environ["https_proxy"] = "https://test2.com:124"
44+
res = get_proxies()
45+
self.assertEqual("http://test1.com:123", res["http://"])
46+
self.assertEqual("https://test2.com:124", res["https://"])
47+
self.assertNotIn("all://", res)
48+
49+
def test_http_only(self):
50+
os.environ["http_proxy"] = "http://test1.com:123"
51+
res = get_proxies()
52+
self.assertEqual("http://test1.com:123", res["all://"])
53+
self.assertNotIn("http://", res)
54+
self.assertNotIn("https://", res)
55+
56+
def test_https_only(self):
57+
os.environ["https_proxy"] = "https://test1.com:123"
58+
res = get_proxies()
59+
self.assertEqual("https://test1.com:123", res["all://"])
60+
self.assertNotIn("http://", res)
61+
self.assertNotIn("https://", res)
62+
63+
64+
if __name__ == "__main__":
65+
unittest.main()

Orange/misc/utils/embedder_utils.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import hashlib
33
import pickle
4+
import re
45
from os import environ
56
from os.path import join, isfile
67
from typing import Optional, Dict
@@ -78,22 +79,25 @@ def add(self, cache_key, value):
7879

7980
def get_proxies() -> Optional[Dict[str, str]]:
8081
"""
81-
Return dict with proxy addresses if they exists.
82+
Return dict with proxy addresses if they exist.
8283
8384
Returns
8485
-------
8586
proxy_dict
8687
Dictionary with format {proxy type: proxy address} or None if
8788
they not set.
8889
"""
89-
def add_protocol(url: Optional[str], prot: str) -> Optional[str]:
90-
if url and not url.startswith(prot):
91-
return f"{prot}://{url}"
92-
return url
93-
http_proxy = add_protocol(environ.get("http_proxy"), "http")
94-
https_proxy = add_protocol(environ.get("https_proxy"), "https")
90+
def add_scheme(url: Optional[str]) -> Optional[str]:
91+
if url is not None and not re.search(r"^[A-Za-z0-9+.\-]+://", url):
92+
# if no scheme default to http - as other libraries do (e.g. requests)
93+
return f"http://{url}"
94+
else:
95+
return url
96+
97+
http_proxy = add_scheme(environ.get("http_proxy"))
98+
https_proxy = add_scheme(environ.get("https_proxy"))
9599
if http_proxy and https_proxy: # both proxy addresses defined
96-
return {"http://": https_proxy, "https://": https_proxy}
100+
return {"http://": http_proxy, "https://": https_proxy}
97101
elif any([https_proxy, http_proxy]): # one of the proxies defined
98102
return {"all://": http_proxy or https_proxy}
99103
return None # proxies not defined

0 commit comments

Comments
 (0)