首页 > 解决方案 > xpath 匹配不适用于查询参数

问题描述

我试图在查询参数的 xpath 匹配中放置一个 AND 条件,但是它不起作用。

我正在创建多个存根,其中 uniqueId 可以包含同样的数字 00000 或 11111 但 serviceName 会不同。例如,在第一个请求中,uniqueId 为 1000001 且 serviceName='ABC',在第二个请求中,uniqueId 再次为 1000001 且 serviceName 为 'XYZ'。因为这两种反应会有所不同。我创建了三个存根,一个用于服务 ABC,第二个用于服务 XYZ,第三个是默认的,没有和 xpath 匹配。当我使用包含 uniqueId 1000001 和 serviceName ABC 或 XYZ 的查询参数调用服务时,每次我得到默认存根的响应。在查询参数中传递 xml

"queryParameters":{
      "xmlreqdoc": {
        "matchesXPath" : "//*[[local-name()='uniqueId'][text()[contains(.,'00000')]] AND //*[local-name()='serviceName'][text() = 'ABCDEFGH']]"
      }
    }

我已经创建了具有优先级的存根..并且似乎每次都返回默认值。以下是请求xml

<?xml version="1.0" encoding="UTF-8"?>
<service>
<serviceHeader>
<userId>XXXX</userId>
<password>XXXXX</password>
<serviceName>ABCDEFGH</serviceName>
</serviceHeader>
<serviceBody>
<subServiceName>add</subServiceName>
<uniqueId>00000</uniqueId>
</serviceBody>
</service>

下面是我创建的存根映射

{
  "priority" : 1,
  "request": {
    "urlPath": "/testMappings",
    "method": "GET",
    "headers": {
      "Content-Type": {
        "equalTo": "application/xml"
      },
      "Accept": {
        "equalTo": "application/xml"
      }
    },
    "queryParameters":{
      "xmlreqdoc": {
        "matchesXPath" : "//*[//[local-name()='uniqueId'][text()[contains(.,'00000')]] AND //*[local-name()='serviceName'][text() = 'ABCDEFGH']]"
      }
    }
  },
  "response": {
    "status": 400,
    "headers": {
      "Content-Type" : "application/xml"
    },
    "fixedDelayMilliseconds": 500,
    "bodyFileName": "ERROR.xml"
  }
}

任何帮助或指示将不胜感激。提前致谢!!

标签: xpathwiremock

解决方案


您已经告诉我们您尝试过的错误语法,但您没有告诉我们您要达到的目标,所以我们可以告诉您您做错了什么,但我们无法告诉您如何实现正确的。

"//*[//[local-name()='uniqueId'][text()[contains(.,'00000')]] AND //*[local-name()='serviceName'][text() = 'ABCDEFGH']]"

这里明显错误的事情:

  • //紧随其后的是谓词
  • 当不涉及命名空间时,在 local-name() 上进行不必要的匹配
  • 不必要的使用text()
  • 可以使用时不必要地使用 contains()=
  • AND应该and

也许你想要//uniqueId = '00000' and //serviceName = 'ABCDEFGH'


推荐阅读