3

I am using Ruby/Cucumber/Savon to automate Soap webservice. I need to validate the response against the wsdl file. Savon docs don't mention validating XML response anywhere. Does anyone know a good solution to doing this?

Thanks, Harv Gill

Harv Gill
  • 41
  • 3

2 Answers2

2

The excellent Nokogiri library supports XML schema (XSD) validation which is used for SOAP messages (i.e. the "Types" section of the WSDL should contain a reference or inline XSD).

xsd = Nokogiri::XML::Schema(File.read(SCHEMA_FILE))
doc = Nokogiri::XML(File.read(XML_FILE))

xsd.validate(doc).each do |error|
  puts error.message
end
maerics
  • 151,642
  • 46
  • 269
  • 291
  • Thank you for your response. My wsdl contains inline XSD. When I try to use the wsdl file in place of the SCHEMA_FILE, I get an error that says, "schema.rb:37:in `from_document': The XML document 'in_memory_buffer' is not a schema document. (Nokogiri::XML::SyntaxError)" – Harv Gill Mar 24 '15 at 20:46
  • @HarvGill: yes, a WSDL file is not an XSD file. You'll have to extract the schema from the containing document. – maerics Mar 26 '15 at 03:24
1

I've made a gem to simplify this process. It should extract all schemas from the WSDL and import any if needed. Let me know if it doesn't work for you.

require 'wsdl_validator'
wsld = WsdlValidator.new('path_to_wsdl')
# xml can be String, Nokogiri::XML::Document
wsdl.validate xml

This will return true if valid or raise an exception with the error message if it's not.

You can get the XML from a Savon response and pass by the following

wsdl = 'path_to_wsdl'
client = Savon::Client.new(wsdl: wsdl)  
response = client.call(:operation, message: { element: 'value' })
WsdlValidator.new(wsdl).validate response.xml
Samuel Garratt
  • 301
  • 3
  • 5