pytest_httpx icon indicating copy to clipboard operation
pytest_httpx copied to clipboard

Support ignoring query param order if the same parameter is provided more than once

Open victor-h-costa opened this issue 1 year ago • 1 comments

As a question/feature request:

In some cases I usually convert an iterable to a set to avoid duplicates, like for example:

response = httpx.AsyncClient().get(
    "http://localhost:1111/videos",
    params={
        "field": list(set([1, 2, 2]))
    }
)

Which is difficult to test with pytest_httpx since the order of the query parameter field isn't known and doesn't really matter.

Is it possible to add new argument to HTTPXMock.add_response to ignore query param order if the same parameter is provided more than once?

# **Suggestion 1**
httpx_mock.add_response(
    method="GET",
    url="http://localhost:1111/videos?field=1&field=2",
    status_code=200,
    ignore_query_param_value_order=True,
)

# **Suggestion 2**
httpx_mock.add_response(
    method="GET",
    url="http://localhost:1111/videos?field=1&field=2",
    status_code=200,
    ignore_query_param_value_order=["field"],
)

Thanks

victor-h-costa avatar Sep 23 '24 14:09 victor-h-costa

Hello @victor-h-costa

Order of parameters potentially matters in the API you are reaching out. This looks however like a valid use case but I don't know when and how I will tackle this.

In the meantime, you have the 2 possible solutions:

  • Use a regex as matching URL (can be quite complex to read)
  • Use an ordered list when you send your parameters (requires to change your code for testing purpose)

Colin-b avatar Sep 23 '24 14:09 Colin-b