Class: Soaspec::SoapHandler

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

Overview

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

Direct Known Subclasses

BasicSoapHandler

Instance Attribute Summary collapse

Attributes inherited from ExchangeHandler

#template_name

Instance Method Summary collapse

Methods included from SoapAccessors

root_attributes

Methods inherited from ExchangeHandler

#default_hash=, #elements, #expected_mandatory_elements, #expected_mandatory_json_values, #expected_mandatory_xpath_values, #set_remove_key, #store, #to_s, #use

Methods included from HandlerAccessors

#attribute, #element, #mandatory_elements, #mandatory_json_values, #mandatory_xpath_values

Constructor Details

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

Setup object to handle communicating with a particular SOAP WSDL



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

def initialize(name = self.class.to_s, options = {})
  @default_hash = {}
  @request_option = :hash
  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 = default_options.merge logging_options
  merged_options.merge! savon_options
  merged_options.merge!(options)
  @client = Savon.client(merged_options)
end

Instance Attribute Details

#clientObject

Savon client used to make SOAP calls



26
27
28
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 26

def client
  @client
end

#operationObject

SOAP Operation to use by default



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

def operation
  @operation
end

Instance Method Details

#default_optionsHash

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 47

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



120
121
122
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 120

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

#include_in_body?(response, expected) ⇒ Boolean



132
133
134
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 132

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

#include_key?(response, expected) ⇒ Boolean



137
138
139
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 137

def include_key?(response, expected)
  response.body.include_key? expected
end

#include_value?(response, expected_value) ⇒ Boolean

Whether any of the keys of the Body Hash include value



174
175
176
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 174

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

#logging_optionsObject

Options to log xml request and response



36
37
38
39
40
41
42
43
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 36

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(override_parameters) ⇒ Object

Used in together with Exchange request that passes such override parameters



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 90

def make_request(override_parameters)
  test_values = override_parameters # Used in making request via hash or in template via Erb
  test_values = test_values.transform_keys_to_symbols if Soaspec.always_use_keys?
  begin
    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
  rescue Savon::HTTPError => e
    e
  end
end

#request_root_attributesObject

Attributes set at the root XML element of SOAP request



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

def request_root_attributes
  nil
end

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



108
109
110
111
112
113
114
115
116
117
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 108

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



65
66
67
68
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 65

def savon_options
  {
  }
end

#status_code_for(response) ⇒ Integer

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



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

def status_code_for(response)
  response.http.code
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



167
168
169
170
171
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 167

def value_from_path(response, path, attribute: nil)
  path = "//*[@#{attribute}]" unless attribute.nil?
  path = '//' + path if path[0] != '/'
  xpath_value_for(response: response, xpath: path, attribute: attribute)
end

#xpath_value_for(response: nil, xpath: nil, attribute: nil) ⇒ String

Returns the value at the provided xpath

Raises:



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/soaspec/exchange_handlers/soap_handler.rb', line 146

def xpath_value_for(response: nil, xpath: nil, attribute: nil)
  raise ArgumentError('response and xpath must be passed to method') unless response && xpath
  result =
    if Soaspec.strip_namespaces? && !xpath.include?(':')
      temp_doc = response.doc.dup
      temp_doc.remove_namespaces!
      temp_doc.at_xpath(xpath)
    else
      response.xpath(xpath).first # Note this is Savon's xpath method. Hence Nokogiri 'at_xpath' not used
    end
  raise NoElementAtPath, "No value at Xpath '#{xpath}' in XML #{response.doc}" unless result
  return result.inner_text if attribute.nil?
  result.attributes[attribute].inner_text
end