【问题标题】:Cannot find x path from request in wiremock在wiremock中找不到请求的x路径
【发布时间】:2019-08-17 06:10:29
【问题描述】:

我正在尝试在 wirkmock 中创建存根。但是当我到达端点时显示“请求不匹配”

当我使用简单的检查时它就可以工作

在代码中:

  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

标题

Content-Type:application/xml

身体

<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>
                                                           |
-----------------------------------------------------------------------------------------------------------------------

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

【问题讨论】:

    标签: scala xpath wiremock


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢!!它对我有用。我当时不明白当我读到一个wiremock文档时
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多