soaspec 0.2.32 → 0.2.33
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +15 -15
- data/.gitlab-ci.yml +62 -62
- data/.rspec +3 -3
- data/.rubocop.yml +2 -2
- data/CODE_OF_CONDUCT.md +74 -74
- data/ChangeLog +632 -625
- data/Dockerfile +7 -7
- data/Gemfile +8 -8
- data/LICENSE.txt +21 -21
- data/README.md +231 -231
- data/Rakefile +52 -52
- data/Todo.md +16 -16
- data/exe/soaspec +138 -138
- data/exe/xml_to_yaml_file +43 -43
- data/lib/soaspec.rb +106 -105
- data/lib/soaspec/baseline.rb +23 -0
- data/lib/soaspec/core_ext/hash.rb +44 -44
- data/lib/soaspec/cucumber/generic_steps.rb +94 -94
- data/lib/soaspec/demo.rb +6 -6
- data/lib/soaspec/errors.rb +24 -22
- data/lib/soaspec/exchange/exchange.rb +129 -129
- data/lib/soaspec/exchange/exchange_extractor.rb +90 -90
- data/lib/soaspec/exchange/exchange_properties.rb +28 -28
- data/lib/soaspec/exchange/exchange_repeater.rb +21 -21
- data/lib/soaspec/exchange/request_builder.rb +70 -70
- data/lib/soaspec/exchange/variable_storer.rb +24 -24
- data/lib/soaspec/exchange_handlers/exchange_handler.rb +98 -98
- data/lib/soaspec/exchange_handlers/exchange_handler_defaults.rb +61 -61
- data/lib/soaspec/exchange_handlers/handler_accessors.rb +132 -132
- data/lib/soaspec/exchange_handlers/request/rest_request.rb +59 -51
- data/lib/soaspec/exchange_handlers/request/soap_request.rb +41 -41
- data/lib/soaspec/exchange_handlers/response_extractor.rb +84 -84
- data/lib/soaspec/exchange_handlers/rest_exchanger_factory.rb +111 -111
- data/lib/soaspec/exchange_handlers/rest_handler.rb +307 -298
- data/lib/soaspec/exchange_handlers/rest_methods.rb +65 -65
- data/lib/soaspec/exchange_handlers/rest_parameters.rb +112 -112
- data/lib/soaspec/exchange_handlers/rest_parameters_defaults.rb +42 -42
- data/lib/soaspec/exchange_handlers/soap_handler.rb +241 -241
- data/lib/soaspec/exe_helpers.rb +94 -94
- data/lib/soaspec/generate_server.rb +48 -48
- data/lib/soaspec/generator/.rspec.erb +5 -5
- data/lib/soaspec/generator/.travis.yml.erb +5 -5
- data/lib/soaspec/generator/Gemfile.erb +8 -8
- data/lib/soaspec/generator/README.md.erb +29 -29
- data/lib/soaspec/generator/Rakefile.erb +19 -19
- data/lib/soaspec/generator/config/data/default.yml.erb +2 -2
- data/lib/soaspec/generator/css/bootstrap.css +6833 -6833
- data/lib/soaspec/generator/features/support/env.rb.erb +3 -3
- data/lib/soaspec/generator/generate_exchange.html.erb +47 -47
- data/lib/soaspec/generator/lib/blz_service.rb.erb +26 -26
- data/lib/soaspec/generator/lib/dynamic_class_content.rb.erb +12 -12
- data/lib/soaspec/generator/lib/new_rest_service.rb.erb +56 -56
- data/lib/soaspec/generator/lib/new_soap_service.rb.erb +29 -29
- data/lib/soaspec/generator/lib/package_service.rb.erb +2 -2
- data/lib/soaspec/generator/lib/shared_example.rb.erb +8 -8
- data/lib/soaspec/generator/spec/dynamic_soap_spec.rb.erb +12 -12
- data/lib/soaspec/generator/spec/rest_spec.rb.erb +9 -9
- data/lib/soaspec/generator/spec/soap_spec.rb.erb +51 -51
- data/lib/soaspec/generator/spec/spec_helper.rb.erb +23 -23
- data/lib/soaspec/generator/template/soap_template.xml +6 -6
- data/lib/soaspec/indifferent_hash.rb +9 -9
- data/lib/soaspec/interpreter.rb +70 -70
- data/lib/soaspec/matchers.rb +140 -118
- data/lib/soaspec/o_auth2.rb +142 -142
- data/lib/soaspec/soaspec_shared_examples.rb +26 -26
- data/lib/soaspec/spec_logger.rb +143 -143
- data/lib/soaspec/template_reader.rb +30 -30
- data/lib/soaspec/test_server/bank.wsdl +90 -90
- data/lib/soaspec/test_server/get_bank.rb +166 -166
- data/lib/soaspec/test_server/id_manager.rb +41 -41
- data/lib/soaspec/test_server/invoices.rb +29 -29
- data/lib/soaspec/test_server/namespace.xml +14 -14
- data/lib/soaspec/test_server/note.xml +5 -5
- data/lib/soaspec/test_server/puppy_service.rb +21 -21
- data/lib/soaspec/test_server/test_attribute.rb +14 -14
- data/lib/soaspec/test_server/test_namespace.rb +14 -14
- data/lib/soaspec/version.rb +6 -6
- data/lib/soaspec/virtual_server.rb +190 -176
- data/lib/soaspec/wait.rb +43 -43
- data/lib/soaspec/wsdl_generator.rb +215 -215
- data/soaspec.gemspec +58 -58
- data/test.wsdl +116 -116
- data/test.xml +10 -10
- data/test_wsdl.rb +43 -43
- metadata +4 -3
@@ -1,41 +1,41 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Soaspec
|
4
|
-
# Models a request made to a SOAP API
|
5
|
-
class SoapRequest
|
6
|
-
# @example SOAP operation
|
7
|
-
# :login
|
8
|
-
# @return [Symbol] SOAP operation used
|
9
|
-
attr_accessor :operation
|
10
|
-
# Hash representing what will be sent to Savon. Either the exact xml (if using a template)
|
11
|
-
# or a message Hash that will be converted to XML by Savon
|
12
|
-
# @example
|
13
|
-
# { xml: "<env:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ...</env:Body>\n</env:Envelope> " }
|
14
|
-
# @return [Hash] Body of request sent
|
15
|
-
attr_accessor :body
|
16
|
-
# @return [Symbol] Method of building the request (:hash, :template)
|
17
|
-
attr_accessor :request_option
|
18
|
-
# @return [String] Name given to test to describe it
|
19
|
-
attr_accessor :test_name
|
20
|
-
|
21
|
-
# @param [Symbol] operation Soap operation used
|
22
|
-
# @param [Hash] body Hash with method of SOAP generated contained
|
23
|
-
# @param [Symbol] request_option Method of building the request (:hash, :template)
|
24
|
-
def initialize(operation, body, request_option)
|
25
|
-
self.body = body
|
26
|
-
self.operation = operation
|
27
|
-
self.request_option = request_option
|
28
|
-
end
|
29
|
-
|
30
|
-
# @param [String, Symbol] value Message to send to object retrieving a value
|
31
|
-
# @return [Object] Result of retrieving value
|
32
|
-
def [](value)
|
33
|
-
send(value)
|
34
|
-
end
|
35
|
-
|
36
|
-
# @return [String] Show inspection of all parameters
|
37
|
-
def to_s
|
38
|
-
inspect
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Soaspec
|
4
|
+
# Models a request made to a SOAP API
|
5
|
+
class SoapRequest
|
6
|
+
# @example SOAP operation
|
7
|
+
# :login
|
8
|
+
# @return [Symbol] SOAP operation used
|
9
|
+
attr_accessor :operation
|
10
|
+
# Hash representing what will be sent to Savon. Either the exact xml (if using a template)
|
11
|
+
# or a message Hash that will be converted to XML by Savon
|
12
|
+
# @example
|
13
|
+
# { xml: "<env:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ...</env:Body>\n</env:Envelope> " }
|
14
|
+
# @return [Hash] Body of request sent
|
15
|
+
attr_accessor :body
|
16
|
+
# @return [Symbol] Method of building the request (:hash, :template)
|
17
|
+
attr_accessor :request_option
|
18
|
+
# @return [String] Name given to test to describe it
|
19
|
+
attr_accessor :test_name
|
20
|
+
|
21
|
+
# @param [Symbol] operation Soap operation used
|
22
|
+
# @param [Hash] body Hash with method of SOAP generated contained
|
23
|
+
# @param [Symbol] request_option Method of building the request (:hash, :template)
|
24
|
+
def initialize(operation, body, request_option)
|
25
|
+
self.body = body
|
26
|
+
self.operation = operation
|
27
|
+
self.request_option = request_option
|
28
|
+
end
|
29
|
+
|
30
|
+
# @param [String, Symbol] value Message to send to object retrieving a value
|
31
|
+
# @return [Object] Result of retrieving value
|
32
|
+
def [](value)
|
33
|
+
send(value)
|
34
|
+
end
|
35
|
+
|
36
|
+
# @return [String] Show inspection of all parameters
|
37
|
+
def to_s
|
38
|
+
inspect
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -1,84 +1,84 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Soaspec
|
4
|
-
# Enables extracting a response according to type / path
|
5
|
-
module ResponseExtractor
|
6
|
-
# Convert XML or JSON response into a Hash. Doesn't accept empty body
|
7
|
-
# @param [String] response Response as a String (either in XML or JSON)
|
8
|
-
# @return [Hash] Extracted Hash from response
|
9
|
-
def extract_hash(response)
|
10
|
-
raise NoElementAtPath, "Empty Body. Can't assert on it" if response.body.empty?
|
11
|
-
|
12
|
-
case Interpreter.response_type_for response
|
13
|
-
when :xml then parse_xml(response.body.to_s)
|
14
|
-
when :json
|
15
|
-
converted = JSON.parse(response.body)
|
16
|
-
return converted.transform_keys_to_symbols if converted.is_a? Hash
|
17
|
-
return converted.map!(&:transform_keys_to_symbols) if converted.is_a? Array
|
18
|
-
|
19
|
-
raise 'Incorrect Type produced ' + converted.class
|
20
|
-
else
|
21
|
-
raise "Neither XML nor JSON detected. It is #{type}. Don't know how to parse It is #{response.body}"
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
# @param [Object] response Response object
|
26
|
-
# @return [Hash] Hash representing response body
|
27
|
-
def to_hash(response)
|
28
|
-
case Interpreter.response_type_for(response)
|
29
|
-
when :xml then IndifferentHash.new(parse_xml(response.body.to_s))
|
30
|
-
when :json
|
31
|
-
IndifferentHash.new(JSON.parse(response.body.to_s))
|
32
|
-
else
|
33
|
-
raise "Unable to interpret type of '#{response.body}'. Could be because of: #{Interpreter.diagnose_error}"
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
# @param [String] xml XML to convert
|
40
|
-
# @return [Hash] Hash representing XML
|
41
|
-
def parse_xml(xml)
|
42
|
-
parser = Nori.new(strip_namespaces: strip_namespaces?, convert_tags_to: ->(tag) { tag.snakecase.to_sym })
|
43
|
-
parser.parse(xml)
|
44
|
-
end
|
45
|
-
|
46
|
-
# This enables shortcut xpaths to be used. If no '/' is given, one is appended to the start of the path
|
47
|
-
# If attribute value is set then this is also adjusted
|
48
|
-
# @return [String] New Xpath adjusted according to any add ons
|
49
|
-
def prefix_xpath(xpath, attribute)
|
50
|
-
xpath = "//*[@#{attribute}]" unless attribute.nil?
|
51
|
-
if xpath[0] != '/'
|
52
|
-
xpath = convert_to_pascal_case(xpath) if pascal_keys?
|
53
|
-
xpath = '//' + xpath
|
54
|
-
end
|
55
|
-
xpath
|
56
|
-
end
|
57
|
-
|
58
|
-
# Convert snakecase to PascalCase
|
59
|
-
# @return [String] PascalCase converted path
|
60
|
-
def convert_to_pascal_case(key)
|
61
|
-
return key if /[[:upper:]]/ =~ key[0] # If first character already capital, don't do conversion
|
62
|
-
|
63
|
-
key.split('_').map(&:capitalize).join
|
64
|
-
end
|
65
|
-
|
66
|
-
# Find element with name specified or pascal case equivalent
|
67
|
-
# @return [String] String to find paths with pascal converted or given path
|
68
|
-
def add_pascal_path(json_path)
|
69
|
-
return json_path unless pascal_keys?
|
70
|
-
|
71
|
-
json_path.split(',').collect do |sub_path|
|
72
|
-
"#{sub_path},#{convert_to_pascal_case(sub_path)}"
|
73
|
-
end.join(',')
|
74
|
-
end
|
75
|
-
|
76
|
-
# @param [String] json_path Path set from Exchange
|
77
|
-
# @return [String] JSON Path
|
78
|
-
def prefix_json_path(json_path)
|
79
|
-
return json_path if json_path[0] == '$'
|
80
|
-
|
81
|
-
"$..#{json_path}"
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Soaspec
|
4
|
+
# Enables extracting a response according to type / path
|
5
|
+
module ResponseExtractor
|
6
|
+
# Convert XML or JSON response into a Hash. Doesn't accept empty body
|
7
|
+
# @param [String] response Response as a String (either in XML or JSON)
|
8
|
+
# @return [Hash] Extracted Hash from response
|
9
|
+
def extract_hash(response)
|
10
|
+
raise NoElementAtPath, "Empty Body. Can't assert on it" if response.body.empty?
|
11
|
+
|
12
|
+
case Interpreter.response_type_for response
|
13
|
+
when :xml then parse_xml(response.body.to_s)
|
14
|
+
when :json
|
15
|
+
converted = JSON.parse(response.body)
|
16
|
+
return converted.transform_keys_to_symbols if converted.is_a? Hash
|
17
|
+
return converted.map!(&:transform_keys_to_symbols) if converted.is_a? Array
|
18
|
+
|
19
|
+
raise 'Incorrect Type produced ' + converted.class
|
20
|
+
else
|
21
|
+
raise "Neither XML nor JSON detected. It is #{type}. Don't know how to parse It is #{response.body}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# @param [Object] response Response object
|
26
|
+
# @return [Hash] Hash representing response body
|
27
|
+
def to_hash(response)
|
28
|
+
case Interpreter.response_type_for(response)
|
29
|
+
when :xml then IndifferentHash.new(parse_xml(response.body.to_s))
|
30
|
+
when :json
|
31
|
+
IndifferentHash.new(JSON.parse(response.body.to_s))
|
32
|
+
else
|
33
|
+
raise "Unable to interpret type of '#{response.body}'. Could be because of: #{Interpreter.diagnose_error}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
# @param [String] xml XML to convert
|
40
|
+
# @return [Hash] Hash representing XML
|
41
|
+
def parse_xml(xml)
|
42
|
+
parser = Nori.new(strip_namespaces: strip_namespaces?, convert_tags_to: ->(tag) { tag.snakecase.to_sym })
|
43
|
+
parser.parse(xml)
|
44
|
+
end
|
45
|
+
|
46
|
+
# This enables shortcut xpaths to be used. If no '/' is given, one is appended to the start of the path
|
47
|
+
# If attribute value is set then this is also adjusted
|
48
|
+
# @return [String] New Xpath adjusted according to any add ons
|
49
|
+
def prefix_xpath(xpath, attribute)
|
50
|
+
xpath = "//*[@#{attribute}]" unless attribute.nil?
|
51
|
+
if xpath[0] != '/'
|
52
|
+
xpath = convert_to_pascal_case(xpath) if pascal_keys?
|
53
|
+
xpath = '//' + xpath
|
54
|
+
end
|
55
|
+
xpath
|
56
|
+
end
|
57
|
+
|
58
|
+
# Convert snakecase to PascalCase
|
59
|
+
# @return [String] PascalCase converted path
|
60
|
+
def convert_to_pascal_case(key)
|
61
|
+
return key if /[[:upper:]]/ =~ key[0] # If first character already capital, don't do conversion
|
62
|
+
|
63
|
+
key.split('_').map(&:capitalize).join
|
64
|
+
end
|
65
|
+
|
66
|
+
# Find element with name specified or pascal case equivalent
|
67
|
+
# @return [String] String to find paths with pascal converted or given path
|
68
|
+
def add_pascal_path(json_path)
|
69
|
+
return json_path unless pascal_keys?
|
70
|
+
|
71
|
+
json_path.split(',').collect do |sub_path|
|
72
|
+
"#{sub_path},#{convert_to_pascal_case(sub_path)}"
|
73
|
+
end.join(',')
|
74
|
+
end
|
75
|
+
|
76
|
+
# @param [String] json_path Path set from Exchange
|
77
|
+
# @return [String] JSON Path
|
78
|
+
def prefix_json_path(json_path)
|
79
|
+
return json_path if json_path[0] == '$'
|
80
|
+
|
81
|
+
"$..#{json_path}"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -1,111 +1,111 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Soaspec
|
4
|
-
# Convenience methods for once off usage of a REST request
|
5
|
-
module RestExchangeFactory
|
6
|
-
# Make REST Exchange with 'post' method within this Handler context
|
7
|
-
# @param [Hash, String] params Exchange parameters. If String is used it will be for the request payload
|
8
|
-
# @option params [String] :name Name to appear in traffic logs
|
9
|
-
# @option params [Hash] :params Extra parameters (E.g. headers)
|
10
|
-
# @option params [String] :suburl URL appended to base_url of class
|
11
|
-
# @option params [Hash] :q Query for REST
|
12
|
-
# Following are for the body of the request
|
13
|
-
# @option params [Hash] :body Hash to be converted to JSON in request body
|
14
|
-
# @option params [String] :payload String to be passed directly in request body
|
15
|
-
# @option params [String] :template_name Path to file to be read via ERB and passed in request body
|
16
|
-
# @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body
|
17
|
-
def post(params = {})
|
18
|
-
perform_exchange_with(:post, params)
|
19
|
-
end
|
20
|
-
|
21
|
-
# Make REST Exchange with 'patch' method within this Handler context
|
22
|
-
# @param [Hash, String] params Exchange parameters. If String is used it will be for the request payload
|
23
|
-
# @option params [String] :name Name to appear in traffic logs
|
24
|
-
# @option params [Hash] :params Extra parameters (E.g. headers)
|
25
|
-
# @option params [String] suburl URL appended to base_url of class
|
26
|
-
# @option params [Hash] :q Query for REST
|
27
|
-
# Following are for the body of the request
|
28
|
-
# @option params [Hash] :body Hash to be converted to JSON in request body
|
29
|
-
# @option params [String] :payload String to be passed directly in request body
|
30
|
-
# @option params [String] :template_name Path to file to be read via ERB and passed in request body
|
31
|
-
# @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body
|
32
|
-
def patch(params)
|
33
|
-
perform_exchange_with(:patch, params)
|
34
|
-
end
|
35
|
-
|
36
|
-
# Make REST Exchange with 'put' method within this Handler context
|
37
|
-
# @param [Hash, String] params Exchange parameters. If String is used it will be for the request payload
|
38
|
-
# @option params [String] :name Name to appear in traffic logs
|
39
|
-
# @option params [Hash] :params Extra parameters (E.g. headers)
|
40
|
-
# @option params [String] :suburl URL appended to base_url of class
|
41
|
-
# @option params [Hash] :q Query for REST
|
42
|
-
# Following are for the body of the request
|
43
|
-
# @option params [Hash] :body Hash to be converted to JSON in request body
|
44
|
-
# @option params [String] :payload String to be passed directly in request body
|
45
|
-
# @option params [String] :template_name Path to file to be read via ERB and passed in request body
|
46
|
-
# @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body
|
47
|
-
def put(params = {})
|
48
|
-
perform_exchange_with(:put, params)
|
49
|
-
end
|
50
|
-
|
51
|
-
# Make REST Exchange with 'get' method within this Handler context.
|
52
|
-
# If merely a string is passed it will be used as the URL appended to base_url (same as suburl). Otherwise a Hash is expected
|
53
|
-
# @param [Hash, String] params Exchange parameters. If String is used it will be for suburl
|
54
|
-
# @option params [String] :name Name to appear in traffic logs
|
55
|
-
# @option params [String] :suburl URL appended to base_url of class
|
56
|
-
# @option params [Hash] :params Extra parameters (E.g. headers)
|
57
|
-
# @option params [Hash] :q Query for REST
|
58
|
-
# @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body
|
59
|
-
def get(params = {})
|
60
|
-
perform_exchange_with(:get, params)
|
61
|
-
end
|
62
|
-
|
63
|
-
# Make REST Exchange with 'delete' method within this Handler context.
|
64
|
-
# If merely a string is passed it will be used as the URL appended to base_url (same as suburl). Otherwise a Hash is expected
|
65
|
-
# @param [Hash, String] params Exchange parameters. If String is used it will be for suburl
|
66
|
-
# @option params [String] :name Name to appear in traffic logs
|
67
|
-
# @option params [String] :suburl URL appended to base_url of class
|
68
|
-
# @option params [Hash] :q Query for REST
|
69
|
-
# @option params [Hash] :params Extra parameters (E.g. headers)
|
70
|
-
# @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body
|
71
|
-
def delete(params = {})
|
72
|
-
perform_exchange_with(:delete, params)
|
73
|
-
end
|
74
|
-
|
75
|
-
private
|
76
|
-
|
77
|
-
# Make REST Exchange within this Handler context
|
78
|
-
# @param [Symbol] rest_method HTTP rest method to use
|
79
|
-
# @param [Hash, String] params Exchange parameters.
|
80
|
-
# @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body
|
81
|
-
def perform_exchange_with(rest_method, params = {})
|
82
|
-
params = determine_params_for(rest_method, params)
|
83
|
-
params[:name] ||= rest_method.to_s
|
84
|
-
exchange_params = { name: params[:name] }
|
85
|
-
if params[:template_name]
|
86
|
-
exchange_params[:template_name] = params[:template_name]
|
87
|
-
params.delete :template_name
|
88
|
-
end
|
89
|
-
new(exchange_params)
|
90
|
-
exchange = Exchange.new(params[:name], method: rest_method, **params)
|
91
|
-
yield exchange if block_given?
|
92
|
-
exchange
|
93
|
-
end
|
94
|
-
|
95
|
-
# @param [Symbol] method HTTP rest method to use
|
96
|
-
# @param [Hash, String] params Exchange parameters.
|
97
|
-
# @return [Hash] Exchange Parameters after setting shorthand parameters
|
98
|
-
def determine_params_for(method, params)
|
99
|
-
return params if params.is_a? Hash
|
100
|
-
|
101
|
-
case method
|
102
|
-
when :get, :delete
|
103
|
-
{ suburl: params.to_s }
|
104
|
-
when :post, :put, :patch
|
105
|
-
{ payload: params.to_s }
|
106
|
-
else
|
107
|
-
raise "'#{params}' needs to be a 'Hash' but is a #{params.class}"
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Soaspec
|
4
|
+
# Convenience methods for once off usage of a REST request
|
5
|
+
module RestExchangeFactory
|
6
|
+
# Make REST Exchange with 'post' method within this Handler context
|
7
|
+
# @param [Hash, String] params Exchange parameters. If String is used it will be for the request payload
|
8
|
+
# @option params [String] :name Name to appear in traffic logs
|
9
|
+
# @option params [Hash] :params Extra parameters (E.g. headers)
|
10
|
+
# @option params [String] :suburl URL appended to base_url of class
|
11
|
+
# @option params [Hash] :q Query for REST
|
12
|
+
# Following are for the body of the request
|
13
|
+
# @option params [Hash] :body Hash to be converted to JSON in request body
|
14
|
+
# @option params [String] :payload String to be passed directly in request body
|
15
|
+
# @option params [String] :template_name Path to file to be read via ERB and passed in request body
|
16
|
+
# @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body
|
17
|
+
def post(params = {})
|
18
|
+
perform_exchange_with(:post, params)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Make REST Exchange with 'patch' method within this Handler context
|
22
|
+
# @param [Hash, String] params Exchange parameters. If String is used it will be for the request payload
|
23
|
+
# @option params [String] :name Name to appear in traffic logs
|
24
|
+
# @option params [Hash] :params Extra parameters (E.g. headers)
|
25
|
+
# @option params [String] suburl URL appended to base_url of class
|
26
|
+
# @option params [Hash] :q Query for REST
|
27
|
+
# Following are for the body of the request
|
28
|
+
# @option params [Hash] :body Hash to be converted to JSON in request body
|
29
|
+
# @option params [String] :payload String to be passed directly in request body
|
30
|
+
# @option params [String] :template_name Path to file to be read via ERB and passed in request body
|
31
|
+
# @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body
|
32
|
+
def patch(params)
|
33
|
+
perform_exchange_with(:patch, params)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Make REST Exchange with 'put' method within this Handler context
|
37
|
+
# @param [Hash, String] params Exchange parameters. If String is used it will be for the request payload
|
38
|
+
# @option params [String] :name Name to appear in traffic logs
|
39
|
+
# @option params [Hash] :params Extra parameters (E.g. headers)
|
40
|
+
# @option params [String] :suburl URL appended to base_url of class
|
41
|
+
# @option params [Hash] :q Query for REST
|
42
|
+
# Following are for the body of the request
|
43
|
+
# @option params [Hash] :body Hash to be converted to JSON in request body
|
44
|
+
# @option params [String] :payload String to be passed directly in request body
|
45
|
+
# @option params [String] :template_name Path to file to be read via ERB and passed in request body
|
46
|
+
# @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body
|
47
|
+
def put(params = {})
|
48
|
+
perform_exchange_with(:put, params)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Make REST Exchange with 'get' method within this Handler context.
|
52
|
+
# If merely a string is passed it will be used as the URL appended to base_url (same as suburl). Otherwise a Hash is expected
|
53
|
+
# @param [Hash, String] params Exchange parameters. If String is used it will be for suburl
|
54
|
+
# @option params [String] :name Name to appear in traffic logs
|
55
|
+
# @option params [String] :suburl URL appended to base_url of class
|
56
|
+
# @option params [Hash] :params Extra parameters (E.g. headers)
|
57
|
+
# @option params [Hash] :q Query for REST
|
58
|
+
# @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body
|
59
|
+
def get(params = {})
|
60
|
+
perform_exchange_with(:get, params)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Make REST Exchange with 'delete' method within this Handler context.
|
64
|
+
# If merely a string is passed it will be used as the URL appended to base_url (same as suburl). Otherwise a Hash is expected
|
65
|
+
# @param [Hash, String] params Exchange parameters. If String is used it will be for suburl
|
66
|
+
# @option params [String] :name Name to appear in traffic logs
|
67
|
+
# @option params [String] :suburl URL appended to base_url of class
|
68
|
+
# @option params [Hash] :q Query for REST
|
69
|
+
# @option params [Hash] :params Extra parameters (E.g. headers)
|
70
|
+
# @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body
|
71
|
+
def delete(params = {})
|
72
|
+
perform_exchange_with(:delete, params)
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
# Make REST Exchange within this Handler context
|
78
|
+
# @param [Symbol] rest_method HTTP rest method to use
|
79
|
+
# @param [Hash, String] params Exchange parameters.
|
80
|
+
# @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body
|
81
|
+
def perform_exchange_with(rest_method, params = {})
|
82
|
+
params = determine_params_for(rest_method, params)
|
83
|
+
params[:name] ||= rest_method.to_s
|
84
|
+
exchange_params = { name: params[:name] }
|
85
|
+
if params[:template_name]
|
86
|
+
exchange_params[:template_name] = params[:template_name]
|
87
|
+
params.delete :template_name
|
88
|
+
end
|
89
|
+
new(exchange_params)
|
90
|
+
exchange = Exchange.new(params[:name], method: rest_method, **params)
|
91
|
+
yield exchange if block_given?
|
92
|
+
exchange
|
93
|
+
end
|
94
|
+
|
95
|
+
# @param [Symbol] method HTTP rest method to use
|
96
|
+
# @param [Hash, String] params Exchange parameters.
|
97
|
+
# @return [Hash] Exchange Parameters after setting shorthand parameters
|
98
|
+
def determine_params_for(method, params)
|
99
|
+
return params if params.is_a? Hash
|
100
|
+
|
101
|
+
case method
|
102
|
+
when :get, :delete
|
103
|
+
{ suburl: params.to_s }
|
104
|
+
when :post, :put, :patch
|
105
|
+
{ payload: params.to_s }
|
106
|
+
else
|
107
|
+
raise "'#{params}' needs to be a 'Hash' but is a #{params.class}"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|