无法从 Wiremock 中的请求中找到 x 路径

Cannot find x path from request in wiremock

我正在尝试在 wirkmock 中创建存根。但是当我到达终点时显示 'request not match'

当我使用一个简单的检查时它起作用了

代码中:

  stubFor(get(urlEqualTo("/v1/test.svc"))
    .withHeader("Content-Type", equalTo("application/xml"))
      .withRequestBody(containing("<b:Name>Test</b:Name>"))
    .willReturn(aResponse()
      .withStatus(200)
      .withHeader("Content-Type", "application/xml")
      .withBody("Passed")));

请求:

端点

http://{{url}}/v1/test.svc

header

Content-Type:application/xml

body

<test xmlns="http://abc.example.com">
    <request xmlns:b="http://abc.example.com/b" xmlns:i="http://abc.example.com/i">
        <b:Name>Test</b:Name>
        <b:Age>18</b:Age>
        <b:Count>2020</b:Count>
    </request>
</test>

结果:

Passed

但是当我尝试使用 xpath 检查某些值时它不起作用

代码中:

  stubFor(get(urlEqualTo("/paymentapi/paymentservice.svc"))
    .withHeader("Content-Type", equalTo("application/xml"))
      .withRequestBody(containing("<b:Name>Test</b:Name>"))
      .withRequestBody(matchingXPath("//Age/text()",equalTo("18")))
    .willReturn(aResponse()
      .withStatus(200)
      .withHeader("Content-Type", "application/xml")
      .withBody("Passed")));

结果:


                                               Request was not matched
                                               =======================

-----------------------------------------------------------------------------------------------------------------------
| Closest stub                                             | Request                                                  |
-----------------------------------------------------------------------------------------------------------------------
                                                           |
GET                                                        | GET
/vi/test.svc                                               | /v1/test.svc
                                                           |
Content-Type: application/xml                              | Content-Type: application/xml
                                                           |
//Age/text()                                               | <test xmlns="http://abc.example.com">
    <request  <<<<< Body does not match
                                                           | xmlns:b="http://abc.example.com/b"
                                                           | xmlns:i="http://abc.example.com/i">
<b:Name>Test</b:Name>
        <b:Age>18</b:Age>
<b:Count>2020</b:Count>
    </request>
</test>
                                                           |
-----------------------------------------------------------------------------------------------------------------------

谁能告诉我如何检查这个值?

Age 元素位于 http://abc.example.com/b 命名空间 URI 下。从 Wiremock documentation 开始,您可以像这样为 XPath 表达式声明范围内命名空间:

.withRequestBody(matchingXPath("//b:Age[.=18]")
  .withXPathNamespace("b", "http://abc.example.com/b"))