How to verify if json key contains specific value in Wiremock

649 views Asked by At

I have below request URL:

http://localhost:9082/v1/action/query

And I have below requests in wiremock request file:

  1. {"queryString":"Select firstname, lastname, workphone, id, accountId from mydetails}

    { "request": { "method": "POST", "url": "/v1/action/query", "bodyPatterns" : [ { "contains": "queryString=from mydetails" } ] }, "response": { "status": 200, "bodyFileName": "mydetails.json", "headers": { "Content-Type": "application/json" } } }

  2. {"queryString":"Select firstname, lastname, workphone, id, accountId from myinfo}

    { "request": { "method": "POST", "url": "/v1/action/query", "bodyPatterns" : [ { "contains": "queryString=from myinfo" } ] }, "response": { "status": 200, "bodyFileName": "myinfo.json", "headers": { "Content-Type": "application/json" } } }

Now I have same URL for both request. I have to verify if in request body if we are getting table "from myinfo" inside queryString Json key . then myinfo file returned. and if we have "from Mydetails" then my details one called.

Do we have any suggestion for same?

1

There are 1 answers

0
agoff On

You can use the matchingJsonPath functionality to match one or the other.

Assuming a JSON Mapping...

{
    "request": {
         ...
        "bodyPatterns": [{
            "matchesJsonPath": "$(@.queryString =~ .*/myinfo/.*)"
        }]
    }
}

The exact JsonPath query might need to be fixed, that's just a rough estimate of what I think it would be