transport.close()
def __enter__(self: T) -> T:
+ if self._state != ClientState.UNOPENED:
+ msg = {
+ ClientState.OPENED: "Cannot open a client instance more than once.",
+ ClientState.CLOSED: "Cannot reopen a client instance, once it has been closed.",
+ }[self._state]
+ raise RuntimeError(msg)
+
self._state = ClientState.OPENED
self._transport.__enter__()
await proxy.aclose()
async def __aenter__(self: U) -> U:
+ if self._state != ClientState.UNOPENED:
+ msg = {
+ ClientState.OPENED: "Cannot open a client instance more than once.",
+ ClientState.CLOSED: "Cannot reopen a client instance, once it has been closed.",
+ }[self._state]
+ raise RuntimeError(msg)
+
self._state = ClientState.OPENED
await self._transport.__aenter__()
await client.aclose()
assert client.is_closed
+ # Once we're close we cannot make any more requests.
with pytest.raises(RuntimeError):
await client.get("http://example.com")
+ # Once we're closed we cannot reopen the client.
+ with pytest.raises(RuntimeError):
+ async with client:
+ pass # pragma: nocover
+
@pytest.mark.usefixtures("async_environment")
async def test_client_closed_state_using_with_block():
client.close()
assert client.is_closed
+
+ # Once we're close we cannot make any more requests.
with pytest.raises(RuntimeError):
client.get("http://example.com")
+ # Once we're closed we cannot reopen the client.
+ with pytest.raises(RuntimeError):
+ with client:
+ pass # pragma: nocover
+
def test_client_closed_state_using_with_block():
with httpx.Client(transport=httpx.MockTransport(hello_world)) as client: