diff --git a/.chronus/changes/python-response-body-or-no-content-2026-10-12-3-14-0.md b/.chronus/changes/python-response-body-or-no-content-2026-10-12-3-14-0.md new file mode 100644 index 00000000000..3aa81d6331b --- /dev/null +++ b/.chronus/changes/python-response-body-or-no-content-2026-10-12-3-14-0.md @@ -0,0 +1,7 @@ +--- +changeKind: internal +packages: + - "@typespec/http-client-python" +--- + +Add sync and async Spector tests for responses with a model body or no content, including raw response status and headers. diff --git a/cspell.yaml b/cspell.yaml index e16c4c437b1..328c0f73b33 100644 --- a/cspell.yaml +++ b/cspell.yaml @@ -40,6 +40,7 @@ words: - blib - blockful - blockless + - bodyornocontent - buongiorno - cadl - cadleditor diff --git a/packages/http-client-python/tests/mock_api/shared/asynctests/test_response_body_or_no_content_async.py b/packages/http-client-python/tests/mock_api/shared/asynctests/test_response_body_or_no_content_async.py new file mode 100644 index 00000000000..ce1608e9890 --- /dev/null +++ b/packages/http-client-python/tests/mock_api/shared/asynctests/test_response_body_or_no_content_async.py @@ -0,0 +1,35 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +import pytest +import pytest_asyncio +from response.bodyornocontent.aio import BodyOrNoContentClient +from response.bodyornocontent.models import BlobLayout + + +@pytest_asyncio.fixture +async def client(): + async with BodyOrNoContentClient(endpoint="http://localhost:3000") as client: + yield client + + +@pytest.mark.asyncio +async def test_get_body(client: BodyOrNoContentClient): + result = await client.get_body() + assert result == BlobLayout(content="hello") + + response = await client.get_body(cls=lambda pipeline_response, _, __: pipeline_response.http_response) + assert response.status_code == 200 + assert response.headers["x-ms-request-id"] == "body-request" + + +@pytest.mark.asyncio +async def test_get_no_content(client: BodyOrNoContentClient): + result = await client.get_no_content() + assert result is None + + response = await client.get_no_content(cls=lambda pipeline_response, _, __: pipeline_response.http_response) + assert response.status_code == 204 + assert response.headers["x-ms-request-id"] == "no-content-request" diff --git a/packages/http-client-python/tests/mock_api/shared/test_response_body_or_no_content.py b/packages/http-client-python/tests/mock_api/shared/test_response_body_or_no_content.py new file mode 100644 index 00000000000..38c76ffaeaf --- /dev/null +++ b/packages/http-client-python/tests/mock_api/shared/test_response_body_or_no_content.py @@ -0,0 +1,32 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +import pytest +from response.bodyornocontent import BodyOrNoContentClient +from response.bodyornocontent.models import BlobLayout + + +@pytest.fixture +def client(): + with BodyOrNoContentClient(endpoint="http://localhost:3000") as client: + yield client + + +def test_get_body(client: BodyOrNoContentClient): + result = client.get_body() + assert result == BlobLayout(content="hello") + + response = client.get_body(cls=lambda pipeline_response, _, __: pipeline_response.http_response) + assert response.status_code == 200 + assert response.headers["x-ms-request-id"] == "body-request" + + +def test_get_no_content(client: BodyOrNoContentClient): + result = client.get_no_content() + assert result is None + + response = client.get_no_content(cls=lambda pipeline_response, _, __: pipeline_response.http_response) + assert response.status_code == 204 + assert response.headers["x-ms-request-id"] == "no-content-request"