.nettestingwiremock

How to Handle Dynamic Parameters in Request Object for WireMock API Testing .Net?


I am trying to create a request object with a dynamic parameter for testing an API endpoint using WireMock. Below is my current implementation:

var response = new Response
{
    CustomerId = CustomerReference,
    CustomerEmail = _customerInfo.EmailAddress,
    CustomerName = _customerInfo.FirstName + " " + _customerInfo.LastName,
    RequestDate = DateTime.UtcNow
};

var request = new Request
{
    CustomerEmail = customerEmail,
    CustomerId = customerId,
    CustomerName = customerName,
    RequestReference = "shouldbedynamic",
};

WireMockApiFake
    .Given(Request.Create()
        .WithPath("/api/customer")
        .UsingPost()
        .WithBodyAsJson(request))
    .RespondWith(Response.Create()
        .WithStatusCode(HttpStatusCode.OK)
        .WithBodyAsJson(response));

The issue is that the RequestReference field in the Request object is dynamically generated in the actual code and cannot have a fixed or constant value. I tried using the * symbol as a wildcard for the RequestReference but it did not work.

My Question: How can I make the RequestReference field dynamic in the WireMock setup so that the test can match requests regardless of the exact value of RequestReference?

Any advice or examples would be greatly appreciated!


Solution

  • var request = new Request
    {
        CustomerEmail = customerEmail,
        CustomerId = customerId,
        CustomerName = customerName,
        RequestReference = "*",
    };
    
    WireMockApiFake
            .Given(Request.Create()
                .WithPath("/api/customer")
                .UsingPost()
                .WithBody(new JsonPartialWildcardMatcher(request)))
            .RespondWith(Response.Create()
                .WithStatusCode(HttpStatusCode.OK)
                .WithBodyAsJson(response));
    

    I fix the issue using this implementation.