mock-server / mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
http://mock-server.com
Apache License 2.0
4.62k stars 1.08k forks source link

Ignore specific JSON keys in mockserver body expectation #1878

Open sdaus opened 6 months ago

sdaus commented 6 months ago

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' andrandom-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)));