How does WithBody work with request matchers? #571

Closed
opened 2025-12-29 08:30:22 +01:00 by adam · 5 comments
Owner

Originally created by @jonakirke94 on GitHub (Feb 12, 2024).

I'm using the testcontainers wiremock and try to set up a request mapping.

I'm using AdminApiMappingBuilder

This is an example:

builder
    .Given(m => m.WithRequest(req => req
            .UsingGet()
            .WithUrl("test"))
        .WithResponse(rsp => rsp
            .WithStatusCode(200)
            .WithHeaders(() => ApiFixture.DefaultResponseType)
            .WithBody("hello world")).Build()

How can I use WithBody here with JsonPartialMatcher? WithBody takes a MatcherModelBuilder but it's unclear to me how we can leverage this.

builder
    .Given(m => m.WithRequest(req => req
            .UsingGet()
            .WithBody() ???? <------
            .WithUrl("test"))
Originally created by @jonakirke94 on GitHub (Feb 12, 2024). I'm using the testcontainers wiremock and try to set up a request mapping. I'm using AdminApiMappingBuilder This is an example: ``` builder .Given(m => m.WithRequest(req => req .UsingGet() .WithUrl("test")) .WithResponse(rsp => rsp .WithStatusCode(200) .WithHeaders(() => ApiFixture.DefaultResponseType) .WithBody("hello world")).Build() ``` How can I use WithBody here with JsonPartialMatcher? WithBody takes a MatcherModelBuilder but it's unclear to me how we can leverage this. ``` builder .Given(m => m.WithRequest(req => req .UsingGet() .WithBody() ???? <------ .WithUrl("test")) ```
adam added the question label 2025-12-29 08:30:22 +01:00
adam closed this issue 2025-12-29 08:30:23 +01:00
Author
Owner

@StefH commented on GitHub (Feb 13, 2024):

@jonakirke94
There a are several Json matchers available, see this wiki page for an overview (https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matchers) and see the detail page for how to use it.

@StefH commented on GitHub (Feb 13, 2024): @jonakirke94 There a are several Json matchers available, see this wiki page for an overview (https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matchers) and see the detail page for how to use it.
Author
Owner

@jonakirke94 commented on GitHub (Feb 13, 2024):

@StefH The syntax seem different when using the AdminAPI

The examples do not use the AdminApiMappingBuilder

Could you show me how to do it?

builder
    .Given(m => m.WithRequest(req => req
            .UsingGet()
            .WithBody() ???? <------
            .WithUrl("test"))
@jonakirke94 commented on GitHub (Feb 13, 2024): @StefH The syntax seem different when using the AdminAPI The examples do not use the AdminApiMappingBuilder Could you show me how to do it? ``` builder .Given(m => m.WithRequest(req => req .UsingGet() .WithBody() ???? <------ .WithUrl("test")) ```
Author
Owner

@StefH commented on GitHub (Feb 13, 2024):

@jonakirke94
I do understand your question now. In that case, your code should be like:

using var server = WireMockServer.StartWithAdminInterface();

        var api = RestEase.RestClient.For<IWireMockAdminApi>(server.Url!);

        var guid = Guid.Parse("53241df5-582c-458a-a67b-6de3d1d0508e");
        var mappingBuilder = api.GetMappingBuilder();
        mappingBuilder.Given(m => m
            .WithTitle("This is my title 1")
            .WithGuid(guid)
            .WithRequest(req => req
                .UsingPost()
                .WithPath("/bla1")
                .WithBody(body => body
                    .WithMatcher(matcher => matcher
                        .WithName("JsonPartialMatcher")
                        .WithPattern(new { test = "abc" })
                    )
                )
            )
            .WithResponse(rsp => rsp
                .WithBody("The Response")
            )
        );

        // Act
        var status = await mappingBuilder.BuildAndPostAsync().ConfigureAwait(false);

        // Assert
        status.Status.Should().Be("Mapping added");
@StefH commented on GitHub (Feb 13, 2024): @jonakirke94 I do understand your question now. In that case, your code should be like: ``` c# using var server = WireMockServer.StartWithAdminInterface(); var api = RestEase.RestClient.For<IWireMockAdminApi>(server.Url!); var guid = Guid.Parse("53241df5-582c-458a-a67b-6de3d1d0508e"); var mappingBuilder = api.GetMappingBuilder(); mappingBuilder.Given(m => m .WithTitle("This is my title 1") .WithGuid(guid) .WithRequest(req => req .UsingPost() .WithPath("/bla1") .WithBody(body => body .WithMatcher(matcher => matcher .WithName("JsonPartialMatcher") .WithPattern(new { test = "abc" }) ) ) ) .WithResponse(rsp => rsp .WithBody("The Response") ) ); // Act var status = await mappingBuilder.BuildAndPostAsync().ConfigureAwait(false); // Assert status.Status.Should().Be("Mapping added"); ```
Author
Owner

@jonakirke94 commented on GitHub (Feb 13, 2024):

Thanks.

So in your example it matches a json property "test" with value "abc"

@jonakirke94 commented on GitHub (Feb 13, 2024): Thanks. So in your example it matches a json property "test" with value "abc"
Author
Owner

@StefH commented on GitHub (Feb 13, 2024):

Thanks.

So in your example it matches a json property "test" with value "abc"

correct

@StefH commented on GitHub (Feb 13, 2024): > Thanks. > > So in your example it matches a json property "test" with value "abc" correct
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#571