Created
January 21, 2015 12:47
-
-
Save mwlang/8de20866c2cf0b29b978 to your computer and use it in GitHub Desktop.
Below is an example of using Savon to test SOAP endpoints of a Rails app that uses Wash out. The Savon approach works, but Savon likes to mangle the XML you feed it by wrapping it in the SOAP Envelope and Body tags. Therefore, I'm trying to duplicate with Faraday what's working with Savon so I can fully control the XML being fed to the SOAP endp…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Failure/Error: response = post_xml(:stock_update, "single_item") | |
Faraday::ConnectionFailed: | |
getaddrinfo: nodename nor servname provided, or not known | |
# /Users/mwlang/.rvm/gems/ruby-2.1.2/gems/faraday-0.9.0/lib/faraday/adapter/net_http.rb:80:in `perform_request' | |
# /Users/mwlang/.rvm/gems/ruby-2.1.2/gems/faraday-0.9.0/lib/faraday/adapter/net_http.rb:39:in `call' | |
# /Users/mwlang/.rvm/gems/ruby-2.1.2/gems/faraday-0.9.0/lib/faraday/rack_builder.rb:139:in `build_response' | |
# /Users/mwlang/.rvm/gems/ruby-2.1.2/gems/faraday-0.9.0/lib/faraday/connection.rb:377:in `run_request' | |
# /Users/mwlang/.rvm/gems/ruby-2.1.2/gems/faraday-0.9.0/lib/faraday/connection.rb:177:in `post' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def faraday_connection | |
Faraday.new(:url => "http://app/soap/wsdl") do |faraday| | |
faraday.adapter Faraday.default_adapter | |
end | |
end | |
def faraday_xml(service, fixture_name) | |
response = faraday_connection.post do |req| | |
req.url '/soap/action' | |
req.headers['Content-Type'] = 'text/xml; charset="utf-8"' | |
req.headers['SOAPAction'] = '/soap/action/stock_update' | |
req.body = read_fixture(service, fixture_name) | |
end | |
response.body | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HTTPI.logger = Logger.new(open("/dev/null", 'w')) | |
HTTPI.adapter = :rack | |
HTTPI::Adapter::Rack.mount 'app', Webservice::Application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def savon_xml(method, xml) | |
savon = Savon::Client.new(wsdl: "http://app/soap/wsdl") | |
hash_str = savon.call(method, :message => xml).to_hash["#{method}_response".to_sym] | |
result = eval(hash_str[:value]) rescue hash_str | |
result.is_a?(String) ? {value: result} : result | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
describe SoapController, :type => :controller do | |
describe "stock_update" do | |
it "should process xml" do | |
response = savon_xml(:update_stock, Fixture.stock_update(:single_item)) | |
expect(response[:value]).to eq "success!" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment