Raghvendra Rao
Raghvendra Rao

Reputation: 156

In wiremock mapping json BodyPatterns only the last comparison is happening

I am using wiremock-jre8-standalone-2.27.0 jar to mock an API. My mapping json looks like:

 {
  "request": {
    "url": "/sampleUrl",
    "method": "POST",
    "bodyPatterns": [
      {
        "matchesJsonPath" : {
          "expression": "$[0].fruit",
          "contains": "apple"
        },
        "matchesJsonPath" : {
          "expression": "$[0].quantity",
          "contains": "1221"
        },
        "matchesJsonPath" : {
          "expression": "$[1].fruit",
          "contains": "banana"
        },
        "matchesJsonPath" : {
          "expression": "$[2].quantity",
          "contains": "2784"
        }
      }
    ]
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json; charset=utf-8"
    },
    "bodyFileName": "prices.json",
    "delayDistribution": {
      "type": "uniform",
      "lower": 200000,
      "upper": 500000
    }

As it could be seen there are 4 matchesJsonPath inside bodyPatterns but only the last matchesJsonPath ($[2].quantity == 2784) is compared every time. Is I change the remaining things in the request body such as it fails the first three matchesJsonPath and send the request through Postman I still get the response. Is there a way to make Wiremock check all the conditions?

Upvotes: 3

Views: 1780

Answers (1)

agoff
agoff

Reputation: 7145

The issue is with your bodyPatterns array. Each match needs to be its own JSON object within the array. You currently have the matchers all within one object.

"bodyPatterns": [
    {
        "matchesJsonPath" : {
          "expression": "$[0].fruit",
          "contains": "apple"
        }
    },
    {
        "matchesJsonPath" : {
          "expression": "$[0].quantity",
          "contains": "1221"
        }    
    },
    {
        "matchesJsonPath" : {
          "expression": "$[1].fruit",
          "contains": "banana"
        }    
    },
    {
        "matchesJsonPath" : {
          "expression": "$[2].quantity",
          "contains": "2784"
        }    
    }
]

Upvotes: 2

Related Questions