Validate XML response against WSDL using Ruby Savon

杀马特。学长 韩版系。学妹 提交于 2019-12-12 14:32:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!