mockserver icon indicating copy to clipboard operation
mockserver copied to clipboard

Ignore specific JSON keys in mockserver body expectation

Open sdaus opened this issue 1 year ago • 0 comments

Describe the feature request I'd like to request a feature to ignore certain JSON keys when using the .withBody(JsonBody.json(...)) request expectation matcher.

What you are trying to do I have JSON bodies where the value of a particular key keeps changing with every single test run, making it impossible for me to use the .withBody() matcher.

Example In this example, I have two JSON keys that change with each test run that I want to ignore: current-time' and random-id'.

        mockServerClient
                .when(request("/example").withMethod("POST")
                        .withHeader(EXAMPLE_HEADER)
                        .withBody(JsonBody.json("{\"example\": \"example data\",\"current-time\":\"1717078245\",\"random-id\": \"A98DB3\"}")), Times.once())
                .respond(response().withStatusCode(Integer.valueOf(200)));

The solution you'd like Introduce an option to specify one or more JSON keys to be ignored when matching the JSON body.

Describe alternatives you've considered I'm currently using a temporary workaround using EasyMock.anyString() to match any JSON body:

        mockServerClient
                .when(request("/example").withMethod("POST")
                        .withHeader(EXAMPLE_HEADER)
                        .withBody(EasyMock.anyString()), Times.once())
                .respond(response().withStatusCode(Integer.valueOf(200)));

sdaus avatar May 30 '24 14:05 sdaus