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

Closed
opened 2025-12-29 08:29:47 +01:00 by adam · 4 comments
Owner

Originally created by @basdijkstra on GitHub (Aug 12, 2023).

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.

Originally created by @basdijkstra on GitHub (Aug 12, 2023). So, I'm running this test using [RestAssured.Net](https://github.com/basdijkstra/rest-assured-net): ```csharp [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](https://github.com/WireMock-Net/WireMock.Net/assets/10740451/0edc87fe-06b1-40ba-a572-77940fdabb12) And here's the WireMock.Net stub definition I expect to match this request: ```csharp 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: ```csharp 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.
adam added the question label 2025-12-29 08:29:47 +01:00
adam closed this issue 2025-12-29 08:29:47 +01:00
Author
Owner

@StefH commented on GitHub (Aug 12, 2023):

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)
            );
@StefH commented on GitHub (Aug 12, 2023): The easiest solution is to use `WithParam` (I actually don't know if that path you use will work) ``` c# 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) ); ```
Author
Owner

@basdijkstra commented on GitHub (Aug 12, 2023):

That matches

id=1&id=2&id=3

not

id=1,2,3

I’m using that in a different test case here: a75c483c08/RestAssured.Net.Tests/QueryParameterTests.cs (L230)

@basdijkstra commented on GitHub (Aug 12, 2023): 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
Author
Owner

@StefH commented on GitHub (Aug 12, 2023):

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.

@StefH commented on GitHub (Aug 12, 2023): 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.
Author
Owner

@basdijkstra commented on GitHub (Aug 12, 2023):

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

16a2326de0

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

@basdijkstra commented on GitHub (Aug 12, 2023): 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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#541