lib/soaspec/exchange_handlers/soap_handler.rb in soaspec-0.3.2 vs lib/soaspec/exchange_handlers/soap_handler.rb in soaspec-0.3.3

- old
+ new

@@ -4,10 +4,12 @@ require_relative '../core_ext/hash' require_relative '../errors' require_relative 'handler_accessors' require_relative '../interpreter' require_relative 'request/soap_request' +require_relative 'soap_globals' +require_relative 'soap_defaults' require 'forwardable' module Soaspec # Accessors specific to SOAP handler module SoapAccessors @@ -20,18 +22,30 @@ # Wraps around Savon client defining default values dependent on the soap request class SoapHandler < ExchangeHandler extend Soaspec::SoapAccessors extend Forwardable + extend Soaspec::SoapGlobals delegate [:operations] => :client # Savon client used to make SOAP calls attr_accessor :client # SOAP Operation to use by default attr_accessor :operation + # @return [Hash] List of Savon globals + attr_writer :savon_globals + def savon_globals + @savon_globals = {} + %i[wsdl basic_auth soap_version raise_errors namespace namespaces endpoint + ssl_verify_mode].each do |global| + @savon_globals.merge!(global => send(global)) if respond_to? global + end + @savon_globals + end + # Attributes set at the root XML element of SOAP request def request_root_attributes; end # Options to log xml request and response def logging_options @@ -41,27 +55,10 @@ logger: Soaspec::SpecLogger.create, pretty_print_xml: true # Prints XML pretty } end - # Default Savon options. See http://savonrb.com/version2/globals.html for details - # @example 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' - # @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 - } - 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 {} @@ -75,18 +72,21 @@ options = name name = self.class.to_s end super set_remove_keys(options, %i[operation default_hash template_name]) - merged_options = Soaspec::SpecLogger.log_api_traffic? ? default_options.merge(logging_options) : default_options + merged_options = SoapDefaults.options + merged_options.merge!(logging_options) if Soaspec::SpecLogger.log_api_traffic? merged_options.merge! savon_options + puts 'globals' + savon_globals.to_s + merged_options.merge! savon_globals merged_options.merge!(options) self.client = Savon.client(merged_options) end # @param [Hash] override_parameters Parameters for building the request - # @return [Hash] Parameters used in making a request + # @return [SoapRequest] Parameters used in making a request def request_parameters(override_parameters) SoapRequest.new(operation, request_body_params(override_parameters), @request_option) end # Used in making request via hash or in template via Erb @@ -95,11 +95,11 @@ def request_body_params(request_parameters) test_values = request_parameters[:body] || request_parameters test_values.transform_keys_to_symbols if Soaspec.always_use_keys? if @request_option == :template test_values = IndifferentHash.new(test_values) # Allow test_values to be either Symbol or String - { xml: Soaspec::TemplateReader.new.render_body(template_name, binding) } + { xml: Soaspec::TemplateReader.new.render_body(template_name, test_values) } elsif @request_option == :hash { message: @default_hash.merge(test_values), attributes: request_root_attributes } end end @@ -109,11 +109,11 @@ # Call the SOAP operation with the request XML provided request = request_parameters(request_parameters) begin client.call request.operation, request.body # request_body_params(request_parameters) rescue Savon::HTTPError => e - soap_error + e end end # @param [Hash] format Format of expected result # @return [Object] Generic body to be displayed in error messages @@ -211,30 +211,35 @@ response.body end # Convenience methods for once off usage of a SOAP request class << self + # @return [Array] List of operations WSDL can perform + def operations + operation_class = new('Get operations') + operation_class.operations + end + # Implement undefined setter with []= for FactoryBot to use without needing to define params to set # @param [Object] method_name Name of method not defined # @param [Object] args Arguments passed to method # @param [Object] block def method_missing(method_name, *args, &block) - tmp_class = new(method_name) - operations = tmp_class.operations if operations.include? method_name + tmp_class = new(method_name) tmp_class.operation = method_name exchange = Exchange.new(method_name, *args) exchange.exchange_handler = tmp_class yield exchange if block_given? exchange else super end end + # Override respond_to_missing so that any method name that is an operation + # can be used def respond_to_missing?(method_name, *args) - tmp_class = new(args) - operations = tmp_class.operations operations.include?(method_name) || super end end end end