【问题标题】:WireMock bodyPatterns XML Match regexWireMock bodyPatterns XML 匹配正则表达式
【发布时间】:2015-11-30 20:50:04
【问题描述】:

我正在使用 WireMock 模拟 SOA 服务,但是我的 bodyPattern XML 有问题,我可以在 XML 中使用正则表达式吗?

我的请求标头会根据请求时间而变化,我只想匹配标头中的任何内容。

{
  "request" : {
    "url" : "/service/v1/WebService",
    "method" : "POST",
    "bodyPatterns" : [ {
      "equalToXml" :"\\Q<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header>
      <SOAP-ENV:Header>
I want to match whatever inside header.
</SOAP-ENV:Header>

    } ]
  },

【问题讨论】:

    标签: regex xml wiremock


    【解决方案1】:

    例如,您的请求如下所示,

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ex="http://example.com">
    <soapenv:Header/>
    <soapenv:Body>
        <ex:somebody>
            <ex:input>
                <ex:name>John</ex:name>
                <ex:phone>7414444</ex:phone>
                <ex:role>teacher</ex:role>
            </ex:input>
        </ex:somebody>
    </soapenv:Body>
    

    那么你的 JSON 文件可能是,

    {
    "request" : {
        "url" : "/service/v1/WebService",
        "bodyPatterns" : [ {
            "matchesXPath": "//ex:input[ex:name=\"John\" and xw:phone=\"7414444\"]",
            "xPathNamespaces" : {
                "ex" : "http://example.com"
            }
        }]
    },
    "response" : {
        "status" : 200,
        "headers": {
            "Content-Type": "text/xml;charset=UTF-8"
        },
        "body" : "<Abody>"
    }}
    

    【讨论】:

      【解决方案2】:

      您可以使用 Xpath 来达到相同的结果

      "bodyPatterns": [
            {
              "matchesXPath": "your xpath"
            }
      

      【讨论】:

        猜你喜欢
        • 2018-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-09
        • 1970-01-01
        • 1970-01-01
        • 2019-03-30
        • 2011-05-01
        相关资源
        最近更新 更多