简体   繁体   English

在 Wiremock 中评估 XPath 表达式

[英]Evaluating XPath expressions in Wiremock

I am using Wiremock for testing and I am facing the below issue.我正在使用 Wiremock 进行测试,我面临以下问题。

There are 2 scenarios有2个场景

  1. When the request would be coming as below当请求如下
<Body>
    <ElementA>Element A</ElementA>
    <ElementB>123<ElementB>
</Body>

Here we only need to check that the value for element B should be 123.这里我们只需要检查元素 B 的值是否应该是 123。

  1. When the request would be coming as below当请求如下
<Body>
    <ElementA flag="true">Element A</ElementA>
    <ElementB>123<ElementB>
</Body>

Here we need to check that the value for element B should be 123 and the value for flag should be true as well.这里我们需要检查元素 B 的值是否应该是 123,并且标志的值也应该是 true。

Now I want my wiremock to return different responses in both the cases and my wiremock config files looks something like this.现在我希望我的wiremock在这两种情况下都返回不同的响应,我的wiremock配置文件看起来像这样。

  1. For case 1对于案例 1
{
  "request": {
    "url": "/",
    "method": "POST",
    "bodyPatterns": [
      {
        "matchesXPath": {
          "expression": "//ElementB/text()",
          "contains": "123"
        }

      },{
      "absent": {
        "expression":  "//ElementA[@flag=true]"
      }
      }
    ]
  },
  "response": {
    "status": 200,
    "bodyFileName": "someFile.xml",
    "headers": {
      "Content-Type": "text/xml; charset=utf-8"
    }
  }
}

But here it only takes absent as true and the other element does not comes.但在这里它只取缺席为真,而其他元素不来。 Example from wiremock:来自wiremock的示例:

  "bodyPatterns" : [ {
        "matchesXPath" : {
          "expression" : "//ElementA/text()",
          "contains" : "123"
        }
      }, {
        "absent" : true
      } ]
  1. For case 2对于案例 2
{
  "request": {
    "url": "/",
    "method": "POST",
    "bodyPatterns": [
      {
        "matchesXPath": {
          "expression": "//ElementB[text()='123']"

        },
        "matchesXPath": {
          "expression": "//ElementA[@flag]",
          "contains": "true"
        }
      }
    ]
  },
  "response": {
    "status": 200,
    "bodyFileName": "someotherFile.xml",
    "headers": {
      "Content-Type": "text/xml; charset=utf-8"
    }
  }
}

But somehow it is not able to return the response in case 1 and in case 2 it does not care for the first condition it only checks if the value for flag is true.但不知何故,它无法在案例 1 中返回响应,而在案例 2 中它不关心第一个条件,它只检查 flag 的值是否为真。

Any help/ pointers on this would be appreciated.对此的任何帮助/指示将不胜感激。

I'm not convinced that you need the absent flag.我不相信您需要absent标志。 I think you can use priorities instead to force WireMock to check the more specific request before moving to the more general response.我认为您可以使用优先级来强制 WireMock 在转向更一般的响应之前检查更具体的请求。

I also think that there is an error in how you're checking the XPath in Case 2. I'd try something like...我还认为您在案例 2 中检查 XPath 的方式存在错误。我会尝试类似...

{
  "priority": 1,
  "request": {
    "url": "/",
    "method": "POST",
    "bodyPatterns": [
      {
        "matchesXPath": {
          "expression": "//ElementB[text()]",
          "contains": "123"
        },
        "matchesXPath": {
          "expression": "//ElementA[@flag]",
          "contains": "true"
        }
      }
    ]
  },
  "response": {
    "status": 200,
    "bodyFileName": "someFile.xml",
    "headers": {
      "Content-Type": "text/xml; charset=utf-8"
    }
  }
}

Look at the addition of "priority": 1 .看看"priority": 1 I also think you could change the matchesXPath objects to simply check for the values if you don't need them to match a regex pattern.我还认为,如果您不需要匹配正则表达式模式,您可以更改matchesXPath对象以简单地检查值。 "matchesXPath": "//ElementB[text() = 123]"

{
  "priority": 2,
  "request": {
    "url": "/",
    "method": "POST",
    "bodyPatterns": [
      {
        "matchesXPath": {
          "expression": "//ElementB[text()]",
          "contains": "123"
        }
      }
    ]
  },
  "response": {
    "status": 200,
    "bodyFileName": "someotherFile.xml",
    "headers": {
      "Content-Type": "text/xml; charset=utf-8"
    }
  }
}

Because the priority 1 mapping checks for flag=true , it is the case that a match on the priority 2 mapping does not contain Element A with a flag=true attribute.因为优先级 1 映射检查flag=true ,所以优先级 2 映射上的匹配项不包含具有 flag=true 属性的元素 A。 So, after WireMock checks the priority 1 mapping (which requires a match on flag=true ), it checks the priority 2 mapping (which only requires that ElementB's text = 123).因此,在 WireMock 检查优先级 1 映射(需要在flag=true上匹配)之后,它会检查优先级 2 映射(仅需要 ElementB 的文本 = 123)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM