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:



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

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



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

def client
  @client
end

#operationObject

SOAP Operation to use by default



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

def operation
  @operation
end

Instance Method Details

#default_hash=(hash) ⇒ Object



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

def default_hash=(hash)
  @request_option = :hash
  @default_hash = hash
end

#default_optionsHash

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

Returns:

  • (Hash)

    Default Savon options for all BasicSoapHandler



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

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

#logging_optionsObject

Options to log xml request and response



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

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



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

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 # Either string or symbol
  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



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

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



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

def mandatory_xpath_values
  {}
end

#name(name) ⇒ Object



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

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

#override(request_parameters) ⇒ Object



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

def override(request_parameters)
  @test_values = request_parameters
  self
end

#root_attributesObject

Attributes set at the root XML element of SOAP request



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

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



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

def savon_options
  {
  }
end

#status_code_for(response) ⇒ Object



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

def status_code_for(response)
  response.http.code
end

#value_from_path(exchange, path) ⇒ Object



117
118
119
120
# File 'lib/soaspec/basic_soap_handler.rb', line 117

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

#xpath_value_for(param) ⇒ Object



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

def xpath_value_for(param)
  result = param[:exchange].response.xpath(param[:xpath]).first
  raise 'No value at Xpath' unless result
  result.inner_text
end