SOAP webservices are widely used in enterprise environments. Although they feel a bit clumsy in comparison to slim REST services, sometimes you have to deal with them.
The great thing is, to test such a service you are often free to use any tool you like. I like RSpec!
To query a web service you just need a few lines of code. I recommend Savon as SOAP client. It is used as shown here:
require 'rubygems'
require 'savon'
WSDL_URL = 'http://www.webservicex.net/geoipservice.asmx?wsdl'
client = Savon::Client.new WSDL_URL
response = client.get_geo_ip do |soap|
soap.body = { "wsdl:IPAddress" => "209.85.149.106" }
end
puts response
The response object can be converted to hash with the to_hash method, so you can fetch all values simply like you would do it with any other hash.
Now, the rest should be easy and is just a normal RSpec test:
require 'rubygems'
require 'savon'
WSDL_URL = 'http://www.webservicex.net/geoipservice.asmx?wsdl'
RETURN_CODE_OK = "1"
RETURN_CODE_ERROR = "0"
describe "Geo IP Webservice at #{WSDL_URL}" do
# helper method
def get_geo_ip_result ip
response = @client.get_geo_ip do |soap|
soap.body = {"wsdl:IPAddress" => ip}
end
response.to_hash[:get_geo_ip_response][:get_geo_ip_result]
end
before :all do
@client = Savon::Client.new WSDL_URL
end
it "should yield a country name" do
result = get_geo_ip_result "209.85.149.106"
result[:country_name].should_not be_nil
result[:return_code].should eql(RETURN_CODE_OK)
end
it "should return error for malformed ip address" do
result = get_geo_ip_result "not.an.ip.address"
result[:return_code].should eql(RETURN_CODE_ERROR)
end
it "should fail if no ip address is submitted" do
lambda { @client.get_geo_ip }.should raise_error
end
# ...
end
Happy testing!
EDIT:
@dbloete pointed me to the fact that with RSpec 2 you can expect errors even more readable:
it "should fail if no ip address is submitted" do
expect { @client.get_geo_ip }.to raise_error
end

