lib/soaspec/cucumber/generic_steps.rb in soaspec-0.1.18 vs lib/soaspec/cucumber/generic_steps.rb in soaspec-0.2.0

- old
+ new

@@ -1,49 +1,67 @@ require 'active_support/core_ext/string/inflections' +# @return [Exchange] Return current or last exchange used in Cucumber +def current_exchange + @current_exchange ||= Soaspec.last_exchange +end + +# Pass in the operation (HTTP method or SOAP operation) in first parameter and api name as second. +# API name can be mulitple words and it will be converted to camel case to find the ExchangeHandler class Given 'I am performing a {word} on the {string} API' do |operation, api_name| - @exchange = api_name.camelize.constantize.send operation + @current_exchange = api_name.tr(' ', '_').camelize.constantize.send operation end +# Set a parameter in the request body +# @param [String] Element in request body to set +# @param [String] Value to set it to Given 'I set the {word} to {string}' do |key, value| - @exchange[key] = value + current_exchange[key] = value end +# Add onto the base_url to make a complete url for the test Given 'I use the path {word}' do |suburl| - @exchange.suburl = suburl + current_exchange.suburl = suburl end +# Add a query parameter for http 'get' requests. e.g. will add '?filter_key=filter_value' onto URL Given 'I filter {string} by {string}' do |filter_key, filter_value| transformed_key = filter_key.to_sym - if @exchange.override_parameters[:q] - @exchange.override_parameters[:q][transformed_key] = filter_value + if current_exchange.override_parameters[:q] + current_exchange.override_parameters[:q][transformed_key] = filter_value else - @exchange.override_parameters[:q] = { transformed_key => filter_value } + current_exchange.override_parameters[:q] = { transformed_key => filter_value } end end -# This is getting quite technical +# Add HTTP header 'header_name' as 'header_value' Given 'I set header {string} to {string}' do |header_name, header_value| - if @exchange.override_parameters[:params] - @exchange.override_parameters[:params][header_name] = header_value + if current_exchange.override_parameters[:params] + current_exchange.override_parameters[:params][header_name] = header_value else - @exchange.override_parameters[:params] = { header_name => header_value } + current_exchange.override_parameters[:params] = { header_name => header_value } end end +# Make the API call. This is automatically done when any method extracting a response is made. It can be done +# explicitly here as it is a meaningful step When 'I make the request' do - @exchange.call + current_exchange.call end +# Extract the value from the response that is at the path 'key' and verify it is eq to expected_value Then 'it should have the {word} {string}' do |key, expected_value| - expect(@exchange[key]).to eq expected_value + expect(current_exchange[key]).to eq expected_value end +# Extract the value from the response that is at the path 'key_string' and verify it is eq to expected_value +# Conversion is made on key_string to make it one snake case word Then 'it should have the {string} {string}' do |key_string, expected_value| key = key_string.tr(' ', '_') - actual_value = @exchange.respond_to?(key) ? @exchange.send(key) : @exchange[key] + actual_value = current_exchange.respond_to?(key) ? current_exchange.send(key) : current_exchange[key] expect(actual_value.to_s).to eq expected_value end +# Verify that the HTTP status code is 200..299 and that any defined mandatory elements / mandatory values are as expected Then 'it should be successful' do - expect(200..299).to cover @exchange.status_code + expect(current_exchange).to be_successful end \ No newline at end of file