Warm tip: This article is reproduced from stackoverflow.com, please click
regex wiremock mocking httprequest stub

Check for null values in request body using Wiremock

发布于 2020-04-05 00:26:40

I am trying to setup a wiremock stub that will return a 400 error if any field has a null value in the json payload. Basically to simulate a Bad Request. I've been trying with a regex that matches any lowercase string for the json key but it doesn't seem to like it. I can't find any examples of what I want online so not sure if it's even possible.

My Bad Request body:

{
  "cat": null,
  "dog": {
    "id": 1344
},
  "horse": {
    "id": 1
},
  "fish": 1
}

My Stub:

wireMockServer.stubFor(post(urlEqualTo("/sample-api"))
            .withRequestBody(matchingJsonPath("$.^[a-z]*", equalTo(null)))
            .willReturn(aResponse()
                    .withStatus(400)
                    .withHeader("Content-Type", "application/json")))

In this example I would expect the stub to match "cat" as the value of it is null. This isn't the case. Can anyone tell me what I'm doing wrong?

Questioner
Aimee Jones
Viewed
187
A. Kootstra 2020-01-16 15:03

In the WireMock documentation on Request Matching the section on JSON Path matching. In the source code there is a reference to com.jayway.jsonpath.JsonPath library used. The build.gradle refers to version 2.4.0. The documentation for the Jayway JSON Path library can be found on their Github project page. There is a good, but by no means perfect online evaluator here.

The WireMock documentation only shows support for Regular Expression for the node values in the form of the "matchesJsonPath". In the Jayway documenatation there is an online example: $..book[?(@.author =~ /.*REES/i)]. For this reason the only approach is to name all the nodes that are not allowed to be null.

In the below example mapping all the mentioned nodes will be tested, regardless of their depth (see @id). This mapping will not trigger if all the mentioned nodes are not null, but some unmentioned ones are.

{
  "request": {
    "urlPattern": "/sample-api",
    "method": "GET",
    "bodyPatterns" : [ {
      "matchesJsonPath" : "$..[?(@.cat == null || @.dog == null || @.horse == null || @.fish == null || @.id == null)]"
    } ] 
  },
  "response": {
    "status": "400",
    "headers": {
      "Content-Type": "application/json; charset=utf-8"
    },
    "jsonBody": {
      "message": "some sapi message"
    }
  }
}