lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.2.4 vs lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.2.5
- old
+ new
@@ -194,10 +194,11 @@
raise NoElementAtPath, "Path '#{path}' not found in '#{response.body}'" if matching_values.empty?
matching_values.first.first
when :hash
response.dig(path.split('.')) # Use path as Hash dig expression separating params via '.' TODO: Unit test
else
+ raise NoElementAtPath, 'Response is empty' if response.to_s.empty?
response.to_s[/path/] # Perform regular expression using path if not XML nor JSON TODO: Unit test
end
end
# @return [Enumerable] List of values returned from path
@@ -216,11 +217,11 @@
# TODO: This and 'to_hash' method should be merged
# Convert XML or JSON response into a Hash
# @param [String] response Response as a String (either in XML or JSON)
# @return [Hash]
def extract_hash(response)
- raise ArgumentError("Empty Body. Can't assert on it") if response.body.empty?
+ raise NoElementAtPath, "Empty Body. Can't assert on it" if response.body.empty?
case Interpreter.response_type_for response
when :json
converted = JSON.parse(response.body)
return converted.transform_keys_to_symbols if converted.is_a? Hash
return converted.map!(&:transform_keys_to_symbols) if converted.is_a? Array
@@ -255,11 +256,11 @@
private
# Work out data to send based upon payload, template_name, or body
# @return [String] Payload to send in REST request
def post_data(test_values)
- data = if @request_option == :hash && test_values[:body]
+ data = if @request_option == :hash && !test_values[:payload]
test_values[:payload] = JSON.generate(hash_used_in_request(test_values[:body])).to_s
elsif @request_option == :template
test_values = test_values[:body].dup if test_values[:body]
test_values = IndifferentHash.new(test_values) # Allow test_values to be either Symbol or String
Soaspec::TemplateReader.new.render_body(template_name, binding)
@@ -270,10 +271,10 @@
data
end
# @return [Hash] Hash used in REST request based on data conversion
def hash_used_in_request(override_hash)
- request = @default_hash.merge(override_hash)
+ request = override_hash ? @default_hash.merge(override_hash) : @default_hash
if pascal_keys?
request.map { |k, v| [convert_to_pascal_case(k.to_s), v] }.to_h
else
request
end