Repository: encode/httpx
File: tests/client/test_async_client.py (Python)

Code before:
        response = await client.get("https://www.example.com")
        assert response.status_code == 200
        assert response.json() == {"app": "unmounted"}

        response = await client.get("custom://www.example.com")
        assert response.status_code == 200
        assert response.json() == {"app": "mounted"}


@pytest.mark.usefixtures("async_environment")
async def test_response_aclose_map_exceptions():
    class BrokenStream:
        async def __aiter__(self):
            # so we're an AsyncIterator
            pass  # pragma: nocover

        async def aclose(self):
            raise httpcore.CloseError(OSError(104, "Connection reset by peer"))

    def handle(request: httpx.Request) -> httpx.Response:
        return httpx.Response(200, stream=BrokenStream())

    async with httpx.AsyncClient(transport=httpx.MockTransport(handle)) as client:
        async with client.stream("GET", "http://example.com") as response:
            with pytest.raises(httpx.CloseError):
                await response.aclose()


Review comment (bug):
Am I correct saying this would also reproduce for a regular `client.request()`? If so, should we update this to…?

```suggestion
        with pytest.raises(httpx.CloseError):
            await client.request("GET", "http://example.com")
    
        with pytest.raises(httpx.CloseError):
            async with client.stream("GET", "http://example.com"):
                pass  # Close on exit.
```

(Also dropped the explicit `response.aclose()` since the expected behavior is that exiting the `stream()` async context manager would do that for us, and fail with the proper exception.)

Code after:
        response = await client.get("https://www.example.com")
        assert response.status_code == 200
        assert response.json() == {"app": "unmounted"}

        response = await client.get("custom://www.example.com")
        assert response.status_code == 200
        assert response.json() == {"app": "mounted"}


@pytest.mark.usefixtures("async_environment")
async def test_response_aclose_map_exceptions():
    class BrokenStream:
        async def __aiter__(self):
            # so we're an AsyncIterator
            pass  # pragma: nocover

        async def aclose(self):
            raise httpcore.CloseError(OSError(104, "Connection reset by peer"))

    def handle(request: httpx.Request) -> httpx.Response:
        return httpx.Response(200, stream=BrokenStream())

    async with httpx.AsyncClient(transport=httpx.MockTransport(handle)) as client:
        async with client.stream("GET", "http://example.com") as response:
            with pytest.raises(httpx.CloseError):
                await response.aclose()


@pytest.mark.usefixtures("async_environment")
async def test_async_mock_transport():
    async def hello_world(request):
        return httpx.Response(200, text="Hello, world!")

    transport = httpx.MockTransport(hello_world)

    async with httpx.AsyncClient(transport=transport) as client:
        response = await client.get("https://www.example.com")
        assert response.status_code == 200
        assert response.text == "Hello, world!"