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

Code before:
    data = b"Echo request body"

    async with httpx.AsyncClient() as client:
        response = await client.post(
            server.url.copy_with(path="/echo_body"), headers=headers, data=data
        )

    assert response.status_code == 200
    assert response.content == data


@pytest.mark.usefixtures("async_environment")
async def test_context_managed_transport():
    class Transport(httpcore.AsyncHTTPTransport):
        def __init__(self):
            self.events = []

        async def aclose(self):
            self.events.append("transport.aclose")

        async def __aenter__(self):
            await super().__aenter__()
            self.events.append("transport.__aenter__")

        async def __aexit__(self):
            await super().__aexit__()
            self.events.append("transport.__aexit__")

    # Note that we're including 'proxies' here to *also* run through the
    # proxy context management, although we can't easily test that at the
    # moment, since we can't add proxies as transport instances.
    #
    # Once we have a more generalised Mount API we'll be able to remove this
    # in favour of ensuring all mounts are context managed, which will
    # also neccessarily include proxies.
    transport = Transport()
    async with httpx.AsyncClient(transport=transport, proxies="http://www.example.com"):
        pass

    assert transport.events == [
        "transport.__aenter__",
        "transport.aclose",
        "transport.__aexit__",
    ]


Review comment (suggestion):
```suggestion
        async def __aexit__(self, *args):
            await super().__aexit__(*args)
```

Code after:
    data = b"Echo request body"

    async with httpx.AsyncClient() as client:
        response = await client.post(
            server.url.copy_with(path="/echo_body"), headers=headers, data=data
        )

    assert response.status_code == 200
    assert response.content == data


@pytest.mark.usefixtures("async_environment")
async def test_context_managed_transport():
    class Transport(httpcore.AsyncHTTPTransport):
        def __init__(self):
            self.events = []

        async def aclose(self):
            # The base implementation of httpcore.AsyncHTTPTransport just
            # calls into `.aclose`, so simple transport cases can just override
            # this method for any cleanup, where more complex cases
            # might want to additionally override `__aenter__`/`__aexit__`.
            self.events.append("transport.aclose")

        async def __aenter__(self):
            await super().__aenter__()
            self.events.append("transport.__aenter__")

        async def __aexit__(self, *args):
            await super().__aexit__(*args)
            self.events.append("transport.__aexit__")

    # Note that we're including 'proxies' here to *also* run through the
    # proxy context management, although we can't easily test that at the
    # moment, since we can't add proxies as transport instances.
    #
    # Once we have a more generalised Mount API we'll be able to remove this
    # in favour of ensuring all mounts are context managed, which will
    # also neccessarily include proxies.
    transport = Transport()
    async with httpx.AsyncClient(transport=transport, proxies="http://www.example.com"):
        pass

    assert transport.events == [
        "transport.__aenter__",
        "transport.aclose",
        "transport.__aexit__",
    ]