lib/soaspec/exchange.rb in soaspec-0.0.57 vs lib/soaspec/exchange.rb in soaspec-0.0.58
- old
+ new
@@ -5,10 +5,12 @@
# Class of Api Handler for which this exchange is made
attr_reader :api_class
# How many times to retry for a success
attr_accessor :retry_count
+ # Params used when making a request
+ attr_accessor :default_params
# Set retry for success variable to true so that request will be retried
# for retry_count until it's true
def retry_for_success
@retry_for_success = true
@@ -33,17 +35,28 @@
@api_class.__send__(element, response) # Forward the call onto handler to retrieve the element for the response
end
end
end
+ # Merge exchange initialized request params with ones set later on
+ def merge_request_body
+ if @override_parameters[:body]
+ @override_parameters[:body].merge!(default_params[:body])
+ @override_parameters
+ else
+ @override_parameters.merge(default_params[:body])
+ end
+ end
+
# Make request to handler with parameters defined
# Will retry until success code reached if retry_for_success? is set
# @return [Response] Response from Api handler
def make_request
Soaspec::SpecLogger.add_to 'Example ' + @test_name
+ request_params = default_params ? merge_request_body : @override_parameters
retry_count.times do
- response = @api_class.make_request(@override_parameters)
+ response = @api_class.make_request(request_params)
return response unless retry_for_success?
return response if (200..299).cover? @api_class.status_code_for(response)
response
end
end
@@ -77,10 +90,12 @@
# response.header (head of response as Hash)
def response
@response ||= make_request
end
+ alias call response
+
# Get status code from api class. This is http response for Web Api
# @return [Integer] Status code from api class
def status_code
@api_class.status_code_for(response)
end
@@ -99,8 +114,16 @@
# Extract value from path api class
# @param [Object] path Path to return element for api class E.g - for SOAP this is XPath string. For JSON, this is Hash dig Array
# @return [String] Value at path
def [](path)
@api_class.value_from_path(response, path.to_s)
+ end
+
+ # Set a parameter request in the request body.
+ # Can be used to build a request over several steps (e.g Cucumber)
+ # Will be used with FactoryBot
+ def []=(key, value)
+ self.default_params = { body: {} } unless default_params # Initialize as Hash if not set
+ default_params[:body][key] = value
end
end
\ No newline at end of file