WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.36k stars 199 forks source link

How can I match on query parameters with comma-separated values? #991

Closed basdijkstra closed 10 months ago

basdijkstra commented 10 months ago

So, I'm running this test using RestAssured.Net:

[Test]
 public void SingleQueryParameterWithCommaSeparatedValuesCanBeSpecified()
{
    this.CreateStubForSingleQueryParameterWithCommaSeparatedValues();

    Given()
        .QueryParam("id", string.Join(",", 1, 2, 3))
        .When()
        .Get($"{MOCK_SERVER_BASE_URL}/api/single-query-param-csv")
        .Then()
        .StatusCode(200);
}

This is the HTTP call that is being made:

image

And here's the WireMock.Net stub definition I expect to match this request:

private void CreateStubForSingleQueryParameterWithCommaSeparatedValues()
{
    string value = string.Join(",", 1, 2, 3);

    Console.WriteLine($"VALUE: {value}");

    this.Server?.Given(Request.Create()
        .WithPath("/api/single-query-param-csv")
        .WithParam("id", value)
        .UsingGet())
        .RespondWith(Response.Create()
        .WithStatusCode(200));
}

However, when I run the test, WireMock.Net returns a 404, not a 200, so for some reason my request isn't properly matched. Any ideas?

This doesn't work either:

private void CreateStubForSingleQueryParameterWithCommaSeparatedValues()
{
    this.Server?.Given(Request.Create()
        .WithPath("/api/single-query-param-csv?id=1,2,3")
        .UsingGet())
        .RespondWith(Response.Create()
        .WithStatusCode(200));
}

I'm using version 1.5.34, i.e., the latest stable at the time of posting this question.

StefH commented 10 months ago

The easiest solution is to use WithParam (I actually don't know if that path you use will work)

var server = WireMockServer.Start();
        server.Given(
                Request.Create()
                    .UsingGet()
                    .WithPath("/api/single-query-param-csv")
                    .WithParam("id", "1", "2", "3")
            )
            .RespondWith(
                Response.Create().WithStatusCode(200)
            );
basdijkstra commented 10 months ago

That matches

id=1&id=2&id=3

not

id=1,2,3

I’m using that in a different test case here: https://github.com/basdijkstra/rest-assured-net/blob/a75c483c080c5bc352f5cb6ba2db0999a0a56964/RestAssured.Net.Tests/QueryParameterTests.cs#L230

StefH commented 10 months ago

It actually matches both.

See https://github.com/WireMock-Net/WireMock.Net/pull/992

If you only want exactly match on the the string "1,2,3" you need to take a look in that other unit test in the same file which I changed in that PR.

basdijkstra commented 10 months ago

Ah, I didn't know it matched both. Updated my tests now and you're absolutely right:

https://github.com/basdijkstra/rest-assured-net/commit/16a2326de025b3ac3703ff6dd67098bd086f1a78

Thanks for the quick response! Closing this now as my question has been addressed.