nettykaratescenarios

Matching key value pairs in a request in a Karate Netty scenario


I'm writing features for a Karate Netty mock service and I'm trying to use the Scenario to match on a key value pair in the request.

For example:

In a request like this one that has a nested array of objects, I want this feature to fire when any of the Id values are null:

{
    "Array": [

        {
            "Id": "legitId"
        },
        {
            "Id": null
        }
    ],
}

Scenario: pathMatches('path/to/my/endpoint') && methodIs('post') && request.Array.contains('Id': null)

The documentation says I can use JS expressions in the scenario, but I've had a lot of trouble finding something that nashorn can parse that can do this. I've tries JSON.stringify and indexOf, some, the syntax used in matching in a karate test, still no luck. I also tried using a wildcard for the Array index to say something like Array[*].Id == null, but that doesn't fly either.


Solution

  • Good news, in Karate 0.9.6 you can use JsonPath and karate.match() for this:

    Scenario: karate.match("request.Array[*].Id contains null").pass
    

    For more details: https://github.com/intuit/karate/issues/1202#issuecomment-653632397

    And also see this other answer for ideas on using a custom function. defined in the Background to make this easier if needed: https://stackoverflow.com/a/59415796/143475