merge and add some test cases
This commit is contained in:
liris
2014-11-06 08:44:11 +09:00
parent 9f4cdb9ec9
commit 6b029168cf
3 changed files with 44 additions and 7 deletions

View File

@@ -3,7 +3,8 @@ ChangeLog
- 0.22.0
- fix not thread-safe of Websocket.close() (#120)
- Fix not thread-safe of Websocket.close() (#120)
- Try to get proxy info from environment if not explicitly provided (#124)
- 0.21.0

View File

@@ -184,14 +184,14 @@ def _get_proxy_info(is_secure, **options):
if http_proxy_host:
return http_proxy_host, options.get("http_proxy_port", 0)
try_env_keys = ["http_proxy"]
env_keys = ["http_proxy"]
if is_secure:
try_env_keys.insert(0, "https_proxy")
env_keys.insert(0, "https_proxy")
for env_key in try_env_keys:
env_value = os.environ.get(env_key, None)
if env_value:
proxy = urlparse(env_value)
for key in env_keys:
value = os.environ.get(key, None)
if value:
proxy = urlparse(value)
return proxy.hostname, proxy.port
return None, 0

View File

@@ -5,6 +5,7 @@ import six
import sys
sys.path[0:0] = [""]
import os
import os.path
import base64
import socket
@@ -25,6 +26,7 @@ import uuid
# websocket-client
import websocket as ws
from websocket._core import _parse_url, _create_sec_websocket_key
from websocket._core import _get_proxy_info
from websocket._utils import validate_utf8
@@ -533,5 +535,39 @@ class UtilsTest(unittest.TestCase):
state = validate_utf8(six.b(''))
self.assertEqual(state, True)
class ProxyInfoTest(unittest.TestCase):
def setUp(self):
if "http_proxy" in os.environ:
del os.environ["http_proxy"]
if "https_proxy" in os.environ:
del os.environ["https_proxy"]
def testProxyFromArgs(self):
self.assertEqual(_get_proxy_info(False, http_proxy_host="localhost"), ("localhost", 0))
self.assertEqual(_get_proxy_info(False, http_proxy_host="localhost", http_proxy_port=3128), ("localhost", 3128))
self.assertEqual(_get_proxy_info(True, http_proxy_host="localhost"), ("localhost", 0))
self.assertEqual(_get_proxy_info(True, http_proxy_host="localhost", http_proxy_port=3128), ("localhost", 3128))
def testProxyFromEnv(self):
os.environ["http_proxy"] = "http://localhost/"
self.assertEqual(_get_proxy_info(False), ("localhost", None))
os.environ["http_proxy"] = "http://localhost:3128/"
self.assertEqual(_get_proxy_info(False), ("localhost", 3128))
os.environ["http_proxy"] = "http://localhost/"
os.environ["https_proxy"] = "http://localhost2/"
self.assertEqual(_get_proxy_info(False), ("localhost", None))
os.environ["http_proxy"] = "http://localhost:3128/"
os.environ["https_proxy"] = "http://localhost2:3128/"
self.assertEqual(_get_proxy_info(False), ("localhost", 3128))
os.environ["http_proxy"] = "http://localhost/"
os.environ["https_proxy"] = "http://localhost2/"
self.assertEqual(_get_proxy_info(True), ("localhost2", None))
os.environ["http_proxy"] = "http://localhost:3128/"
os.environ["https_proxy"] = "http://localhost2:3128/"
self.assertEqual(_get_proxy_info(True), ("localhost2", 3128))
if __name__ == "__main__":
unittest.main()