async def send( self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None, ): method = request.method url = request.url headers = [(_encode(k), _encode(v)) for k, v in request.headers.items()] if not request.body: body = b"" elif isinstance(request.body, str): body = _encode(request.body) else: body = request.body if isinstance(timeout, tuple): timeout_kwargs = { "connect_timeout": timeout[0], "read_timeout": timeout[1], } else: timeout_kwargs = { "connect_timeout": timeout, "read_timeout": timeout, } ssl = httpcore.SSLConfig(cert=cert, verify=verify) timeout = httpcore.TimeoutConfig(**timeout_kwargs) try: response = await self.pool.request( method, url, headers=headers, body=body, stream=stream, ssl=ssl, timeout=timeout, ) except (httpcore.BadResponse, socket.error) as err: raise ConnectionError(err, request=request) except httpcore.ConnectTimeout as err: raise requests.exceptions.ConnectTimeout(err, request=request) except httpcore.ReadTimeout as err: raise requests.exceptions.ReadTimeout(err, request=request) return self.build_response(request, response)
async def test_load_ssl_config(): ssl_config = httpcore.SSLConfig() context = await ssl_config.load_ssl_context() assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
def test_ssl_eq(): ssl = httpcore.SSLConfig(verify=False) assert ssl == httpcore.SSLConfig(verify=False)
def test_ssl_repr(): ssl = httpcore.SSLConfig(verify=False) assert repr(ssl) == "SSLConfig(cert=None, verify=False)"
async def test_load_ssl_config_no_verify(verify=False): ssl_config = httpcore.SSLConfig(verify=False) context = await ssl_config.load_ssl_context() assert context.verify_mode == ssl.VerifyMode.CERT_NONE
async def test_load_ssl_config_cert_without_key_raises(cert_and_key_paths): cert_path, _ = cert_and_key_paths ssl_config = httpcore.SSLConfig(cert=cert_path) with pytest.raises(ssl.SSLError): await ssl_config.load_ssl_context()
async def test_load_ssl_config_cert_and_key(cert_and_key_paths): cert_path, key_path = cert_and_key_paths ssl_config = httpcore.SSLConfig(cert=(cert_path, key_path)) context = await ssl_config.load_ssl_context() assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
async def test_load_ssl_config_verify_directory(): path = os.path.dirname(httpcore.config.DEFAULT_CA_BUNDLE_PATH) ssl_config = httpcore.SSLConfig(verify=path) context = await ssl_config.load_ssl_context() assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
async def test_load_ssl_config_verify_existing_file(): ssl_config = httpcore.SSLConfig( verify=httpcore.config.DEFAULT_CA_BUNDLE_PATH) context = await ssl_config.load_ssl_context() assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
async def test_load_ssl_config_verify_non_existing_path(): ssl_config = httpcore.SSLConfig(verify="/path/to/nowhere") with pytest.raises(IOError): await ssl_config.load_ssl_context()