def test_proxy_https_requests( proxy_server: typing.Tuple[bytes, bytes, int], ca_ssl_context: ssl.SSLContext, proxy_mode: str, http2: bool, ) -> None: method = b"GET" url = (b"https", b"example.org", 443, b"/") headers = [(b"host", b"example.org")] max_connections = 1 max_keepalive = 2 with httpcore.SyncHTTPProxy( proxy_server, proxy_mode=proxy_mode, ssl_context=ca_ssl_context, max_connections=max_connections, max_keepalive=max_keepalive, http2=http2, ) as http: http_version, status_code, reason, headers, stream = http.request( method, url, headers) _ = read_body(stream) assert http_version == (b"HTTP/2" if http2 else b"HTTP/1.1") assert status_code == 200 assert reason == b"OK"
def test_proxy_socket_does_not_leak_when_the_connection_hasnt_been_added_to_pool( proxy_server: URL, server: Server, proxy_mode: str, protocol: bytes, port: int, ): method = b"GET" url = (protocol, b"blockedhost.example.com", port, b"/") headers = [(b"host", b"blockedhost.example.com")] with pytest.warns(None) as recorded_warnings: with httpcore.SyncHTTPProxy(proxy_server, proxy_mode=proxy_mode) as http: for _ in range(100): try: _ = http.request(method, url, headers) except (httpcore.ProxyError, httpcore.RemoteProtocolError): pass # have to filter out https://github.com/encode/httpx/issues/825 from other tests warnings = [ *filter(lambda warn: "asyncio" not in warn.filename, recorded_warnings.list) ] assert len(warnings) == 0
def test_proxy_https_requests( proxy_server: URL, proxy_mode: str, http2: bool, https_server: Server, ) -> None: max_connections = 1 with httpcore.SyncHTTPProxy( proxy_server, proxy_mode=proxy_mode, max_connections=max_connections, http2=http2, ) as http: status_code, headers, stream, extensions = http.handle_request( method=b"GET", url=(b"https", *https_server.netloc, b"/"), headers=[https_server.host_header], stream=httpcore.ByteStream(b""), extensions={}, ) _ = read_body(stream) assert status_code == 200 assert extensions["http_version"] == b"HTTP/2" if http2 else b"HTTP/1.1" assert extensions.get("reason_phrase", b"") == b"" if http2 else b"OK"
def test_http_proxy(proxy_server: URL, proxy_mode: str, backend: str) -> None: method = b"GET" url = (b"http", b"example.org", 80, b"/") headers = [(b"host", b"example.org")] max_connections = 1 with httpcore.SyncHTTPProxy( proxy_server, proxy_mode=proxy_mode, max_connections=max_connections, backend=backend, ) as http: status_code, headers, stream, ext = http.request(method, url, headers) read_body(stream) assert status_code == 200 assert ext == {"http_version": "HTTP/1.1", "reason": "OK"}
def test_http_proxy(proxy_server: URL, proxy_mode: str, backend: str, server: Server) -> None: method = b"GET" url = (b"http", *server.netloc, b"/") headers = [server.host_header] max_connections = 1 with httpcore.SyncHTTPProxy( proxy_server, proxy_mode=proxy_mode, max_connections=max_connections, backend=backend, ) as http: status_code, headers, stream, ext = http.request(method, url, headers) read_body(stream) assert status_code == 200 reason = "OK" if server.sends_reason else "" assert ext == {"http_version": "HTTP/1.1", "reason": reason}
def init_proxy_transport( self, proxy: Proxy, verify: VerifyTypes = True, cert: CertTypes = None, pool_limits: PoolLimits = DEFAULT_POOL_LIMITS, trust_env: bool = True, ) -> httpcore.SyncHTTPTransport: ssl_context = SSLConfig(verify=verify, cert=cert, trust_env=trust_env).ssl_context return httpcore.SyncHTTPProxy( proxy_url=proxy.url.raw, proxy_headers=proxy.headers.raw, proxy_mode=proxy.mode, ssl_context=ssl_context, max_keepalive=pool_limits.max_keepalive, max_connections=pool_limits.max_connections, )
def __init__( self, verify: VerifyTypes = True, cert: CertTypes = None, http1: bool = True, http2: bool = False, limits: Limits = DEFAULT_LIMITS, trust_env: bool = True, proxy: Proxy = None, uds: str = None, local_address: str = None, retries: int = 0, backend: str = "sync", ) -> None: ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env) if proxy is None: self._pool = httpcore.SyncConnectionPool( ssl_context=ssl_context, max_connections=limits.max_connections, max_keepalive_connections=limits.max_keepalive_connections, keepalive_expiry=limits.keepalive_expiry, http1=http1, http2=http2, uds=uds, local_address=local_address, retries=retries, backend=backend, ) else: self._pool = httpcore.SyncHTTPProxy( proxy_url=proxy.url.raw, proxy_headers=proxy.headers.raw, proxy_mode=proxy.mode, ssl_context=ssl_context, max_connections=limits.max_connections, max_keepalive_connections=limits.max_keepalive_connections, keepalive_expiry=limits.keepalive_expiry, http2=http2, backend=backend, )
def test_http_proxy(proxy_server: URL, proxy_mode: str) -> None: method = b"GET" url = (b"http", b"example.org", 80, b"/") headers = [(b"host", b"example.org")] max_connections = 1 max_keepalive = 2 with httpcore.SyncHTTPProxy( proxy_server, proxy_mode=proxy_mode, max_connections=max_connections, max_keepalive=max_keepalive, ) as http: http_version, status_code, reason, headers, stream = http.request( method, url, headers) body = read_body(stream) assert http_version == b"HTTP/1.1" assert status_code == 200 assert reason == b"OK"
def test_proxy_https_requests(proxy_server: URL, ca_ssl_context: ssl.SSLContext, proxy_mode: str, http2: bool) -> None: method = b"GET" url = (b"https", b"example.org", 443, b"/") headers = [(b"host", b"example.org")] max_connections = 1 with httpcore.SyncHTTPProxy( proxy_server, proxy_mode=proxy_mode, ssl_context=ca_ssl_context, max_connections=max_connections, http2=http2, ) as http: status_code, headers, stream, ext = http.request(method, url, headers) _ = read_body(stream) assert status_code == 200 assert ext["http_version"] == "HTTP/2" if http2 else "HTTP/1.1" assert ext.get("reason", "") == "" if http2 else "OK"
def _init_proxy_transport( self, proxy: Proxy, verify: VerifyTypes = True, cert: CertTypes = None, http2: bool = False, limits: Limits = DEFAULT_LIMITS, trust_env: bool = True, ) -> httpcore.SyncHTTPTransport: ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env) return httpcore.SyncHTTPProxy( proxy_url=proxy.url.raw, proxy_headers=proxy.headers.raw, proxy_mode=proxy.mode, ssl_context=ssl_context, max_connections=limits.max_connections, max_keepalive_connections=limits.max_keepalive_connections, keepalive_expiry=KEEPALIVE_EXPIRY, http2=http2, )
def test_proxy_https_requests( proxy_server: URL, proxy_mode: str, http2: bool, https_server: Server, ) -> None: method = b"GET" url = (b"https", *https_server.netloc, b"/") headers = [https_server.host_header] max_connections = 1 with httpcore.SyncHTTPProxy( proxy_server, proxy_mode=proxy_mode, max_connections=max_connections, http2=http2, ) as http: status_code, headers, stream, ext = http.request(method, url, headers) _ = read_body(stream) assert status_code == 200 assert ext["http_version"] == "HTTP/2" if http2 else "HTTP/1.1" assert ext.get("reason", "") == "" if http2 else "OK"
def test_http_proxy(proxy_server: URL, proxy_mode: str, backend: str, server: Server) -> None: max_connections = 1 with httpcore.SyncHTTPProxy( proxy_server, proxy_mode=proxy_mode, max_connections=max_connections, backend=backend, ) as http: status_code, headers, stream, extensions = http.handle_request( method=b"GET", url=(b"http", *server.netloc, b"/"), headers=[server.host_header], stream=httpcore.ByteStream(b""), extensions={}, ) read_body(stream) assert status_code == 200 reason_phrase = b"OK" if server.sends_reason else b"" assert extensions == { "http_version": b"HTTP/1.1", "reason_phrase": reason_phrase, }