lib/soaspec/rest_handler.rb in soaspec-0.0.24 vs lib/soaspec/rest_handler.rb in soaspec-0.0.25

- old
+ new

@@ -1,9 +1,11 @@ require_relative 'tester' require_relative 'hash_methods' require_relative 'xpath_not_found' +require 'json' +require 'nori' module Soaspec # Wraps around Savon client defining default values dependent on the soap request class RestHandler < Tester # Savon client used to make SOAP calls @@ -16,11 +18,11 @@ { # See request and response. (Put this in traffic file) } end - # Default Savon options. See http://savonrb.com/version2/globals.html for details + # Default Savon options. See https://github.com/rest-client/rest-client for details # @return [Hash] Default Savon options for all BasicSoapHandler def default_options { # method: :get # headers: { content_type: 'text/plain' } @@ -39,16 +41,16 @@ { } end # Setup object to handle communicating with a particular SOAP WSDL - # @param [Hash] specific_options Options defining SOAP request. WSDL, authentication, see http://savonrb.com/version2/globals.html for list of options + # @param [Hash] specific_options Options defining SOAP request. WSDL, authentication def initialize(name, specific_options = {}) options = default_options.merge logging_options options.merge! rest_resource_options options.merge!(specific_options) - @resource = RestClient::Resource.new(base_url, options: options) # @resource[url_extension].get + @resource = RestClient::Resource.new(base_url, options) # @resource[url_extension].get super end def name(name) @test_values = {} @@ -63,15 +65,58 @@ # Used in together with Exchange request that passes such override parameters def make_request(override_parameters) test_values = override_parameters test_values[:params] ||= {} + test_values[:suburl] = test_values[:suburl].to_s if test_values[:suburl] - @resource[test_values[:suburl]].send(test_values[:method].to_s, test_values[:params]) + response = case test_values[:method] + when :post + if test_values[:suburl] + @resource[test_values[:suburl]].send(test_values[:method].to_s, test_values[:payload], test_values[:params]) + else + @resource.send(test_values[:method].to_s, test_values[:payload], test_values[:params]) + end + else + if test_values[:suburl] + @resource[test_values[:suburl]].send(test_values[:method].to_s, test_values[:params]) + else + @resource.send(test_values[:method].to_s, test_values[:params]) + end + end + Soaspec::SpecLogger.add_to(response) + response end def include_in_body?(response, expected) response.body.include? expected + end + + # Convert XML or JSON response into a Hash + def extract_hash(response) + raise "Empty Body. Can't assert on it" if response.body.empty? + type = case response.body[0] + when '<' + :xml + when '{' + :json + else + :unknown + end + + case type + when :json + JSON.parse(response.body).transform_keys_to_symbols + when :xml + parser = Nori.new(:convert_tags_to => lambda { |tag| tag.snakecase.to_sym }) + parser.parse(response.body) + else + raise "Neither XML nor JSON detected. It is #{type}. Don't know how to parse It is #{response.body}" + end + end + + def include_value?(response, expected) + extract_hash(response).include_value? expected end def status_code_for(response) response.code end \ No newline at end of file