Skip to content

Commit 262b720

Browse files
Add local_address support for curio
1 parent 2a1c6ef commit 262b720

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

httpcore/_backends/curio.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,13 @@ async def open_tcp_stream(
155155
OSError: ConnectError,
156156
}
157157
host = hostname.decode("ascii")
158-
kwargs = (
159-
{} if ssl_context is None else {"ssl": ssl_context, "server_hostname": host}
160-
)
158+
159+
kwargs: dict = {}
160+
if ssl_context is not None:
161+
kwargs["ssl"] = ssl_context
162+
kwargs["server_hostname"] = host
163+
if local_address is not None:
164+
kwargs["source_addr"] = (local_address, 0)
161165

162166
with map_exceptions(exc_map):
163167
sock: curio.io.Socket = await curio.timeout_after(

tests/async_tests/test_interfaces.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import httpcore
77
from httpcore._types import URL
88
from tests.conftest import Server
9+
from tests.utils import current_async_library
910

1011

1112
async def read_body(stream: httpcore.AsyncByteStream) -> bytes:
@@ -210,9 +211,11 @@ async def test_http_proxy(proxy_server: URL, proxy_mode: str) -> None:
210211
assert reason == b"OK"
211212

212213

213-
@pytest.mark.asyncio
214-
# This doesn't run with trio, since trio doesn't support local_address.
214+
@pytest.mark.usefixtures("async_environment")
215215
async def test_http_request_local_address() -> None:
216+
if current_async_library() == "trio":
217+
pytest.skip("trio does not support local_address.")
218+
216219
async with httpcore.AsyncConnectionPool(local_address="0.0.0.0") as http:
217220
method = b"GET"
218221
url = (b"http", b"example.org", 80, b"/")

tests/sync_tests/test_interfaces.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import httpcore
77
from httpcore._types import URL
88
from tests.conftest import Server
9+
from tests.utils import current_async_library
910

1011

1112
def read_body(stream: httpcore.SyncByteStream) -> bytes:
@@ -211,8 +212,10 @@ def test_http_proxy(proxy_server: URL, proxy_mode: str) -> None:
211212

212213

213214

214-
# This doesn't run with trio, since trio doesn't support local_address.
215215
def test_http_request_local_address() -> None:
216+
if current_async_library() == "trio":
217+
pytest.skip("trio does not support local_address.")
218+
216219
with httpcore.SyncConnectionPool(local_address="0.0.0.0") as http:
217220
method = b"GET"
218221
url = (b"http", b"example.org", 80, b"/")

tests/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import sniffio
2+
3+
4+
def current_async_library() -> str:
5+
try:
6+
library = sniffio.current_async_library()
7+
except Exception:
8+
return "sync"
9+
else:
10+
return library

0 commit comments

Comments
 (0)