gpt4 book ai didi

ruby-on-rails - 针对 XML 请求的 Rails WebMock

转载 作者:太空宇宙 更新时间:2023-11-03 16:07:34 25 4
gpt4 key购买 nike

我很难弄清楚如何使用 webmock 将 stub_request 与 XML 请求相匹配。

要求:

    <?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header>
<RequesterCredentials xmlns="urn:ebay:api:PayPalAPI" xmlns:n1="urn:ebay:apis:eBLBaseComponents" env:mustUnderstand="0">
<n1:Credentials>
<n1:Username>XYZ</n1:Username>
<n1:Password>ABC</n1:Password><n1:Subject/>
<n1:Signature>HUGE-STRING</n1:Signature>
</n1:Credentials>
</RequesterCredentials>
</env:Header>
<env:Body>
<ManageRecurringPaymentsProfileStatusReq xmlns="urn:ebay:api:PayPalAPI">
<ManageRecurringPaymentsProfileStatusRequest xmlns:n2="urn:ebay:apis:eBLBaseComponents">
<n2:Version>124</n2:Version>
<n2:ManageRecurringPaymentsProfileStatusRequestDetails>
<ProfileID>sdaddsadsd</ProfileID>
<n2:Action>Cancel</n2:Action>
</n2:ManageRecurringPaymentsProfileStatusRequestDetails>
</ManageRecurringPaymentsProfileStatusRequest>
</ManageRecurringPaymentsProfileStatusReq>
</env:Body>
</env:Envelope>

我试过的 stub :

stub_request(:any, /.*.sandbox.paypal.com\/2.0\//).
with(:body => WebMock::Matchers::HashIncludingMatcher.new({'n2:Action'=>'Cancel'}),
:headers => {'User-Agent'=>'Ruby'}).
to_return(:status => 200, ...

stub_request(:any, /.*.sandbox.paypal.com\/2.0\//).
with(query:
hash_including({ProfileID: 'sdaddsadsd'}),
:headers => {'User-Agent'=>'Ruby'}).
to_return(:status => 200, ...

(等)

失败了。

有人吗?

最佳答案

只要您可以将要 stub 的 XML 保存到单独的 XML 文件中(比如 request.xml),这就非常简单:

stub_request(:any, 'https://sandbox.paypal.com')
.with(body: File.read('request.xml').strip)

标题可以省略。

How to use webmock regex matcher?可能会帮助您进行 URL 匹配。

附言strip 是去除结束换行

关于ruby-on-rails - 针对 XML 请求的 Rails WebMock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35084742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com