pytest_httpx icon indicating copy to clipboard operation
pytest_httpx copied to clipboard

The following responses are mocked but not requested

Open jonathan-s opened this issue 1 year ago • 5 comments

Instead of showing an object that looks like the one below, it would be better to have an object that is human readable.

E           AssertionError: The following responses are mocked but not requested:
E             Match all requests
E           assert not [<pytest_httpx._httpx_mock._RequestMatcher object at 0x10e1f6a60>]

jonathan-s avatar Apr 09 '24 12:04 jonathan-s

Seems like this is possibly an edgecase when the url and everything is else is empty.

jonathan-s avatar Apr 09 '24 14:04 jonathan-s

Hello @jonathan-s Can you provide a sample to reproduce? Thanks again

Colin-b avatar Apr 10 '24 06:04 Colin-b

I am having a similar problem. The following example works with pytest-httpx 0.26 but fails with pytest-httpx 0.30:

import fastapi
import httpx
import pytest
import pytest_asyncio
import pytest_httpx


@pytest_asyncio.fixture
async def client() -> fastapi.FastAPI:
    app = fastapi.FastAPI()

    @app.get("/")
    def get() -> None:
        return None

    async with httpx.AsyncClient(
        transport=httpx.ASGITransport(app=app),
        base_url="http://test",
    ) as api_client:
        yield api_client


@pytest.mark.asyncio
async def test_ok(client: httpx.AsyncClient) -> None:
    response = await client.get("/")
    assert response.status_code == 200


@pytest.mark.asyncio
async def test_err(
    client: httpx.AsyncClient, httpx_mock: pytest_httpx.HTTPXMock
) -> None:
    httpx_mock.add_response(status_code=500, text="Internal Server Error")
    response = await client.get("/")
    assert response.status_code == 500

I think the reason is that both, the Starlette text client and pytest-httpx, try to modify the HTTPX transport.

sscherfke avatar Apr 23 '24 09:04 sscherfke

httpx.ASGITransport inherits httpx.AsyncBaseTransport and pytest-httpx only mocks httpx.AsyncHTTPTransport.

I don't think it would be a good idea to mock the base transport, because its handle-Method is "abstract" and should remain so.

Maybe it should mock the asgi/wsgi transport in addition to the http transport or allow passing a list of transport classes to mock.

sscherfke avatar Apr 23 '24 10:04 sscherfke

The Starlette test client inherts httpx.BaseTransport: https://github.com/encode/starlette/blob/master/starlette/testclient.py#L239

sscherfke avatar Apr 23 '24 10:04 sscherfke

Hello @sscherfke , this does not seem to be related at all. Could you open an issue describing your need if any?

Colin-b avatar Sep 20 '24 07:09 Colin-b

As for your issue @jonathan-s,

The display performed by pytest-httpx is:

AssertionError: The following responses are mocked but not requested:
E             Match all requests

The one from pytest is:

assert not [<pytest_httpx._httpx_mock._RequestMatcher object at 0x10e1f6a60>]

So this is the best we can do here, the error is indeed the one you described. You put a request matcher matching every incoming request while none was issued.

If you still feel like this is not clear enough feel free to reopen with a suggestion on what could be displayed for more clarity.

Thanks again

Colin-b avatar Sep 20 '24 07:09 Colin-b