空手道检查xml请求是否包含特定值 [英] Karate check if xml request contains specific value

查看:56
本文介绍了空手道检查xml请求是否包含特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的空手道模拟服务器将接受XML格式的请求,以下是该请求的示例:

My Karate mock server will accept request in XML form, below is the example of the request:

<methodCall>
<methodName>MyMethod</methodName>
<params>
    <param>
        <value>
            <struct>
                <member>
                    <name>a</name>
                    <value>abc</value>
                </member>
                <member>
                    <name>b</name>
                    <value><i4>2</i4></value>
                </member>
            </struct>
        </value>
    </param>
</params>

<struct>中,它可能具有多个<member>标记.如何检查请求<member>中是否存在特定的<name>和特定的<value>?

我可以定义如下的Scenario:

Within the <struct>, it might have multiple <member> tag. How can I check if within the request <member>, there is specific <name> is presented with specific <value>?

I can define a Scenario like below:

Scenario: pathMatches('/test') && methodIs('post') && bodyPath('/methodCall/methodName') == 'MyMethod'

处理基于methodName的请求,但是我想根据它包含的<member>做不同的处理.例如:如果请求包含具有<name>color</name>且值为<value>blue</value>的成员,那么我将相应地执行该工作.

to handle the request based on methodName, but I would like to do different handling depends on what <member> it contains. For example: If request contains a member with <name>color</name> with value <value>blue</value> then I will do the job accordingly.

推荐答案

也许您对此考虑过多,所拥有的可能就足够了.您需要做的就是有条件地返回response.一个提示是,空手道可以将XML自动覆盖到JSON中,这很方便,因为它更容易编写JSON路径查询或过滤操作:

Maybe you are over-thinking this and what you have may be sufficient. All you need to do is return a response conditionally. One tip is that Karate can auto-covert XML into JSON which is convenient because it is easier to write JSON-path queries or filter operations:

下面是一些示例代码,可以为您提供一些想法:

Here is some example code that can give you some ideas:

* def structs = get[0] request..member
* def fun = function(x){ return x.name == 'a' && x.value == 'abc' }
* def test = karate.filter(structs, fun)
* if (test.length) karate.set('response', '<some>response</some>')

我想关键是您要尝试进行非平凡的条件处理,因此代码将比平时更复杂.

I guess the point is you are trying to do non-trivial conditional handling, so the code will be more complex than usual.

您可以使用JSON路径而不是karate.filter() hack,但是查询表达式可能会更难于读取IMO.请注意,您可以在Background中定义一个函数,例如getStructType()-将所需的所有逻辑放入其中-然后在Scenario HTTP路径"route"表达式中使用它.

You can use JSON-path instead of the karate.filter() hack, but the query expression may get harder to read IMO. Note that you can define a function such as getStructType() in the Background - put all the logic you want in it - and then use it in the Scenario HTTP path "route" expression.

这篇关于空手道检查xml请求是否包含特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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