Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie committed May 25, 2023
2 parents f77e889 + 7335950 commit 073df47
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased

### Added

* Add `socket_options` argument to `httpx.HTTPTransport` and `httpx.AsyncHTTPTransport` classes. (#2716)

### Fixed

* Return `500` error response instead of exceptions when `raise_app_exceptions=False` is set on `ASGITransport`. (#2669)
* Ensure all WSGITransport environs have a SERVER_PROTOCOL. (#2708)

## 0.24.1 (17th May, 2023)

### Added
Expand Down
8 changes: 7 additions & 1 deletion httpx/_transports/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,15 @@ async def send(message: typing.Dict[str, typing.Any]) -> None:
try:
await self.app(scope, receive, send)
except Exception: # noqa: PIE-786
if self.raise_app_exceptions or not response_complete.is_set():
if self.raise_app_exceptions:
raise

response_complete.set()
if status_code is None:
status_code = 500
if response_headers is None:
response_headers = {}

assert response_complete.is_set()
assert status_code is not None
assert response_headers is not None
Expand Down
12 changes: 12 additions & 0 deletions httpx/_transports/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
T = typing.TypeVar("T", bound="HTTPTransport")
A = typing.TypeVar("A", bound="AsyncHTTPTransport")

SOCKET_OPTION = typing.Union[
typing.Tuple[int, int, int],
typing.Tuple[int, int, typing.Union[bytes, bytearray]],
typing.Tuple[int, int, None, int],
]


@contextlib.contextmanager
def map_httpcore_exceptions() -> typing.Iterator[None]:
Expand Down Expand Up @@ -122,6 +128,7 @@ def __init__(
uds: typing.Optional[str] = None,
local_address: typing.Optional[str] = None,
retries: int = 0,
socket_options: typing.Optional[typing.Iterable[SOCKET_OPTION]] = None,
) -> None:
ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)

Expand All @@ -136,6 +143,7 @@ def __init__(
uds=uds,
local_address=local_address,
retries=retries,
socket_options=socket_options,
)
elif proxy.url.scheme in ("http", "https"):
self._pool = httpcore.HTTPProxy(
Expand All @@ -153,6 +161,7 @@ def __init__(
keepalive_expiry=limits.keepalive_expiry,
http1=http1,
http2=http2,
socket_options=socket_options,
)
elif proxy.url.scheme == "socks5":
try:
Expand Down Expand Up @@ -257,6 +266,7 @@ def __init__(
uds: typing.Optional[str] = None,
local_address: typing.Optional[str] = None,
retries: int = 0,
socket_options: typing.Optional[typing.Iterable[SOCKET_OPTION]] = None,
) -> None:
ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)

Expand All @@ -271,6 +281,7 @@ def __init__(
uds=uds,
local_address=local_address,
retries=retries,
socket_options=socket_options,
)
elif proxy.url.scheme in ("http", "https"):
self._pool = httpcore.AsyncHTTPProxy(
Expand All @@ -288,6 +299,7 @@ def __init__(
keepalive_expiry=limits.keepalive_expiry,
http1=http1,
http2=http2,
socket_options=socket_options,
)
elif proxy.url.scheme == "socks5":
try:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ classifiers = [
]
dependencies = [
"certifi",
"httpcore>=0.15.0,<0.18.0",
"httpcore>=0.17.2,<0.18.0",
"idna",
"sniffio",
]
Expand Down
10 changes: 10 additions & 0 deletions tests/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

import httpx
from httpx import ASGITransport


async def hello_world(scope, receive, send):
Expand Down Expand Up @@ -191,3 +192,12 @@ async def read_body(scope, receive, send):

assert response.status_code == 200
assert disconnect


@pytest.mark.anyio
async def test_asgi_exc_no_raise():
transport = ASGITransport(app=raise_exc, raise_app_exceptions=False)
async with httpx.AsyncClient(transport=transport) as client:
response = await client.get("http://www.example.org/")

assert response.status_code == 500

0 comments on commit 073df47

Please sign in to comment.