require_relative 'tester' require_relative 'hash_methods' require_relative 'xpath_not_found' require_relative 'accessors' module Soaspec # Accessors specific to SOAP handler module SoapAccessors def root_attributes(attributes) define_method('request_root_attributes') do attributes end end end # Wraps around Savon client defining default values dependent on the soap request class BasicSoapHandler < Tester extend Soaspec::Accessors extend Soaspec::SoapAccessors # Savon client used to make SOAP calls attr_accessor :client # SOAP Operation to use by default attr_accessor :operation # Attributes set at the root XML element of SOAP request def request_root_attributes nil end # Options to log xml request and response def logging_options { log: true, # See request and response. (Put this in traffic file) log_level: :debug, logger: Soaspec::SpecLogger.create, pretty_print_xml: true # Prints XML pretty } end # Default Savon options. See http://savonrb.com/version2/globals.html for details # @return [Hash] Default Savon options for all BasicSoapHandler def default_options { ssl_verify_mode: :none, # Easier for testing. Not so secure follow_redirects: true, # Necessary for many API calls soap_version: 2, # use SOAP 1.2. You will get 415 error if this is incorrect raise_errors: false # HTTP errors not cause failure as often negative test scenarios expect not 200 response # Things could go wrong if not set properly # env_namespace: :soap, # Change environment namespace # namespace_identifier: :tst, # Change namespace element # element_form_default: :qualified # Populate each element with namespace # namespace: 'http://Extended_namespace.xsd' change root namespace # basic_auth: 'user', 'password' } end # Add values to here when extending this class to have default Savon options. # See http://savonrb.com/version2/globals.html for details # @return [Hash] Savon options adding to & overriding defaults def savon_options { } end # Setup object to handle communicating with a particular SOAP WSDL # @param [Hash] specific_options Options defining SOAP request. WSDL, authentication, see http://savonrb.com/version2/globals.html for list of options def initialize(name, specific_options = {}) options = default_options.merge logging_options options.merge! savon_options options.merge!(specific_options) @client = Savon.client(options) super end def name(name) @test_values = {} @test_name = name self end def override(request_parameters) @test_values = request_parameters self end # Used in together with Exchange request that passes such override parameters def make_request(override_parameters) test_values = override_parameters # Used in Erb # Erb parses template file, executing Ruby code in `<% %>` blocks to work out final request test_values = test_values.transform_keys_to_symbols if Soaspec::Environment.always_use_keys? if @request_option == :template request_body = File.read('template/' + template_name + '.xml') render_body = ERB.new(request_body).result(binding) @client.call(operation, xml: render_body) # Call the SOAP operation with the request XML provided elsif @request_option == :hash @client.call(operation, message: @default_hash.merge(test_values), attributes: request_root_attributes) end end def default_hash=(hash) @request_option = :hash @default_hash = Soaspec::Environment.always_use_keys? ? hash.transform_keys_to_symbols : hash end def status_code_for(response) response.http.code end def include_in_body?(response, expected) response.to_xml.to_s.include? expected end # Returns the value at the provided xpath def xpath_value_for(param) result = if Soaspec::Environment.strip_namespaces? && !param[:xpath].include?(':') temp_doc = param[:exchange].response.doc temp_doc.remove_namespaces! temp_doc.xpath(param[:xpath]).first else param[:exchange].response.xpath(param[:xpath]).first end raise NoElementAtXpath, "No value at Xpath '#{param[:xpath]}'" unless result result.inner_text end def value_from_path(exchange, path) path = '//' + path if path[0] != '/' xpath_value_for(exchange: exchange, xpath: path) end # Whether any of the keys of the Body Hash include value def include_value?(response, expected_value) response.body.include_value?(expected_value) end end end