Best Python code snippet using localstack_python
test_config.py
Source:test_config.py
...5import certifi6import pytest7import httpx8def test_load_ssl_config():9 context = httpx.create_ssl_context()10 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED11 assert context.check_hostname is True12def test_load_ssl_config_verify_non_existing_path():13 with pytest.raises(IOError):14 httpx.create_ssl_context(verify="/path/to/nowhere")15def test_load_ssl_config_verify_existing_file():16 context = httpx.create_ssl_context(verify=certifi.where())17 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED18 assert context.check_hostname is True19@pytest.mark.parametrize("config", ("SSL_CERT_FILE", "SSL_CERT_DIR"))20def test_load_ssl_config_verify_env_file(21 https_server, ca_cert_pem_file, config, cert_authority22):23 os.environ[config] = (24 ca_cert_pem_file25 if config.endswith("_FILE")26 else str(Path(ca_cert_pem_file).parent)27 )28 context = httpx.create_ssl_context(trust_env=True)29 cert_authority.configure_trust(context)30 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED31 assert context.check_hostname is True32 assert len(context.get_ca_certs()) == 133def test_load_ssl_config_verify_directory():34 path = Path(certifi.where()).parent35 context = httpx.create_ssl_context(verify=str(path))36 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED37 assert context.check_hostname is True38def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):39 context = httpx.create_ssl_context(cert=(cert_pem_file, cert_private_key_file))40 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED41 assert context.check_hostname is True42@pytest.mark.parametrize("password", [b"password", "password"])43def test_load_ssl_config_cert_and_encrypted_key(44 cert_pem_file, cert_encrypted_private_key_file, password45):46 context = httpx.create_ssl_context(47 cert=(cert_pem_file, cert_encrypted_private_key_file, password)48 )49 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED50 assert context.check_hostname is True51def test_load_ssl_config_cert_and_key_invalid_password(52 cert_pem_file, cert_encrypted_private_key_file53):54 with pytest.raises(ssl.SSLError):55 httpx.create_ssl_context(56 cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")57 )58def test_load_ssl_config_cert_without_key_raises(cert_pem_file):59 with pytest.raises(ssl.SSLError):60 httpx.create_ssl_context(cert=cert_pem_file)61def test_load_ssl_config_no_verify():62 context = httpx.create_ssl_context(verify=False)63 assert context.verify_mode == ssl.VerifyMode.CERT_NONE64 assert context.check_hostname is False65def test_load_ssl_context():66 ssl_context = ssl.create_default_context()67 context = httpx.create_ssl_context(verify=ssl_context)68 assert context is ssl_context69def test_create_ssl_context_with_get_request(server, cert_pem_file):70 context = httpx.create_ssl_context(verify=cert_pem_file)71 response = httpx.get(server.url, verify=context)72 assert response.status_code == 20073def test_limits_repr():74 limits = httpx.Limits(max_connections=100)75 expected = "Limits(max_connections=100, max_keepalive_connections=None, keepalive_expiry=5.0)"76 assert repr(limits) == expected77def test_limits_eq():78 limits = httpx.Limits(max_connections=100)79 assert limits == httpx.Limits(max_connections=100)80def test_timeout_eq():81 timeout = httpx.Timeout(timeout=5.0)82 assert timeout == httpx.Timeout(timeout=5.0)83def test_timeout_all_parameters_set():84 timeout = httpx.Timeout(connect=5.0, read=5.0, write=5.0, pool=5.0)85 assert timeout == httpx.Timeout(timeout=5.0)86def test_timeout_from_nothing():87 timeout = httpx.Timeout(None)88 assert timeout.connect is None89 assert timeout.read is None90 assert timeout.write is None91 assert timeout.pool is None92def test_timeout_from_none():93 timeout = httpx.Timeout(timeout=None)94 assert timeout == httpx.Timeout(None)95def test_timeout_from_one_none_value():96 timeout = httpx.Timeout(None, read=None)97 assert timeout == httpx.Timeout(None)98def test_timeout_from_one_value():99 timeout = httpx.Timeout(None, read=5.0)100 assert timeout == httpx.Timeout(timeout=(None, 5.0, None, None))101def test_timeout_from_one_value_and_default():102 timeout = httpx.Timeout(5.0, pool=60.0)103 assert timeout == httpx.Timeout(timeout=(5.0, 5.0, 5.0, 60.0))104def test_timeout_missing_default():105 with pytest.raises(ValueError):106 httpx.Timeout(pool=60.0)107def test_timeout_from_tuple():108 timeout = httpx.Timeout(timeout=(5.0, 5.0, 5.0, 5.0))109 assert timeout == httpx.Timeout(timeout=5.0)110def test_timeout_from_config_instance():111 timeout = httpx.Timeout(timeout=5.0)112 assert httpx.Timeout(timeout) == httpx.Timeout(timeout=5.0)113def test_timeout_repr():114 timeout = httpx.Timeout(timeout=5.0)115 assert repr(timeout) == "Timeout(timeout=5.0)"116 timeout = httpx.Timeout(None, read=5.0)117 assert repr(timeout) == "Timeout(connect=None, read=5.0, write=None, pool=None)"118@pytest.mark.skipif(119 not hasattr(ssl.SSLContext, "keylog_filename"),120 reason="requires OpenSSL 1.1.1 or higher",121)122@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")123def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch): # pragma: nocover124 with monkeypatch.context() as m:125 m.delenv("SSLKEYLOGFILE", raising=False)126 context = httpx.create_ssl_context(trust_env=True)127 assert context.keylog_filename is None # type: ignore128 filename = str(tmpdir.join("test.log"))129 with monkeypatch.context() as m:130 m.setenv("SSLKEYLOGFILE", filename)131 context = httpx.create_ssl_context(trust_env=True)132 assert context.keylog_filename == filename # type: ignore133 context = httpx.create_ssl_context(trust_env=False)134 assert context.keylog_filename is None # type: ignore135def test_proxy_from_url():136 proxy = httpx.Proxy("https://example.com")137 assert str(proxy.url) == "https://example.com"138 assert proxy.auth is None139 assert proxy.headers == {}140 assert repr(proxy) == "Proxy('https://example.com')"141def test_proxy_with_auth_from_url():142 proxy = httpx.Proxy("https://username:password@example.com")143 assert str(proxy.url) == "https://example.com"144 assert proxy.auth == ("username", "password")145 assert proxy.headers == {}146 assert repr(proxy) == "Proxy('https://example.com', auth=('username', '********'))"147def test_invalid_proxy_scheme():...
test_helpers.py
Source:test_helpers.py
...16 self.assertTrue(keyfile.exists(), str(keyfile))17 return cafile, certfile, keyfile18 @pytest.mark.skipif(sys.version_info >= (3, 4),19 reason="requires python3.4")20 def test_create_ssl_context(self):21 # TODO: I would realy want to check that proper certificates load, but22 # the API is fuzzy, and 3.3 misses a lot of functions, so23 # for now this test only checks that no error are raised during24 # context creation...25 cafile, certfile, keyfile = self._check_ssl_dir()26 context = create_ssl_context()27 self.assertEqual(context.verify_mode, ssl.CERT_REQUIRED)28 context = create_ssl_context(cafile=str(cafile))29 self.assertEqual(context.verify_mode, ssl.CERT_REQUIRED)30 context = create_ssl_context(31 cafile=str(cafile),32 certfile=str(certfile),33 keyfile=str(keyfile),34 password="abcdefgh")35 self.assertEqual(context.verify_mode, ssl.CERT_REQUIRED)36 with self.assertRaisesRegexp(37 ValueError, "`cadata` not supported by Python3.3"):38 with cafile.open("rb") as f:39 data = f.read()40 create_ssl_context(cadata=data.decode("ascii"))41 @pytest.mark.skipif(sys.version_info < (3, 4),42 reason="requires python3.4")43 def test_create_ssl_context_py34(self):44 cafile, certfile, keyfile = self._check_ssl_dir()45 context = create_ssl_context()46 self.assertEqual(context.verify_mode, ssl.CERT_REQUIRED)47 self.assertEqual(context.check_hostname, True)48 context = create_ssl_context(cafile=str(cafile))49 self.assertEqual(context.verify_mode, ssl.CERT_REQUIRED)50 self.assertEqual(context.check_hostname, True)51 der_ca = context.get_ca_certs(binary_form=True)52 self.assertTrue(der_ca)53 # Same with `cadata` argument54 with cafile.open("rb") as f:55 data = f.read()56 context = create_ssl_context(cadata=data.decode("ascii"))57 self.assertEqual(context.get_ca_certs(binary_form=True), der_ca)58 # And with DER encoded binary form59 context = create_ssl_context(cadata=der_ca[0])60 self.assertEqual(context.get_ca_certs(binary_form=True), der_ca)61 context = create_ssl_context(62 cafile=str(cafile),63 certfile=str(certfile),64 keyfile=str(keyfile),65 password="abcdefgh")66 self.assertEqual(context.verify_mode, ssl.CERT_REQUIRED)67 self.assertEqual(context.check_hostname, True)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!