Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request Matching JsonPathMatcher Partially #1114

Open
hmiguel opened this issue Jun 4, 2024 · 0 comments
Open

Request Matching JsonPathMatcher Partially #1114

hmiguel opened this issue Jun 4, 2024 · 0 comments
Assignees
Labels

Comments

@hmiguel
Copy link

hmiguel commented Jun 4, 2024

Hello, I'm trying to match a json path partially, but I'm not sure how to do it.

Here's what I have:

        var client = new HttpClient();
        var server = WireMockServer.Start();

        // this match
        var test1 = new { things = new { name = "RequiredThing" } };

        // this does not match
        var test2 = new { things = new { name = "RequiredThing" }, soup = "carrot" };
   
        server.Given(
            WireMock.RequestBuilders.Request.Create()
            .WithPath("/test1")
                .UsingPost()
                .WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"))
            )
            .RespondWith(
                WireMock.ResponseBuilders.Response.Create()
                   .WithHeader("Content-Type", "application/json")
                   .WithStatusCode(200)
                   .WithDelay(TimeSpan.FromMilliseconds(50))
            );

        server.Given(
           WireMock.RequestBuilders.Request.Create()
           .WithPath("/test2")
               .UsingPost()
               .WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"))
           )
           .RespondWith(
               WireMock.ResponseBuilders.Response.Create()
                  .WithHeader("Content-Type", "application/json")
                  .WithStatusCode(200)
                  .WithDelay(TimeSpan.FromMilliseconds(50))
           );

        var response1 = await client.PostAsJsonAsync($"http://localhost:{server.Port}/test1", test1);
        response1.EnsureSuccessStatusCode();

        var response2 = await client.PostAsJsonAsync($"http://localhost:{server.Port}/test2", test2);
        response2.EnsureSuccessStatusCode();

My expectation is that both requests match, but the second one fails.

The same approach using JmesPathMatching works fine.

server.Given(
    WireMock.RequestBuilders.Request.Create()
    .WithPath("/test2")
        .UsingPost()
       //.WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"))
       .WithBody(new JmesPathMatcher("things.name == 'RequiredThing'"))
    )
    .RespondWith(
        WireMock.ResponseBuilders.Response.Create()
           .WithHeader("Content-Type", "application/json")
           .WithStatusCode(200)
           .WithDelay(TimeSpan.FromMilliseconds(50))
    );

WireMock.Net: 1.5.56

@StefH StefH self-assigned this Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants