Mocking the Post Method on an Async HTTPX Post Request
A quick snippet for mocking HTTP requests with an the async httpx context manager.
async with httpx.AsyncClient() as client:
signup_response = await client.post(
f"https://{AUTH0_DOMAIN}/api/v2/users",
data=signup_request_body,
headers=signup_request_headers,
timeout=30,
)
Quick and simple post for getting at the method that you want to patch
@patch("httpx.AsyncClient"). # The client to patch
async def test_auth0_post_request_example(
mock_httpx_client,
):
mock_httpx_client.return_value.__aenter__.return_value.post.return_value = Response(
201,
json={
"user_id": "fake_user_id",
"username": "fake_user_name",
"email": "fake_email",
},
)
- mock_httpx_client (The patched uninitialised class of
httpx.AsyncClient
) - return_value (The class that is returned when initialised) this is the
()
- aenter is the
async with
portion of the code. - return_value is the
as client:
portion - post (the
post
function) - return_value is the invoked return value from
post
so the()
part of the function call.
Hopefully that makes sense!