2

I am having a hard time figuring out how to match stub_request with XML requests using webmock.

Request:

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

Stubs I tried:

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

or

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

(etc.)

have failed.

Anyone?

chrisandrew.cl
  • 867
  • 14
  • 30

1 Answers1

1

Provided you can save your XML to be stubbed into separate XML file (say request.xml), it is quite easy:

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

Headers can be omitted.

How to use webmock regex matcher? might help you with URL matching.

P.S. strip is to remove ending line feed

Artur INTECH
  • 6,024
  • 2
  • 37
  • 34