Search code examples
rubyxmlsoapsavonhttpi

View Savon Request XML without Sending to Server


I'm using the Savon gem to make a SOAP request using code similar to what's below. It's working, but I would like to view/capture the request XML without actually making a call to their server. I can view it now after a request is made by sticking a debugger line after the request and inspecting the client variable.

Does anyone know of a way to view the request XML without actually making a request? I want to be able to validate the XML against a schema using Cucumber or Rspec.

client = Savon::Client.new do |wsdl, http|
  wsdl.document = "http://fakesite.org/fake.asmx?wsdl"
end

client.request(:testpostdata, :xmlns => "http://fakesite.org/") do
  soap.header = { :cAuthentication => {"UserName" => "MyName", "Password" => "MyPassword" } }
  soap.body = { :xml_data => to_xml }
end

Solution

  • Savon uses HTTPI to execute SOAP requests. HTTPI is a common interface on top of various Ruby HTTP clients. You could probably mock/stub the HTTP request executed by Savon via:

    HTTPI.expects(:post).with do |http|
      SchemaValidation.validate(:get_user, http.body)
    end
    

    Please note that I used Mocha for mocking the SOAP request, getting the HTTP body and validating it against some validation method (pseudo-code).

    Currently, Savon does not support building up requests without executing them. So the only way to validate the request would be to intercept it.

    If you would need Savon to support this feature, please let me know and open a ticket over at GitHub.

    EDIT: There's also savon_spec, which is a little helper for basic fixture-based testing with Savon.