-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
73 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import unittest | ||
|
||
from twstock.proxy import get_proxies, configure_proxy_provider, reset_proxy_provider | ||
from twstock.proxy import SingleProxyProvider | ||
|
||
|
||
class ProxyProviderTest(unittest.TestCase): | ||
|
||
def setUp(self): | ||
reset_proxy_provider() | ||
|
||
def tearDown(self): | ||
reset_proxy_provider() | ||
|
||
def test_configure(self): | ||
# default values are empty | ||
self.assertDictEqual({}, get_proxies()) | ||
|
||
# configure fake proxy | ||
configure_proxy_provider(SingleProxyProvider(dict(http="http-proxy", https="https-proxy"))) | ||
self.assertEqual("http-proxy", get_proxies()['http']) | ||
self.assertEqual("https-proxy", get_proxies()['https']) | ||
|
||
# reset proxy | ||
reset_proxy_provider() | ||
self.assertDictEqual({}, get_proxies()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import abc | ||
|
||
|
||
class ProxyProvider(abc.ABC): | ||
@abc.abstractmethod | ||
def get_proxy(self): | ||
return NotImplemented | ||
|
||
|
||
class NoProxyProvier(ProxyProvider): | ||
def get_proxy(self): | ||
return {} | ||
|
||
|
||
class SingleProxyProvider(ProxyProvider): | ||
def __init__(self, proxy=None): | ||
self._proxy = proxy | ||
|
||
def get_proxy(self): | ||
return self._proxy | ||
|
||
|
||
_provider_instance = NoProxyProvier() | ||
|
||
|
||
def reset_proxy_provider(): | ||
configure_proxy_provider(NoProxyProvier()) | ||
|
||
|
||
def configure_proxy_provider(provider_instance): | ||
global _provider_instance | ||
if not isinstance(provider_instance, ProxyProvider): | ||
raise BaseException("proxy provider should be a ProxyProvider object") | ||
_provider_instance = provider_instance | ||
|
||
|
||
def get_proxies(): | ||
return _provider_instance.get_proxy() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters