def setUp(self): self.proxy_server = ProxyServerThread(timeout=self.timeout) self.proxy_server.start() self.http_server = HttpServerThread(timeout=self.timeout) self.http_server.start() self.proxy_url = self.proxy_server.get_proxy_url() self.remote_website_http = self.http_server.get_server_url()
def setUp(self): self.proxy_server = ProxyServerThread(timeout=self.timeout) self.proxy_server.start() self.proxy_url = self.proxy_server.get_proxy_url() self.assertIsNone(os.environ.get('http_proxy')) self.assertIsNone(os.environ.get('https_proxy')) os.environ['http_proxy'] = self.proxy_url os.environ['https_proxy'] = self.proxy_url
class ProxyTestCase(unittest.TestCase): # pylint: disable=R0904,C0111 remote_website_http = "http://example.org/" remote_website_https = "https://example.org/" timeout = 5 def setUp(self): self.proxy_server = ProxyServerThread(timeout=self.timeout) self.proxy_server.start() self.proxy_url = self.proxy_server.get_proxy_url() def tearDown(self): self.proxy_server.stop() self.proxy_server.join() def test_geocoder_constructor_uses_http_proxy(self): base_http = urlopen(self.remote_website_http, timeout=self.timeout) base_html = base_http.read() geocoder_dummy = DummyGeocoder(proxies={"http": 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_uses_https_proxy(self): base_http = urlopen(self.remote_website_https, timeout=self.timeout) base_html = base_http.read() geocoder_dummy = DummyGeocoder(proxies={"https": self.proxy_url}, timeout=self.timeout) self.assertEqual(0, len(self.proxy_server.requests)) self.assertEqual(base_html, geocoder_dummy.geocode(self.remote_website_https)) self.assertEqual(1, len(self.proxy_server.requests))
async def start_proxy(_, request, class_geocoder): cls = request.cls cls.proxy_server = ProxyServerThread(timeout=cls.proxy_timeout) cls.proxy_server.start() cls.proxy_url = cls.proxy_server.get_proxy_url() async with cls.inject_geocoder( cls.make_geocoder(proxies=cls.proxy_url)): await yield_() cls.proxy_server.stop() cls.proxy_server.join()
class SystemProxiesTestCase(unittest.TestCase): timeout = 5 def setUp(self): self.proxy_server = ProxyServerThread(timeout=self.timeout) self.proxy_server.start() self.http_server = HttpServerThread(timeout=self.timeout) self.http_server.start() self.proxy_url = self.proxy_server.get_proxy_url() self.remote_website_http = self.http_server.get_server_url() self.assertIsNone(os.environ.get('http_proxy')) self.assertIsNone(os.environ.get('https_proxy')) os.environ['http_proxy'] = self.proxy_url os.environ['https_proxy'] = self.proxy_url def tearDown(self): self.proxy_server.stop() self.http_server.stop() self.proxy_server.join() self.http_server.join() os.environ.pop('http_proxy', None) os.environ.pop('https_proxy', None) def test_system_proxies_are_respected_by_default(self): geocoder_dummy = DummyGeocoder(timeout=self.timeout) self.assertEqual(0, len(self.proxy_server.requests)) geocoder_dummy.geocode(self.remote_website_http) self.assertEqual(1, len(self.proxy_server.requests)) def test_system_proxies_are_respected_with_none(self): # proxies=None means "use system proxies", e.g. from the ENV. geocoder_dummy = DummyGeocoder(proxies=None, timeout=self.timeout) self.assertEqual(0, len(self.proxy_server.requests)) geocoder_dummy.geocode(self.remote_website_http) self.assertEqual(1, len(self.proxy_server.requests)) def test_system_proxies_are_reset_with_empty_dict(self): geocoder_dummy = DummyGeocoder(proxies={}, timeout=self.timeout) self.assertEqual(0, len(self.proxy_server.requests)) geocoder_dummy.geocode(self.remote_website_http) self.assertEqual(0, len(self.proxy_server.requests)) def test_string_value_overrides_system_proxies(self): os.environ['http_proxy'] = '127.0.0.1:1' os.environ['https_proxy'] = '127.0.0.1:1' geocoder_dummy = DummyGeocoder(proxies=self.proxy_url, timeout=self.timeout) self.assertEqual(0, len(self.proxy_server.requests)) geocoder_dummy.geocode(self.remote_website_http) self.assertEqual(1, len(self.proxy_server.requests))
class ProxyTestCase(unittest.TestCase): # pylint: disable=R0904,C0111 remote_website_http = "http://example.org/" remote_website_https = "https://example.org/" timeout = 5 def setUp(self): self.proxy_server = ProxyServerThread(timeout=self.timeout) self.proxy_server.start() self.proxy_url = self.proxy_server.get_proxy_url() def tearDown(self): self.proxy_server.stop() self.proxy_server.join() def test_geocoder_constructor_uses_http_proxy(self): base_http = urlopen(self.remote_website_http, timeout=self.timeout) base_html = base_http.read() geocoder_dummy = DummyGeocoder(proxies={"http": 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_uses_https_proxy(self): base_http = urlopen(self.remote_website_https, timeout=self.timeout) base_html = base_http.read() geocoder_dummy = DummyGeocoder(proxies={"https": self.proxy_url}, timeout=self.timeout) self.assertEqual(0, len(self.proxy_server.requests)) self.assertEqual(base_html, geocoder_dummy.geocode(self.remote_website_https)) self.assertEqual(1, len(self.proxy_server.requests)) @unittest.skipUnless(hasattr( ssl, 'create_default_context' ), "The current Python version doesn't support `ssl.create_default_context`." ) def test_ssl_context_with_proxy_is_respected(self): # Create an ssl context which should not allow the negotiation with # the `self.remote_website_https`. bad_ctx = ssl.create_default_context(cafile=CERT_SELFSIGNED_CA) geocoder_dummy = DummyGeocoder(proxies={"https": self.proxy_url}, ssl_context=bad_ctx, timeout=self.timeout) self.assertEqual(0, len(self.proxy_server.requests)) with self.assertRaises(GeocoderServiceError) as cm: geocoder_dummy.geocode(self.remote_website_https) self.assertIn('SSL', str(cm.exception)) self.assertEqual(1, len(self.proxy_server.requests))
class SystemProxiesTestCase(unittest.TestCase): remote_website_http = "http://example.org/" timeout = 5 def setUp(self): self.proxy_server = ProxyServerThread(timeout=self.timeout) self.proxy_server.start() self.proxy_url = self.proxy_server.get_proxy_url() self.assertIsNone(os.environ.get('http_proxy')) self.assertIsNone(os.environ.get('https_proxy')) os.environ['http_proxy'] = self.proxy_url os.environ['https_proxy'] = self.proxy_url def tearDown(self): self.proxy_server.stop() self.proxy_server.join() os.environ.pop('http_proxy', None) os.environ.pop('https_proxy', None) def test_system_proxies_are_respected_by_default(self): geocoder_dummy = DummyGeocoder(timeout=self.timeout) self.assertEqual(0, len(self.proxy_server.requests)) geocoder_dummy.geocode(self.remote_website_http) self.assertEqual(1, len(self.proxy_server.requests)) def test_system_proxies_are_respected_with_none(self): # proxies=None means "use system proxies", e.g. from the ENV. geocoder_dummy = DummyGeocoder(proxies=None, timeout=self.timeout) self.assertEqual(0, len(self.proxy_server.requests)) geocoder_dummy.geocode(self.remote_website_http) self.assertEqual(1, len(self.proxy_server.requests)) def test_system_proxies_are_reset_with_empty_dict(self): geocoder_dummy = DummyGeocoder(proxies={}, timeout=self.timeout) self.assertEqual(0, len(self.proxy_server.requests)) geocoder_dummy.geocode(self.remote_website_http) self.assertEqual(0, len(self.proxy_server.requests)) def test_string_value_overrides_system_proxies(self): os.environ['http_proxy'] = '127.0.0.1:1' os.environ['https_proxy'] = '127.0.0.1:1' geocoder_dummy = DummyGeocoder(proxies=self.proxy_url, timeout=self.timeout) self.assertEqual(0, len(self.proxy_server.requests)) geocoder_dummy.geocode(self.remote_website_http) self.assertEqual(1, len(self.proxy_server.requests))
class LocalProxyTestCase(unittest.TestCase): timeout = 5 def setUp(self): self.proxy_server = ProxyServerThread(timeout=self.timeout) self.proxy_server.start() self.http_server = HttpServerThread(timeout=self.timeout) self.http_server.start() self.proxy_url = self.proxy_server.get_proxy_url() self.remote_website_http = self.http_server.get_server_url() def tearDown(self): self.proxy_server.stop() self.http_server.stop() self.proxy_server.join() self.http_server.join() def test_geocoder_constructor_uses_http_proxy(self): base_http = urlopen(self.remote_website_http, timeout=self.timeout) base_html = base_http.read() geocoder_dummy = DummyGeocoder(proxies={"http": 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_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_has_both_schemes_proxy(self): g = DummyGeocoder(proxies=self.proxy_url, scheme='http') self.assertDictEqual(g.proxies, { 'http': self.proxy_url, 'https': self.proxy_url })
class IGNFranceUsernameAuthProxyTestCase(GeocoderTestBase): @classmethod def make_geocoder(cls, **kwargs): return IGNFrance(api_key=env['IGNFRANCE_USERNAME_KEY'], username=env['IGNFRANCE_USERNAME'], password=env['IGNFRANCE_PASSWORD'], timeout=10, **kwargs) proxy_timeout = 5 def setUp(self): self.proxy_server = ProxyServerThread(timeout=self.proxy_timeout) self.proxy_server.start() self.proxy_url = self.proxy_server.get_proxy_url() self.geocoder = self.make_geocoder(proxies=self.proxy_url) def tearDown(self): self.proxy_server.stop() self.proxy_server.join() def test_proxy_is_respected(self): self.assertEqual(0, len(self.proxy_server.requests)) self.geocode_run( { "query": "Camp des Landes, 41200 VILLEFRANCHE-SUR-CHER", "query_type": "StreetAddress", "exactly_one": True }, { "latitude": 47.293048, "longitude": 1.718985, "address": "le camp des landes, 41200 Villefranche-sur-Cher" }, ) self.assertEqual(1, len(self.proxy_server.requests))
def setUp(self): self.proxy_server = ProxyServerThread(timeout=self.timeout) self.proxy_server.start() self.proxy_url = self.proxy_server.get_proxy_url()
def setUp(self): self.proxy_server = ProxyServerThread(timeout=self.proxy_timeout) self.proxy_server.start() self.proxy_url = self.proxy_server.get_proxy_url() self.geocoder = self.make_geocoder(proxies=self.proxy_url)
def proxy_server_thread(timeout): with ProxyServerThread(timeout=timeout) as proxy_server: yield proxy_server
class ProxyTestCase(unittest.TestCase): remote_website_http = "http://example.org/" remote_website_https = "https://example.org/" timeout = 5 def setUp(self): self.proxy_server = ProxyServerThread(timeout=self.timeout) self.proxy_server.start() self.proxy_url = self.proxy_server.get_proxy_url() def tearDown(self): self.proxy_server.stop() self.proxy_server.join() def test_geocoder_constructor_uses_http_proxy(self): base_http = urlopen(self.remote_website_http, timeout=self.timeout) base_html = base_http.read() geocoder_dummy = DummyGeocoder(proxies={"http": 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_uses_https_proxy(self): base_http = urlopen(self.remote_website_https, timeout=self.timeout) base_html = base_http.read() geocoder_dummy = DummyGeocoder(proxies={"https": self.proxy_url}, timeout=self.timeout) self.assertEqual(0, len(self.proxy_server.requests)) self.assertEqual( base_html, geocoder_dummy.geocode(self.remote_website_https) ) self.assertEqual(1, len(self.proxy_server.requests)) @unittest.skipUnless( hasattr(ssl, 'create_default_context'), "The current Python version doesn't support `ssl.create_default_context`." ) def test_ssl_context_with_proxy_is_respected(self): # Create an ssl context which should not allow the negotiation with # the `self.remote_website_https`. bad_ctx = ssl.create_default_context(cafile=CERT_SELFSIGNED_CA) geocoder_dummy = DummyGeocoder(proxies={"https": self.proxy_url}, ssl_context=bad_ctx, timeout=self.timeout) self.assertEqual(0, len(self.proxy_server.requests)) with self.assertRaises(GeocoderServiceError) as cm: 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_has_both_schemes_proxy(self): g = DummyGeocoder(proxies=self.proxy_url, scheme='http') self.assertDictEqual(g.proxies, {'http': self.proxy_url, 'https': self.proxy_url})