def test_url(): url = httpcore.URL("https://www.example.com/") assert url == httpcore.URL(scheme="https", host="www.example.com", port=None, target="/") assert bytes(url) == b"https://www.example.com/"
def test_url_with_port(): url = httpcore.URL("https://www.example.com:443/") assert url == httpcore.URL(scheme="https", host="www.example.com", port=443, target="/") assert bytes(url) == b"https://www.example.com:443/"
def test_url_cannot_include_unicode_strings(): """ URLs instantiated with strings outside of the plain ASCII range are disallowed, but the explicit style allows for these ambiguous cases to be precisely expressed. """ with pytest.raises(TypeError) as exc_info: httpcore.URL("https://www.example.com/☺") assert str( exc_info.value) == "url strings may not include unicode characters." httpcore.URL(scheme=b"https", host=b"www.example.com", target="/☺".encode("utf-8"))
def test_get(server): url = "http://127.0.0.1:8000/" with httpcore.Client() as http: response = http.get(url) assert response.status_code == 200 assert response.url == httpcore.URL(url) assert response.content == b"Hello, world!" assert response.text == "Hello, world!" assert response.protocol == "HTTP/1.1" assert response.encoding == "iso-8859-1" assert response.request.url == httpcore.URL(url) assert response.headers assert response.is_redirect is False assert repr(response) == "<Response(200, 'OK')>"
async def handle_async_request( self, request: Request, ) -> Response: assert isinstance(request.stream, AsyncByteStream) req = httpcore.Request( method=request.method, url=httpcore.URL( scheme=request.url.raw_scheme, host=request.url.raw_host, port=request.url.port, target=request.url.raw_path, ), headers=request.headers.raw, content=request.stream, extensions=request.extensions, ) with map_httpcore_exceptions(): resp = await self._pool.handle_async_request(req) assert isinstance(resp.stream, typing.AsyncIterable) return Response( status_code=resp.status, headers=resp.headers, stream=AsyncResponseStream(resp.stream), extensions=resp.extensions, )
def url_to_origin(url: str): """ Given a URL string, return the origin in the raw tuple format that `httpcore` uses for it's representation. """ scheme, host, port = httpx.URL(url).raw[:3] return httpcore.URL(scheme=scheme, host=host, port=port, target="/")
async def request( self, method, url, headers=(), body=b"", stream=False, ssl=None, timeout=None, ): if ssl is None: ssl = self.ssl_config if timeout is None: timeout = self.timeout parsed_url = httpcore.URL(url) request = httpcore.Request( method, parsed_url, headers=headers, body=body ) connection = await self.acquire_connection( parsed_url, ssl=ssl, timeout=timeout ) if self._request_delay: print(f"\t>> Sleeping ({self._request_delay})") await asyncio.sleep(self._request_delay) response = await connection.send(request) if not stream: try: await response.read() finally: await response.close() return response
def test_request(): request = httpcore.Request("GET", "https://www.example.com/") assert request.method == b"GET" assert request.url == httpcore.URL("https://www.example.com/") assert request.headers == [] assert request.extensions == {} assert repr(request) == "<Request [b'GET']>" assert ( repr(request.url) == "URL(scheme=b'https', host=b'www.example.com', port=None, target=b'/')" ) assert repr(request.stream) == "<ByteStream [0 bytes]>"
async def test_httpcore_request(url, port): async with MockRouter(using="httpcore") as router: router.get(url) % dict(text="foobar") request = httpcore.Request( b"GET", httpcore.URL(scheme=b"https", host=b"foo.bar", port=port, target=b"/"), ) with httpcore.ConnectionPool() as http: response = http.handle_request(request) body = response.read() assert body == b"foobar" async with httpcore.AsyncConnectionPool() as http: response = await http.handle_async_request(request) body = await response.aread() assert body == b"foobar"
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, ) -> None: ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env) if proxy is None: self._pool = httpcore.AsyncConnectionPool( 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, ) else: self._pool = httpcore.AsyncHTTPProxy( proxy_url=httpcore.URL( scheme=proxy.url.raw_scheme, host=proxy.url.raw_host, port=proxy.url.port, target=proxy.url.raw_path, ), proxy_headers=proxy.headers.raw, ssl_context=ssl_context, max_connections=limits.max_connections, max_keepalive_connections=limits.max_keepalive_connections, keepalive_expiry=limits.keepalive_expiry, )
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, ) -> None: ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env) if proxy is None: self._pool = httpcore.AsyncConnectionPool( 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, ) elif proxy.url.scheme in ("http", "https"): self._pool = httpcore.AsyncHTTPProxy( proxy_url=httpcore.URL( scheme=proxy.url.raw_scheme, host=proxy.url.raw_host, port=proxy.url.port, target=proxy.url.raw_path, ), proxy_auth=proxy.raw_auth, proxy_headers=proxy.headers.raw, 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, ) elif proxy.url.scheme == "socks5": try: import socksio # noqa except ImportError: # pragma: nocover raise ImportError( "Using SOCKS proxy, but the 'socksio' package is not installed. " "Make sure to install httpx using `pip install httpx[socks]`." ) from None self._pool = httpcore.AsyncSOCKSProxy( proxy_url=httpcore.URL( scheme=proxy.url.raw_scheme, host=proxy.url.raw_host, port=proxy.url.port, target=proxy.url.raw_path, ), proxy_auth=proxy.raw_auth, 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, ) else: # pragma: nocover raise ValueError( f"Proxy protocol must be either 'http', 'https', or 'socks5', but got {proxy.url.scheme!r}." )
def test_url_with_invalid_argument(): with pytest.raises(TypeError) as exc_info: httpcore.URL(123) # type: ignore assert str(exc_info.value) == "url must be bytes or str, but got int."