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.39k stars 207 forks source link

WireMock.Net.Client Request Builder Matchers #1115

Closed hmiguel closed 3 months ago

hmiguel commented 3 months ago

Hello,

I'm trying to replicate the following server mapping using wiremock.net rest client.

var server = WireMockServer.Start();
server.Given(WireMock.RequestBuilders.Request.Create()
    .WithPath("/test1")
    .UsingPost()
    .WithBody(new JmesPathMatcher("things.name == 'RequiredThing'")))
    .RespondWith(
           WireMock.ResponseBuilders.Response.Create()
           .WithHeader("Content-Type", "application/json")
           .WithStatusCode(200)
           .WithDelay(TimeSpan.FromMilliseconds(50))
    );

Right now, I successfully made it using @R0boC0p extension from here. It look like this:


var api = RestClient.For<IWireMockAdminApi>("http://localhost:2080");
var builder = api.MappingBuilder();
builder.Given(WireMock.RequestBuilders.Request.Create()
        .WithPath("/test1")
        .UsingPost()
        .WithBody(new JmesPathMatcher("things.name == 'RequiredThing'")))
        .RespondWith(
            WireMock.ResponseBuilders.Response.Create()
           .WithHeader("Content-Type", "application/json")
           .WithStatusCode(200)
           .WithDelay(TimeSpan.FromMilliseconds(50))
    );
await builder.BuildAndSendAsync();

My question is: is there any built-in way to do it?

R0boC0p commented 3 months ago

I am not sure if I can follow correctly, but if you are intending to have a builder for the admin-interface, this has been added by the link you posted, and there is no extension code (the one that I proposed) needed, as it's now working out of the box.

What is your exact issue, as your code seems to be just fine?

from what I can see the only difference here is that the example code has

   var result = await mappingBuilder.BuildAndPostAsync().ConfigureAwait(false);

Cheers.

hmiguel commented 3 months ago

My issue is that you don't have an api.MappingBuilder(), only a api.GetMappingBuilder();

And then, api.GetMappingBuilder() doesn't have the same syntax, neither access to WithBody(IMatcher matcher), i.e..

Best

StefH commented 3 months ago

For an example on the mapping builder: see https://github.com/WireMock-Net/WireMock.Net/blob/master/examples/WireMock.Net.Client/Program.cs#L22

hmiguel commented 3 months ago

@StefH I can't see any example with body using matchers, only strings, i.e. .WithBody(new JmesPathMatcher("things.name == 'RequiredThing'"))).

StefH commented 3 months ago

Correct, the JmesPathMatcher (from the server project) is not available if you use the client builder.

If you only have use client-side, you need code like this:

mappingBuilder.Given(m => m
            .WithRequest(req => req
                .WithPath("/test1")
                .UsingPost()
                .WithBody(b => b
                    .WithMatcher(match => match
                        .WithName("JmesPathMatcher")
                        .WithPattern("things.name == 'RequiredThing'")
                    )
                )
            )
            .WithResponse(rsp => rsp
                .WithHeaders(h => h.Add("Content-Type", "application/json"))
                .WithDelay(TimeSpan.FromMilliseconds(50))
                .WithStatusCode(200)
                .WithBodyAsJson(new
                {
                    status = "ok"
                }, true)
            )
        );
hmiguel commented 3 months ago

If so, I prefer to use @R0boC0p extension. Thanks

StefH commented 3 months ago

As stated by @R0boC0p, that extension is not available. The only code which is available on the client is the code I posted.

StefH commented 3 months ago

https://github.com/WireMock-Net/WireMock.Net/pull/1116