Class: Soaspec::BasicSoapHandler

Inherits:
Tester
  • Object
show all
Defined in:
lib/soaspec/basic_soap_handler.rb

Overview

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

Instance Attribute Summary collapse

Attributes inherited from Tester

#template_name

Instance Method Summary collapse

Methods inherited from Tester

#to_s

Constructor Details

#initialize(name, specific_options = {}) ⇒ BasicSoapHandler

Setup object to handle communicating with a particular SOAP WSDL

Parameters:



51
52
53
54
55
56
57
# File 'lib/soaspec/basic_soap_handler.rb', line 51

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

Instance Attribute Details

#clientObject

Savon client used to make SOAP calls



10
11
12
# File 'lib/soaspec/basic_soap_handler.rb', line 10

def client
  @client
end

#operationObject

SOAP Operation to use by default



12
13
14
# File 'lib/soaspec/basic_soap_handler.rb', line 12

def operation
  @operation
end

Instance Method Details

#default_hash=(hash) ⇒ Object



84
85
86
87
# File 'lib/soaspec/basic_soap_handler.rb', line 84

def default_hash=(hash)
  @request_option = :hash
  @default_hash = Soaspec::Environment.always_use_keys? ? hash.transform_keys_to_symbols : hash
end

#default_optionsHash

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

Returns:

  • (Hash)

    Default Savon options for all BasicSoapHandler



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/soaspec/basic_soap_handler.rb', line 26

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

#include_in_body?(response, expected) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/soaspec/basic_soap_handler.rb', line 107

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

#logging_optionsObject

Options to log xml request and response



15
16
17
18
19
20
21
22
# File 'lib/soaspec/basic_soap_handler.rb', line 15

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



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/soaspec/basic_soap_handler.rb', line 71

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: root_attributes)
  end
end

#mandatory_elementsArray

Override this to specify elements that must be present in the response Will be used in ‘success_scenarios’ shared examples

Returns:

  • (Array)

    Array of symbols specifying element names



96
97
98
# File 'lib/soaspec/basic_soap_handler.rb', line 96

def mandatory_elements
  []
end

#mandatory_xpath_valuesHash

Override this to specify xpath results that must be present in the response Will be used in ‘success_scenarios’ shared examples

Returns:

  • (Hash)

    Hash of ‘xpath’ => ‘expected value’ pairs



103
104
105
# File 'lib/soaspec/basic_soap_handler.rb', line 103

def mandatory_xpath_values
  {}
end

#name(name) ⇒ Object



59
60
61
62
63
# File 'lib/soaspec/basic_soap_handler.rb', line 59

def name(name)
  @test_values = {}
  @test_name = name
  self
end

#override(request_parameters) ⇒ Object



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

def override(request_parameters)
  @test_values = request_parameters
  self
end

#root_attributesObject

Attributes set at the root XML element of SOAP request



112
113
114
# File 'lib/soaspec/basic_soap_handler.rb', line 112

def root_attributes
  nil
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



44
45
46
47
# File 'lib/soaspec/basic_soap_handler.rb', line 44

def savon_options
  {
  }
end

#status_code_for(response) ⇒ Object



89
90
91
# File 'lib/soaspec/basic_soap_handler.rb', line 89

def status_code_for(response)
  response.http.code
end

#value_from_path(exchange, path) ⇒ Object



130
131
132
133
# File 'lib/soaspec/basic_soap_handler.rb', line 130

def value_from_path(exchange, path)
  path = '//' + path if path[0] != '/'
  xpath_value_for(exchange: exchange, xpath: path)
end

#xpath_value_for(param) ⇒ Object

Returns the value at the provided xpath

Raises:



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/soaspec/basic_soap_handler.rb', line 117

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