Skip to content

Commit 6665733

Browse files
committed
fix(declarative): preserve all bodyless response semantics
Co-authored-by: Copilot <[email protected]> Copilot-Session: 9015c274-1215-4163-94db-643befd21f8d
1 parent 5926ef8 commit 6665733

3 files changed

Lines changed: 26 additions & 7 deletions

File tree

‎python/packages/declarative/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ and supports identity, gzip, and deflate
5454
responses, including up to four encoding stages. Unsupported encodings,
5555
incomplete encoded bodies, and trailing encoded data raise `httpx.DecodingError`.
5656
Empty encoded responses are validated unless the response is bodyless (HEAD,
57-
204, 205, or 304). Deflate input is retained within the same bound until its wrapped
58-
or raw format can be validated.
57+
1xx, 204, 205, 304, or a successful CONNECT response). Deflate input is retained
58+
within the same bound until its wrapped or raw format can be validated.
5959
Explicit request `Accept-Encoding` headers and client values that differ from
6060
HTTPX's generated default are preserved but do not enable additional decoders.
6161
Caller-supplied response hooks and transports must not

‎python/packages/declarative/agent_framework_declarative/_workflows/_http_handler.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,12 @@ async def _read_bounded_response(response: httpx.Response, limit: int) -> None:
370370
for encoding in response.headers.get("content-encoding", "").split(",")
371371
if encoding.strip().lower() != "identity" and encoding.strip()
372372
]
373-
if response.request.method == "HEAD" or response.status_code in {204, 205, 304}:
373+
if (
374+
response.request.method == "HEAD"
375+
or 100 <= response.status_code < 200
376+
or response.status_code in {204, 205, 304}
377+
or (response.request.method == "CONNECT" and 200 <= response.status_code < 300)
378+
):
374379
encodings = []
375380
if len(encodings) > 4:
376381
raise httpx.DecodingError("HTTP response has too many content encoding stages.", request=response.request)

‎python/packages/declarative/tests/test_default_http_request_handler.py‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,20 @@ async def __aiter__(self) -> AsyncIterator[bytes]:
998998
assert stream.closed
999999
assert not client.is_closed
10001000

1001-
@pytest.mark.parametrize(("method", "status"), [("HEAD", 200), ("GET", 204), ("GET", 205), ("GET", 304)])
1001+
@pytest.mark.parametrize(
1002+
("method", "status"),
1003+
[
1004+
("HEAD", 200),
1005+
("GET", 100),
1006+
("GET", 101),
1007+
("GET", 199),
1008+
("GET", 204),
1009+
("GET", 205),
1010+
("GET", 304),
1011+
("CONNECT", 200),
1012+
("CONNECT", 299),
1013+
],
1014+
)
10021015
async def test_empty_bodyless_response_does_not_require_encoded_body(self, method: str, status: int) -> None:
10031016
stream = _ResponseStream([])
10041017
response = httpx.Response(status, headers={"Content-Encoding": "gzip"}, stream=stream)
@@ -1011,15 +1024,16 @@ async def test_empty_bodyless_response_does_not_require_encoded_body(self, metho
10111024
assert stream.closed
10121025

10131026
@pytest.mark.parametrize("encoding", ["gzip", "deflate"])
1014-
async def test_empty_encoded_get_response_is_rejected(self, encoding: str) -> None:
1027+
@pytest.mark.parametrize(("method", "status"), [("GET", 200), ("CONNECT", 300)])
1028+
async def test_empty_encoded_response_with_body_is_rejected(self, encoding: str, method: str, status: int) -> None:
10151029
stream = _ResponseStream([])
1016-
response = httpx.Response(200, headers={"Content-Encoding": encoding}, stream=stream)
1030+
response = httpx.Response(status, headers={"Content-Encoding": encoding}, stream=stream)
10171031
async with (
10181032
httpx.AsyncClient(transport=httpx.MockTransport(lambda request: response)) as client,
10191033
DefaultHttpRequestHandler(client=client) as handler,
10201034
):
10211035
with pytest.raises(httpx.DecodingError, match="Incomplete"):
1022-
await handler.send(HttpRequestInfo(method="GET", url="https://api.example.test/items"))
1036+
await handler.send(HttpRequestInfo(method=method, url="https://api.example.test/items"))
10231037
assert stream.closed
10241038

10251039
@pytest.mark.parametrize("chunk_size", [1, 166])

0 commit comments

Comments
 (0)