0

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!

2 Answers 2

0
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.

Sign up to request clarification or add additional context in comments.

Comments

0

You don't need to set the request body explicitly and using the .UsingPost() is enough, if you want to use some of the values in the request in the response model then you should use the JsonPath.SelectToken structure.

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 = "can be anything on the fly",
};

WireMockApiFake
    .Given(Request.Create()
        .WithPath("/api/customer")
        .UsingPost())// no need to set .WithBodyAsJson(request)
    .RespondWith(Response.Create()
        .WithStatusCode(HttpStatusCode.OK)
        .WithBodyAsJson(new Response
            {
                CustomerId = "{{JsonPath.SelectToken request.body \"$.requestReference\"}}",
                CustomerEmail = "{{JsonPath.SelectToken request.body \"$.customerEmail\"}}",
                CustomerName = _customerInfo.FirstName + " " + _customerInfo.LastName,
                RequestDate = DateTime.UtcNow
            }));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.