WireMock.Net.Client Request Builder Matchers #604

Closed
opened 2025-12-29 15:28:06 +01:00 by adam · 8 comments
Owner

Originally created by @hmiguel on GitHub (Jun 5, 2024).

Originally assigned to: @StefH on GitHub.

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?

Originally created by @hmiguel on GitHub (Jun 5, 2024). Originally assigned to: @StefH on GitHub. Hello, I'm trying to replicate the following server mapping using wiremock.net rest client. ```c# 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](https://github.com/WireMock-Net/WireMock.Net/issues/886#issue-1587423344). It look like this: ```c# 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?
adam added the question label 2025-12-29 15:28:06 +01:00
adam closed this issue 2025-12-29 15:28:06 +01:00
Author
Owner

@R0boC0p commented on GitHub (Jun 5, 2024):

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.

@R0boC0p commented on GitHub (Jun 5, 2024): 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 ```c# var result = await mappingBuilder.BuildAndPostAsync().ConfigureAwait(false); ``` Cheers.
Author
Owner

@hmiguel commented on GitHub (Jun 6, 2024):

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

@hmiguel commented on GitHub (Jun 6, 2024): 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
Author
Owner

@StefH commented on GitHub (Jun 6, 2024):

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

@StefH commented on GitHub (Jun 6, 2024): For an example on the mapping builder: see https://github.com/WireMock-Net/WireMock.Net/blob/master/examples/WireMock.Net.Client/Program.cs#L22
Author
Owner

@hmiguel commented on GitHub (Jun 7, 2024):

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

@hmiguel commented on GitHub (Jun 7, 2024): @StefH I can't see any example with body using matchers, only strings, i.e. ```.WithBody(new JmesPathMatcher("things.name == 'RequiredThing'")))```.
Author
Owner

@StefH commented on GitHub (Jun 7, 2024):

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)
            )
        );
@StefH commented on GitHub (Jun 7, 2024): 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: ``` c# 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) ) ); ```
Author
Owner

@hmiguel commented on GitHub (Jun 7, 2024):

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

@hmiguel commented on GitHub (Jun 7, 2024): If so, I prefer to use @R0boC0p extension. Thanks
Author
Owner

@StefH commented on GitHub (Jun 7, 2024):

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 on GitHub (Jun 7, 2024): As stated by @R0boC0p, that extension is not available. The only code which is available on the **client** is the code I posted.
Author
Owner

@StefH commented on GitHub (Jun 7, 2024):

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

@StefH commented on GitHub (Jun 7, 2024): https://github.com/WireMock-Net/WireMock.Net/pull/1116
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#604