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

蓝咒 提交于 2021-01-28 08:07:24

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/63625877/matching-key-value-pairs-in-a-request-in-a-karate-netty-scenario

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!