在空手道Netty场景中匹配请求中的键值对 [英] Matching key value pairs in a request in a Karate Netty scenario

查看:119
本文介绍了在空手道Netty场景中匹配请求中的键值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为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.

例如:

在这样一个具有嵌套对象数组的请求中,我希望当任何Id值都为null时触发此功能:

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
        }
    ],
}

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

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

文档说我可以在场景中使用JS表达式,但是我很难找到nashorn可以解析的东西来做到这一点.我已经尝试过JSON.stringify和indexOf,其中一些在空手道测试中用于匹配的语法仍然没有运气.我还尝试对Array索引使用通配符来表示类似Array [*].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.

推荐答案

好消息,在空手道0.9.6中,您可以为此使用JsonPath和karate.match():

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

更多详细信息: https://github.com/intuit/karate /issues/1202#issuecomment-653632397

另请参阅此其他答案,以获取有关使用自定义功能的想法.在Background中定义以使此操作更容易(如果需要): https://stackoverflow.com/a/59415796/143475

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

这篇关于在空手道Netty场景中匹配请求中的键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆