C# - How to set priority for wildcard request matching in Wiremock.Net

If you are on this page you likely created a wildcard endpoint and now you want to respond differently to a request, but keep the wildcard for other requests. What you are looking for is priority, which makes it possible to handle "overlapping" or "colliding" request paths.

In the below code we make two request matchers. The first is a wildcard path (*) which returns a status code 200 for any path parameter. The second is a specific path (123) which returns a status code 500 on the “same” path, but not using a wildcard. However since our second request matcher has a higher priority (1) it is the one that will be triggered in case both matches:

var server = WireMockServer.Start(58116);

server
    .Given(
        Request.Create()
            .WithPath("/weatherforecastbackend/*")
            .UsingGet()
    )
    .AtPriority(10) //Important part
    .RespondWith(
        Response.Create()
            .WithStatusCode(200)
    );

server
    .Given(
        Request.Create()
            .WithPath("/weatherforecastbackend/123")
            .UsingGet()
    )
    .AtPriority(1) //Important part
    .RespondWith(
        Response.Create()
            .WithStatusCode(500)
    );

var httpClient = new HttpClient();

var firstRequest = await httpClient.GetAsync(
   "http://localhost:58116/weatherforecastbackend/123");
Assert.Equal(HttpStatusCode.InternalServerError, 
   firstRequest.StatusCode);

var secondRequest = await httpClient.GetAsync(
   "http://localhost:58116/weatherforecastbackend/456");
Assert.Equal(HttpStatusCode.OK, 
   secondRequest.StatusCode);

The last part of the above code is our test cases, here we can see that the priority works and we get different results on the two requests. The first request hits the specific requester matcher using /123 and gets an internal server error. The second request hits the wildcard using the path /456 and returns the status code OK.

Alternatively you can avoid wild cards of course! But that is likely not helpful if you are here. That is all there is to it, I hope you enjoyed this post on priority in Wiremock.Net, please let me know what you think in the comments down below!