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.35k stars 197 forks source link

How does WithBody work with request matchers? #1065

Closed jonakirke94 closed 4 months ago

jonakirke94 commented 4 months ago

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"))
StefH commented 4 months ago

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

jonakirke94 commented 4 months ago

@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"))
StefH commented 4 months ago

@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");
jonakirke94 commented 4 months ago

Thanks.

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

StefH commented 4 months ago

Thanks.

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

correct