问题
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
回答1:
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
回答2:
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
来源:https://stackoverflow.com/questions/29240559/validate-xml-response-against-wsdl-using-ruby-savon