Class: Soaspec::SoapHandler

Inherits:
ExchangeHandler show all
Extended by:
Forwardable, SoapAccessors
Defined in:
lib/soaspec/exchange_handlers/soap_handler.rb

Overview

Wraps around Savon client defining default values dependent on the soap request

Instance Attribute Summary collapse

Attributes inherited from ExchangeHandler

#template_name

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SoapAccessors

root_attributes

Methods inherited from ExchangeHandler

#convert_to_lower?, #default_hash=, #elements, #expected_mandatory_elements, #expected_mandatory_json_values, #expected_mandatory_xpath_values, #request, #set_remove_key, #set_remove_keys, #store, #strip_namespaces?, #to_s, #use

Methods included from HandlerAccessors

#attribute, #convert_to_lower, #default_hash, #element, #mandatory_elements, #mandatory_json_values, #mandatory_xpath_values, #strip_namespaces, #template_name

Constructor Details

#initialize(name = self.class.to_s, options = {}) ⇒ SoapHandler

Setup object to handle communicating with a particular SOAP WSDL

Parameters:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 75

def initialize(name = self.class.to_s, options = {})
  if name.is_a?(Hash) && options == {} # If name is not set
    options = name
    name = self.class.to_s
  end
  super
  set_remove_key(options, :operation)
  set_remove_key(options, :default_hash)
  set_remove_key(options, :template_name)
  merged_options = Soaspec.log_api_traffic? ? default_options.merge(logging_options) : default_options
  merged_options.merge! savon_options
  merged_options.merge!(options)
  self.client = Savon.client(merged_options)
end

Instance Attribute Details

#clientObject

Savon client used to make SOAP calls



29
30
31
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 29

def client
  @client
end

#operationObject

SOAP Operation to use by default



31
32
33
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 31

def operation
  @operation
end

Class Method Details

.method_missing(method_name, *args, &block) ⇒ Object

Implement undefined setter with []= for FactoryBot to use without needing to define params to set

Parameters:

  • method_name (Object)

    Name of method not defined

  • args (Object)

    Arguments passed to method

  • block (Object)


214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 214

def method_missing(method_name, *args, &block)
  tmp_class = new(method_name)
  operations = tmp_class.operations
  if operations.include? 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

.respond_to_missing?(method_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


228
229
230
231
232
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 228

def respond_to_missing?(method_name, *args)
  tmp_class = new(args)
  operations = tmp_class.operations
  operations.include?(method_name) || super
end

Instance Method Details

#convert_to_lower_case(xml_doc) ⇒ Object

Convert all XML nodes to lowercase

Parameters:

  • (Nokogiri::XML::Document)


153
154
155
156
157
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 153

def convert_to_lower_case(xml_doc)
  xml_doc.traverse do |node|
    node.name = node.name.downcase if node.kind_of?(Nokogiri::XML::Element)
  end
end

#default_optionsHash

Default Savon options. See savonrb.com/version2/globals.html for details

Returns:

  • (Hash)

    Default Savon options for all BasicSoapHandler



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 50

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

#found?(response) ⇒ Boolean

Returns Whether the request found the desired value or not.

Returns:

  • (Boolean)

    Whether the request found the desired value or not



127
128
129
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 127

def found?(response)
  status_code_for(response) != 404
end

#include_in_body?(response, expected) ⇒ Boolean

Returns Whether response includes provided string within it.

Returns:

  • (Boolean)

    Whether response includes provided string within it



139
140
141
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 139

def include_in_body?(response, expected)
  response.to_xml.to_s.include? expected
end

#include_key?(response, expected) ⇒ Boolean

Returns Whether response body contains expected key.

Parameters:

  • expected (Symbol)

Returns:

  • (Boolean)

    Whether response body contains expected key



145
146
147
148
149
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 145

def include_key?(response, expected)
  body = response.body
  body.extend Hashie::Extensions::DeepFind
  !body.deep_find_all(expected).empty?
end

#include_value?(response, expected_value) ⇒ Boolean

Returns Whether any of the keys of the Body Hash include value.

Returns:

  • (Boolean)

    Whether any of the keys of the Body Hash include value



198
199
200
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 198

def include_value?(response, expected_value)
  response.body.include_value?(expected_value)
end

#logging_optionsObject

Options to log xml request and response



39
40
41
42
43
44
45
46
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 39

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

#make_request(request_parameters) ⇒ Object

Used in together with Exchange request that passes such override parameters

Parameters:

  • request_parameters (Hash)

    Parameters used to overwrite defaults in request



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 98

def make_request(request_parameters)
  test_values = request_body_params request_parameters
  # Call the SOAP operation with the request XML provided
  begin
    if @request_option == :template
      test_values = IndifferentHash.new(test_values) # Allow test_values to be either Symbol or String
      client.call(operation, xml: Soaspec::TemplateReader.new.render_body(template_name, binding))
    elsif @request_option == :hash
      client.call(operation, message: @default_hash.merge(test_values), attributes: request_root_attributes)
    end
  rescue Savon::HTTPError => soap_error
    soap_error
  end
end

#request_body_params(request_parameters) ⇒ Object

Used in making request via hash or in template via Erb



91
92
93
94
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 91

def request_body_params(request_parameters)
  test_values = request_parameters[:body] || request_parameters
  test_values.transform_keys_to_symbols if Soaspec.always_use_keys?
end

#request_root_attributesObject

Attributes set at the root XML element of SOAP request



34
35
36
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 34

def request_root_attributes
  nil
end

#response_body(response, format: :hash) ⇒ Object

Returns Generic body to be displayed in error messages.

Parameters:

  • format (Hash) (defaults to: :hash)

    Format of expected result

Returns:

  • (Object)

    Generic body to be displayed in error messages



115
116
117
118
119
120
121
122
123
124
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 115

def response_body(response, format: :hash)
  case format
  when :hash
    response.body
  when :raw
    response.xml
  else
    response.body
  end
end

#savon_optionsHash

Add values to here when extending this class to have default Savon options. See savonrb.com/version2/globals.html for details

Returns:

  • (Hash)

    Savon options adding to & overriding defaults



68
69
70
71
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 68

def savon_options
  {
  }
end

#status_code_for(response) ⇒ Integer

Response status code for response. ‘200’ indicates a success

Parameters:

  • response (Savon::Response)

Returns:

  • (Integer)

    Status code



134
135
136
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 134

def status_code_for(response)
  response.http.code
end

#to_hash(response) ⇒ Object

Hash of response body



203
204
205
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 203

def to_hash(response)
  response.body
end

#value_from_path(response, path, attribute: nil) ⇒ String

Based on a exchange, return the value at the provided xpath If the path does not begin with a ‘/’, a ‘//’ is added to it

Parameters:

  • response (Savon::Response)
  • path (String)

    Xpath

  • attribute (String) (defaults to: nil)

    Generic attribute to find. Will override path

Returns:

  • (String)

    Value at Xpath

Raises:



183
184
185
186
187
188
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 183

def value_from_path(response, path, attribute: nil)
  results = xpath_elements_for(response: response, xpath: path, attribute: attribute)
  raise NoElementAtPath, "No value at Xpath '#{path}' in XML #{response.doc}" if results.empty?
  return results.first.inner_text if attribute.nil?
  results.first.attributes[attribute].inner_text
end

#values_from_path(response, path, attribute: nil) ⇒ Enumerable

Returns List of values returned from path.

Returns:

  • (Enumerable)

    List of values returned from path



191
192
193
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 191

def values_from_path(response, path, attribute: nil)
  xpath_elements_for(response: response, xpath: path, attribute: attribute).map(&:inner_text)
end

#xpath_elements_for(response: nil, xpath: nil, attribute: nil) ⇒ Enumerable

Returns the value at the provided xpath

Parameters:

  • response (Savon::Response) (defaults to: nil)
  • xpath (String) (defaults to: nil)

Returns:

  • (Enumerable)

    Elements found through Xpath



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 163

def xpath_elements_for(response: nil, xpath: nil, attribute: nil)
  raise ArgumentError('response and xpath must be passed to method') unless response && xpath
  xpath = "//*[@#{attribute}]" unless attribute.nil?
  xpath = '//' + xpath if xpath[0] != '/'
  temp_doc = response.doc.dup
  convert_to_lower_case(temp_doc) if convert_to_lower?
  if strip_namespaces? && !xpath.include?(':')
    temp_doc.remove_namespaces!
    temp_doc.xpath(xpath)
  else
    temp_doc.xpath(xpath, temp_doc.collect_namespaces)
  end
end