首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何删除请求中的SOAP可选元素

如何删除请求中的SOAP可选元素
EN

Stack Overflow用户
提问于 2018-06-07 05:02:11
回答 1查看 465关注 0票数 1

使用空手道,我想构建一个soap请求。SOAP请求中有可选元素,如何根据场景大纲删除它们:示例?

分享一个例子,以供讨论。如果已经有一个示例代码,请分享,谢谢。

代码语言:javascript
复制
Feature: SOAP request to get Customer Details

  Background:
    Given url 'http://localhost:8080/CustomerService_V2_Ws'

  Scenario Outline:
    * def removeElement =
    """
     function(parameters, inputXml) {
         if (parameters.city = null)
             karate.remove(inputXml, '/Envelope/Header/AutHeader/ClientContext/city');
         if (parameters.zipcode = null)
             karate.remove(inputXml, '/Envelope/Header/AutHeader/ClientContext/zipcode');
         return inputXml;
     }
    """
    * def inputXml = read('soap-request.xml')
    * def updatedXml = removeElement(parameters,inputXml)
    Given request updatedXml
    When soap action ''
    Then status <http_code>

    Examples:
      | CustomerId | ZipCode | City     |
      | 001        | null    | null     |
      | 002        | 41235   | null     |
      | 003        | null    | New York |


**Contents of "soap-request.xml"**
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>
      <wsc1:AutHeader xmlns:wsc1="http://example.com/ws/WSCommon_v22">
         <wsc1:SourceApplication>ABC</wsc1:SourceApplication>
         <wsc1:DestinationApplication>SoapUI</wsc1:DestinationApplication>
         <wsc1:Function>CustomerService.readDetails</wsc1:Function>
         <wsc1:Version>2</wsc1:Version>
         <wsc1:ClientContext>
            <wsc1:customerid>10000</wsc1:customerid>
            <!--Optional:-->
            <wsc1:zipcode>11111</wsc1:zipcode>
            <!--Optional:-->
            <wsc1:city>xyz</wsc1:city>
         </wsc1:ClientContext>
      </wsc1:AutHeader>
   </soapenv:Header>
   <soapenv:Body />
</soapenv:Envelope>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-07 05:29:12

是的,我的建议是使用嵌入表达式。当表达式以两个散列符号作为前缀时,这将“删除如果为空”,这是为您的精确用例设计的。下面是一个示例:

代码语言:javascript
复制
Scenario: set / remove xml chunks using embedded expressions
* def phone = '123456'
# this will remove the <acc:phoneNumberSearchOption> element
* def searchOption = null
* def search = 
"""
<acc:getAccountByPhoneNumber>
    <acc:phoneNumber>#(phone)</acc:phoneNumber>
    <acc:phoneNumberSearchOption>##(searchOption)</acc:phoneNumberSearchOption>        
</acc:getAccountByPhoneNumber>
"""
* match search ==
"""
<acc:getAccountByPhoneNumber>
    <acc:phoneNumber>123456</acc:phoneNumber>
</acc:getAccountByPhoneNumber>
"""

请注意这里还有更多的例子:xml.feature

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50733259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档