diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..6f1d8af --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,49 @@ +require: + - rubocop-performance + - rubocop-rake + - rubocop-rspec + +AllCops: + TargetRubyVersion: 2.7 + NewCops: enable + +### Metrics ### + +Metrics: + Exclude: + - lib/airborne/request_expectations.rb + +Metrics/MethodLength: + CountAsOne: + - hash + +### Style ### + +Style/Documentation: + Enabled: false + +Style/SymbolArray: + EnforcedStyle: brackets + +### RSpec ### + +RSpec/ContextWording: + Enabled: false + +RSpec/DescribeClass: + Enabled: false + +RSpec/ExampleLength: + Enabled: false + +RSpec/MultipleDescribes: + Enabled: false + +RSpec/MultipleExpectations: + Enabled: false + +RSpec/NotToNot: + EnforcedStyle: to_not + +RSpec/RepeatedDescription: + Enabled: false diff --git a/Gemfile b/Gemfile index 7615be0..d5fa313 100644 --- a/Gemfile +++ b/Gemfile @@ -1,11 +1,17 @@ +# frozen_string_literal: true + source 'https://rubygems.org' gemspec gem 'coveralls', require: false gem 'faraday-retry', require: false +gem 'rubocop', require: false +gem 'rubocop-performance', require: false +gem 'rubocop-rake', require: false +gem 'rubocop-rspec', require: false group :test do - gem 'webmock' gem 'sinatra' + gem 'webmock' end diff --git a/Rakefile b/Rakefile index a9d354e..f39ffa6 100644 --- a/Rakefile +++ b/Rakefile @@ -1 +1,3 @@ +# frozen_string_literal: true + task default: [:spec] diff --git a/airborne.gemspec b/airborne.gemspec index b29355b..ebc7c3e 100644 --- a/airborne.gemspec +++ b/airborne.gemspec @@ -1,21 +1,26 @@ +# frozen_string_literal: true + require 'date' -Gem::Specification.new do |s| +Gem::Specification.new do |s| # rubocop:disable Gemspec/RequireMFA s.name = 'airborne' s.version = '0.3.7' - s.date = Date.today.to_s s.summary = 'RSpec driven API testing framework' s.authors = ['Alex Friedman', 'Seth Pollack'] s.email = ['a.friedman07@gmail.com', 'seth@sethpollack.net'] s.require_paths = ['lib'] s.files = `git ls-files`.split("\n") - s.license = 'MIT' - s.add_runtime_dependency 'rspec', '~> 3.8' - s.add_runtime_dependency 'rest-client', '< 3.0', '>= 2.0.2' - s.add_runtime_dependency 'rack-test', '< 2.0', '>= 1.1.0' - s.add_runtime_dependency 'rack' + s.license = 'MIT' + + s.required_ruby_version = ['>= 2.7', '< 4'] + s.add_runtime_dependency 'activesupport' - s.add_development_dependency 'webmock', '~> 3' - s.add_development_dependency 'rake', '~> 12' + s.add_runtime_dependency 'rack' + s.add_runtime_dependency 'rack-test', '< 2.0', '>= 1.1.0' + s.add_runtime_dependency 'rest-client', '< 3.0', '>= 2.0.2' + s.add_runtime_dependency 'rspec', '~> 3.8' + s.add_development_dependency 'github_changelog_generator', '~> 1.14' + s.add_development_dependency 'rake', '~> 12' + s.add_development_dependency 'webmock', '~> 3' end diff --git a/lib/airborne.rb b/lib/airborne.rb index f605c3a..206ec0d 100644 --- a/lib/airborne.rb +++ b/lib/airborne.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'airborne/optional_hash_type_expectations' require 'airborne/path_matcher' require 'airborne/request_expectations' @@ -5,7 +7,7 @@ require 'airborne/rack_test_requester' require 'airborne/base' -RSpec.configure do |config| +RSpec.configure do |config| # rubocop:disable Metrics/BlockLength config.add_setting :base_url config.add_setting :match_expected config.add_setting :match_actual @@ -17,12 +19,21 @@ config.add_setting :requester_module config.add_setting :verify_ssl, default: true config.before do |example| - config.match_expected = example.metadata[:match_expected].nil? ? - Airborne.configuration.match_expected_default? : example.metadata[:match_expected] - config.match_actual = example.metadata[:match_actual].nil? ? - Airborne.configuration.match_actual_default? : example.metadata[:match_actual] - config.verify_ssl = example.metadata[:verify_ssl].nil? ? - Airborne.configuration.verify_ssl? : example.metadata[:verify_ssl] + config.match_expected = if example.metadata[:match_expected].nil? + Airborne.configuration.match_expected_default? + else + example.metadata[:match_expected] + end + config.match_actual = if example.metadata[:match_actual].nil? + Airborne.configuration.match_actual_default? + else + example.metadata[:match_actual] + end + config.verify_ssl = if example.metadata[:verify_ssl].nil? + Airborne.configuration.verify_ssl? + else + example.metadata[:verify_ssl] + end end # Include last since it depends on the configuration already being added diff --git a/lib/airborne/base.rb b/lib/airborne/base.rb index df44138..5c7afbf 100644 --- a/lib/airborne/base.rb +++ b/lib/airborne/base.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'json' require 'active_support' require 'active_support/core_ext/hash/indifferent_access' @@ -7,12 +9,10 @@ class InvalidJsonError < StandardError; end include RequestExpectations - attr_reader :response, :headers, :body + attr_reader :response - def self.configure - RSpec.configure do |config| - yield config - end + def self.configure(&block) + RSpec.configure(&block) end def self.included(base) @@ -57,10 +57,6 @@ def options(url, headers = nil) @response = make_request(:options, url, headers: headers) end - def response - @response - end - def headers HashWithIndifferentAccess.new(response.headers) end @@ -70,7 +66,9 @@ def body end def json_body - JSON.parse(response.body, symbolize_names: true) rescue fail InvalidJsonError, 'Api request returned invalid json' + JSON.parse(response.body, symbolize_names: true) + rescue StandardError + raise InvalidJsonError, 'Api request returned invalid json' end private diff --git a/lib/airborne/optional_hash_type_expectations.rb b/lib/airborne/optional_hash_type_expectations.rb index e28badc..9a562a9 100644 --- a/lib/airborne/optional_hash_type_expectations.rb +++ b/lib/airborne/optional_hash_type_expectations.rb @@ -1,15 +1,16 @@ +# frozen_string_literal: true + module Airborne class OptionalHashTypeExpectations include Enumerable attr_accessor :hash + def initialize(hash) @hash = hash end - def each - @hash.each do|k, v| - yield(k, v) - end + def each(&block) + @hash.each(&block) end def [](val) diff --git a/lib/airborne/path_matcher.rb b/lib/airborne/path_matcher.rb index 768a0f6..60b4a1c 100644 --- a/lib/airborne/path_matcher.rb +++ b/lib/airborne/path_matcher.rb @@ -1,29 +1,44 @@ +# frozen_string_literal: true + module Airborne class PathError < StandardError; end module PathMatcher - def get_by_path(path, json, &block) - fail PathError, "Invalid Path, contains '..'" if /\.\./ =~ path + WILDCARDS = ['*', '?'].freeze + + def get_by_path(path, json, &block) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength + raise PathError, "Invalid Path, contains '..'" if /\.\./.match?(path) + type = false parts = path.split('.') + exit_now = false parts.each_with_index do |part, index| - if part == '*' || part == '?' + if WILDCARDS.include?(part) ensure_array(path, json) type = part + if index < parts.length.pred - walk_with_path(type, index, path, parts, json, &block) && return + walk_with_path(type, index, path, parts, json, &block) + exit_now = true + break end + next end + begin json = process_json(part, json) - rescue + rescue StandardError raise PathError, "Expected #{json.class}\nto be an object with property #{part}" end end - if type == '*' + + return if exit_now + + case type + when '*' expect_all(json, &block) - elsif type == '?' + when '?' expect_one(path, json, &block) else yield json @@ -32,15 +47,15 @@ def get_by_path(path, json, &block) private - def walk_with_path(type, index, path, parts, json, &block) - last_error = nil + def walk_with_path(type, index, path, parts, json, &block) # rubocop:disable Metrics/MethodLength + last_error = nil item_count = json.length error_count = 0 json.each do |element| begin sub_path = parts[(index.next)...(parts.length)].join('.') get_by_path(sub_path, element, &block) - rescue Exception => e + rescue Exception => e # rubocop:disable Lint/RescueException last_error = e error_count += 1 end @@ -63,39 +78,43 @@ def index?(part) part =~ /^\d+$/ end - def expect_one(path, json, &block) + def expect_one(path, json) item_count = json.length error_count = 0 json.each do |part| - begin - yield part - rescue Exception - error_count += 1 - ensure_match_one(path, item_count, error_count) - end + yield part + rescue Exception # rubocop:disable Lint/RescueException + error_count += 1 + ensure_match_one(path, item_count, error_count) end end def expect_all(json, &block) last_error = nil begin - json.each { |part| yield part } - rescue Exception => e + json.each(&block) + rescue Exception => e # rubocop:disable Lint/RescueException last_error = e end ensure_match_all(last_error) end def ensure_match_one(path, item_count, error_count) - fail RSpec::Expectations::ExpectationNotMetError, "Expected one object in path #{path} to match provided JSON values" if item_count == error_count + return unless item_count == error_count + + raise RSpec::Expectations::ExpectationNotMetError, + "Expected one object in path #{path} to match provided JSON values" end def ensure_match_all(error) - fail error unless error.nil? + raise error unless error.nil? end def ensure_array(path, json) - fail RSpec::Expectations::ExpectationNotMetError, "Expected #{path} to be array got #{json.class} from JSON response" unless json.class == Array + return if json.is_a?(Array) + + raise RSpec::Expectations::ExpectationNotMetError, + "Expected #{path} to be array got #{json.class} from JSON response" end end end diff --git a/lib/airborne/rack_test_requester.rb b/lib/airborne/rack_test_requester.rb index abcb652..a291cfd 100644 --- a/lib/airborne/rack_test_requester.rb +++ b/lib/airborne/rack_test_requester.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rack/test' module Airborne diff --git a/lib/airborne/request_expectations.rb b/lib/airborne/request_expectations.rb index cde9afb..1439b20 100644 --- a/lib/airborne/request_expectations.rb +++ b/lib/airborne/request_expectations.rb @@ -1,9 +1,12 @@ +# frozen_string_literal: true + require 'rspec' require 'date' require 'rack/utils' module Airborne class ExpectationError < StandardError; end + module RequestExpectations include RSpec include PathMatcher @@ -60,14 +63,12 @@ def date def expect_header_impl(key, content, contains = nil) header = headers[key] - if header - if contains - expect(header.downcase).to include(content.downcase) - else - expect(header.downcase).to eq(content.downcase) - end + raise RSpec::Expectations::ExpectationNotMetError, "Header #{key} not present in the HTTP response" unless header + + if contains + expect(header.downcase).to include(content.downcase) else - fail RSpec::Expectations::ExpectationNotMetError, "Header #{key} not present in the HTTP response" + expect(header.downcase).to eq(content.downcase) end end @@ -85,8 +86,8 @@ def expect_json_impl(expected, actual) keys = expected.keys & actual.keys if match_none? keys.flatten.uniq.each do |prop| - expected_value = extract_expected_value(expected, prop) - actual_value = extract_actual(actual, prop) + expected_value = extract_expected_value(expected, prop) + actual_value = extract_actual(actual, prop) next expect_json_impl(expected_value, actual_value) if hash?(expected_value) && hash?(actual_value) next expected_value.call(actual_value) if expected_value.is_a?(Proc) @@ -99,9 +100,9 @@ def expect_json_impl(expected, actual) def expect_json_types_impl(expected, actual) return if nil_optional_hash?(expected, actual) - @mapper ||= get_mapper + @mapper ||= mapper - actual = convert_to_date(actual) if ((expected == :date) || (expected == :date_or_null)) + actual = convert_to_date(actual) if (expected == :date) || (expected == :date_or_null) return expect_type(expected, actual) if expected.is_a?(Symbol) return expected.call(actual) if expected.is_a?(Proc) @@ -113,9 +114,9 @@ def expect_json_types_impl(expected, actual) keys = expected.keys & actual.keys if match_none? keys.flatten.uniq.each do |prop| - type = extract_expected_type(expected, prop) + type = extract_expected_type(expected, prop) value = extract_actual(actual, prop) - value = convert_to_date(value) if ((type == :date) || (type == :date_or_null)) + value = convert_to_date(value) if (type == :date) || (type == :date_or_null) next expect_json_types_impl(type, value) if hash?(type) next type.call(value) if type.is_a?(Proc) @@ -141,45 +142,39 @@ def call_with_path(args) end def extract_expected_value(expected, prop) - begin - raise unless expected.keys.include?(prop) - expected[prop] - rescue - raise ExpectationError, "Expectation is expected to contain property: #{prop}" - end + raise unless expected.key?(prop) + + expected[prop] + rescue StandardError + raise ExpectationError, "Expectation is expected to contain property: #{prop}" end def extract_expected_type(expected, prop) - begin - type = expected[prop] - type.nil? ? raise : type - rescue - raise ExpectationError, "Expectation is expected to contain property: #{prop}" - end + type = expected[prop] + type.nil? ? raise : type + rescue StandardError + raise ExpectationError, "Expectation is expected to contain property: #{prop}" end def extract_actual(actual, prop) - begin - value = actual[prop] - rescue - raise ExpectationError, "Expected #{actual.class} #{actual}\nto be an object with property #{prop}" - end + actual[prop] + rescue StandardError + raise ExpectationError, "Expected #{actual.class} #{actual}\nto be an object with property #{prop}" end def expect_type(expected_type, value, prop_name = nil) - fail ExpectationError, "Expected type #{expected_type}\nis an invalid type" if @mapper[expected_type].nil? + raise ExpectationError, "Expected type #{expected_type}\nis an invalid type" if @mapper[expected_type].nil? insert = prop_name.nil? ? '' : "#{prop_name} to be of type" message = "Expected #{insert} #{expected_type}\n got #{value.class} instead" - expect(@mapper[expected_type].any?{|type| value.is_a?(type)}).to eq(true), message + expect(@mapper[expected_type].any? { |type| value.is_a?(type) }).to eq(true), message end def convert_to_date(value) - begin - DateTime.parse(value) - rescue - end + DateTime.parse(value) + rescue StandardError + nil end def check_array_types(value, prop_name, expected_type) @@ -198,13 +193,12 @@ def hash?(hash) end def expect_array(value, prop_name, expected_type) - expect(value.class).to eq(Array), "Expected #{prop_name}\n to be of type #{expected_type}\n got #{value.class} instead" + expect(value.class).to eq(Array), + "Expected #{prop_name}\n to be of type #{expected_type}\n got #{value.class} instead" end def convert_expectations_for_json_sizes(old_expectations) - unless old_expectations.is_a?(Hash) - return convert_expectation_for_json_sizes(old_expectations) - end + return convert_expectation_for_json_sizes(old_expectations) unless old_expectations.is_a?(Hash) old_expectations.each_with_object({}) do |(prop_name, expected_size), memo| new_value = if expected_size.is_a?(Hash) @@ -221,18 +215,16 @@ def convert_expectation_for_json_sizes(expected_size) end def ensure_hash_contains_prop(prop_name, hash) - begin - yield - rescue - raise ExpectationError, "Expected #{hash.class} #{hash}\nto be an object with property #{prop_name}" - end + yield + rescue StandardError + raise ExpectationError, "Expected #{hash.class} #{hash}\nto be an object with property #{prop_name}" end def property?(expectation) - [String, Regexp, Float, Integer, TrueClass, FalseClass, NilClass, Array].any?{|type| expectation.is_a?(type)} + [String, Regexp, Float, Integer, TrueClass, FalseClass, NilClass, Array].any? { |type| expectation.is_a?(type) } end - def get_mapper + def mapper base_mapper = { integer: [Integer], array_of_integers: [Integer], @@ -256,7 +248,7 @@ def get_mapper mapper = base_mapper.clone base_mapper.each do |key, value| - mapper[(key.to_s + '_or_null').to_sym] = value + [NilClass] + mapper["#{key}_or_null".to_sym] = value + [NilClass] end mapper end diff --git a/lib/airborne/rest_client_requester.rb b/lib/airborne/rest_client_requester.rb index d820a09..aa7f11c 100644 --- a/lib/airborne/rest_client_requester.rb +++ b/lib/airborne/rest_client_requester.rb @@ -1,47 +1,39 @@ +# frozen_string_literal: true + require 'rest_client' module Airborne module RestClientRequester def make_request(method, url, options = {}) headers = base_headers.merge(options[:headers] || {}) - verify_ssl = options.fetch(:verify_ssl, true) - res = if method == :post || method == :patch || method == :put || method == :delete - begin - request_body = options[:body].nil? || is_empty(options[:body]) ? '' : options[:body] - request_body = request_body.to_json if is_json_request(headers) && !is_empty(request_body) - RestClient::Request.execute( - method: method, - url: get_url(url), - payload: request_body, - headers: headers, - verify_ssl: verify_ssl - ) { |response, request, result| response } - rescue RestClient::Exception => e - e.response ? e.response : e.original_exception - end - else - begin - RestClient::Request.execute( - method: method, - url: get_url(url), - headers: headers, - verify_ssl: verify_ssl - ) { |response, request, result| response } - rescue RestClient::Exception => e - e.response ? e.response : e.original_exception - end - end - res + + RestClient::Request.execute( + method: method, + url: get_url(url), + payload: request_body(method, headers, options), + headers: headers, + verify_ssl: options.fetch(:verify_ssl, true) + ) { |response, _request, _result| response } + rescue RestClient::Exception => e + e.response || e.original_exception end private - def is_json_request(headers) + def request_body(method, headers, options) + return unless [:post, :patch, :put, :delete].include?(method) + + request_body = options[:body].nil? || empty?(options[:body]) ? '' : options[:body] + request_body = request_body.to_json if json_request?(headers) && !empty?(request_body) + request_body + end + + def json_request?(headers) header = headers.fetch(:content_type) - header == :json || /application\/([a-zA-Z0-9\.\_\-]*\+?)json/ =~ header + header == :json || %r{application/([a-zA-Z0-9._-]*\+?)json} =~ header end - def is_empty(body) + def empty?(body) return body.empty? if body.respond_to?(:empty?) false diff --git a/rakelib/changelog.rake b/rakelib/changelog.rake index d968040..cc74fe0 100644 --- a/rakelib/changelog.rake +++ b/rakelib/changelog.rake @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'github_changelog_generator/task' if ENV['CHANGELOG_GITHUB_TOKEN'].nil? @@ -11,4 +13,3 @@ GitHubChangelogGenerator::RakeTask.new :changelog do |config| # change this to your github username if you plan to submit a PR with a new CHANGELOG.md config.user = 'brooklynDev' end - diff --git a/rakelib/rspec.rake b/rakelib/rspec.rake index 3c52bf9..0e5b843 100644 --- a/rakelib/rspec.rake +++ b/rakelib/rspec.rake @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rspec/core/rake_task' RSpec::Core::RakeTask.new diff --git a/rakelib/rubocop.rake b/rakelib/rubocop.rake new file mode 100644 index 0000000..0656466 --- /dev/null +++ b/rakelib/rubocop.rake @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'rubocop/rake_task' + +RuboCop::RakeTask.new + +task default: [:rubocop] diff --git a/spec/airborne/base_spec.rb b/spec/airborne/base_spec.rb index 63d1da2..97302ea 100644 --- a/spec/airborne/base_spec.rb +++ b/spec/airborne/base_spec.rb @@ -1,19 +1,21 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'base spec' do it 'when request is made response should be set' do mock_get('simple_get') get '/simple_get' - expect(response).to_not be(nil) + expect(response).to_not be_nil end it 'when request is made headers should be set' do mock_get('simple_get') get '/simple_get' - expect(headers).to_not be(nil) + expect(headers).to_not be_nil end - it 'should throw an InvalidJsonError when accessing json_body on invalid json' do + it 'throws an InvalidJsonError when accessing json_body on invalid json' do mock_get('invalid_json') get '/invalid_json' expect(body).to eq('invalid1234') @@ -23,7 +25,7 @@ it 'when request is made headers should be hash with indifferent access' do mock_get('simple_get', 'Content-Type' => 'application/json') get '/simple_get' - expect(headers).to be_kind_of(Hash) + expect(headers).to be_a(Hash) expect(headers[:content_type]).to eq('application/json') expect(headers['content_type']).to eq('application/json') end @@ -31,25 +33,25 @@ it 'when request is made body should be set' do mock_get('simple_get') get '/simple_get' - expect(body).to_not be(nil) + expect(body).to_not be_nil end it 'when request is made json body should be symbolized hash' do mock_get('simple_get') get '/simple_get' - expect(json_body).to be_kind_of(Hash) - expect(json_body.first[0]).to be_kind_of(Symbol) + expect(json_body).to be_a(Hash) + expect(json_body.first[0]).to be_a(Symbol) end - it 'should handle a 500 error on get' do + it 'handles a 500 error on get' do mock_get('simple_get', {}, [500, 'Internal Server Error']) get '/simple_get' - expect(json_body).to_not be(nil) + expect(json_body).to_not be_nil end - it 'should handle a 500 error on post' do + it 'handles a 500 error on post' do mock_post('simple_post', {}, [500, 'Internal Server Error']) post '/simple_post', {} - expect(json_body).to_not be(nil) + expect(json_body).to_not be_nil end end diff --git a/spec/airborne/client_requester_spec.rb b/spec/airborne/client_requester_spec.rb index e54e10d..9f50c08 100644 --- a/spec/airborne/client_requester_spec.rb +++ b/spec/airborne/client_requester_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'client requester' do @@ -12,42 +14,45 @@ Airborne.configure { |config| config.verify_ssl = true } end - it 'should set :content_type to :json by default' do + it 'sets :content_type to :json by default' do get '/foo' expect(RestClient::Request).to have_received(:execute).with( method: :get, + payload: nil, url: 'http://www.example.com/foo', headers: { content_type: :json }, verify_ssl: true ) end - it 'should override headers with option[:headers]' do + it 'overrides headers with option[:headers]' do get '/foo', { content_type: 'application/x-www-form-urlencoded' } expect(RestClient::Request).to have_received(:execute).with( method: :get, + payload: nil, url: 'http://www.example.com/foo', headers: { content_type: 'application/x-www-form-urlencoded' }, verify_ssl: true ) end - it 'should override headers with airborne config headers' do + it 'overrides headers with airborne config headers' do Airborne.configure { |config| config.headers = { content_type: 'text/plain' } } get '/foo' expect(RestClient::Request).to have_received(:execute).with( method: :get, + payload: nil, url: 'http://www.example.com/foo', headers: { content_type: 'text/plain' }, verify_ssl: true ) end - it 'should serialize body to json when :content_type is (default) :json' do + it 'serializes body to json when :content_type is (default) :json' do post '/foo', { test: 'serialized' } expect(RestClient::Request).to have_received(:execute).with( @@ -59,7 +64,7 @@ ) end - it 'should serialize body to json when :content_type is any enhanced JSON content type' do + it 'serializes body to json when :content_type is any enhanced JSON content type' do post '/foo', { test: 'serialized' }, { content_type: 'application/vnd.airborne.2+json' } expect(RestClient::Request).to have_received(:execute).with( @@ -71,7 +76,7 @@ ) end - it 'should not serialize body to json when :content_type does not match JSON' do + it 'does not serialize body to json when :content_type does not match JSON' do post '/foo', { test: 'not serialized' }, { content_type: 'text/plain' } expect(RestClient::Request).to have_received(:execute).with( @@ -83,7 +88,7 @@ ) end - it 'should send payload with delete request' do + it 'sends payload with delete request' do payload = { example: 'this is the payload' } delete '/foo', payload @@ -97,61 +102,66 @@ end context 'verify_ssl' do - it 'should be true by default' do + it 'is true by default' do get '/foo' expect(RestClient::Request).to have_received(:execute).with( method: :get, + payload: nil, url: 'http://www.example.com/foo', headers: { content_type: :json }, verify_ssl: true ) end - it 'should be set by airborne config' do + it 'is set by airborne config' do Airborne.configure { |config| config.verify_ssl = false } get '/foo' expect(RestClient::Request).to have_received(:execute).with( method: :get, + payload: nil, url: 'http://www.example.com/foo', headers: { content_type: :json }, verify_ssl: false ) end - it 'should be overriden with options[:verify_ssl]' do + it 'is overridden with options[:verify_ssl]' do get '/foo', nil, false expect(RestClient::Request).to have_received(:execute).with( method: :get, + payload: nil, url: 'http://www.example.com/foo', headers: { content_type: :json }, verify_ssl: false ) end - it 'should override airborne config with options[:verify_ssl]' do + it 'overrides airborne config with options[:verify_ssl]' do Airborne.configure { |config| config.verify_ssl = false } get '/foo', nil, true expect(RestClient::Request).to have_received(:execute).with( method: :get, + payload: nil, url: 'http://www.example.com/foo', headers: { content_type: :json }, verify_ssl: true ) end - it 'should interpret airborne "config.verify_ssl = nil" as false' do + it 'interprets airborne "config.verify_ssl = nil" as false' do Airborne.configure { |config| config.verify_ssl = nil } get '/foo' expect(RestClient::Request).to have_received(:execute).with( method: :get, + payload: nil, url: 'http://www.example.com/foo', headers: { content_type: :json }, verify_ssl: false @@ -159,35 +169,38 @@ end context 'rspec metadata', verify_ssl: false do - it 'should override the base airborne config with the rspec metadata' do + it 'overrides the base airborne config with the rspec metadata' do get '/foo' expect(RestClient::Request).to have_received(:execute).with( method: :get, + payload: nil, url: 'http://www.example.com/foo', headers: { content_type: :json }, verify_ssl: false ) end - it 'should be overriden with options[:verify_ssl]' do + it 'is overridden with options[:verify_ssl]' do get '/foo', nil, true expect(RestClient::Request).to have_received(:execute).with( method: :get, + payload: nil, url: 'http://www.example.com/foo', headers: { content_type: :json }, verify_ssl: true ) end - it 'should be overriden by supplied airborne config' do + it 'is overridden by supplied airborne config' do Airborne.configure { |config| config.verify_ssl = true } get '/foo' expect(RestClient::Request).to have_received(:execute).with( method: :get, + payload: nil, url: 'http://www.example.com/foo', headers: { content_type: :json }, verify_ssl: true diff --git a/spec/airborne/delete_spec.rb b/spec/airborne/delete_spec.rb index b892efa..80f1c8d 100644 --- a/spec/airborne/delete_spec.rb +++ b/spec/airborne/delete_spec.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'delete' do - it 'should allow testing on delete requests' do + it 'allows testing on delete requests' do mock_delete 'simple_delete' delete '/simple_delete', {} expect_status 200 diff --git a/spec/airborne/expectations/expect_header_contains_spec.rb b/spec/airborne/expectations/expect_header_contains_spec.rb index 32d61aa..b5845bd 100644 --- a/spec/airborne/expectations/expect_header_contains_spec.rb +++ b/spec/airborne/expectations/expect_header_contains_spec.rb @@ -1,19 +1,21 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect header contains' do - it 'should ensure partial header match exists' do + it 'ensures partial header match exists' do mock_get('simple_get', 'Content-Type' => 'application/json') get '/simple_get' expect_header_contains(:content_type, 'json') end - it 'should ensure header is present' do + it 'ensures header is present' do mock_get('simple_get', 'Content-Type' => 'application/json') get '/simple_get' expect { expect_header_contains(:foo, 'bar') }.to raise_error(ExpectationNotMetError) end - it 'should ensure partial header is present' do + it 'ensures partial header is present' do mock_get('simple_get', 'Content-Type' => 'application/json') get '/simple_get' expect { expect_header_contains(:content_type, 'bar') }.to raise_error(ExpectationNotMetError) diff --git a/spec/airborne/expectations/expect_header_spec.rb b/spec/airborne/expectations/expect_header_spec.rb index 8689ac0..b6f2ba6 100644 --- a/spec/airborne/expectations/expect_header_spec.rb +++ b/spec/airborne/expectations/expect_header_spec.rb @@ -1,19 +1,21 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect header' do - it 'should find exact match for header content' do + it 'finds exact match for header content' do mock_get('simple_get', 'Content-Type' => 'application/json') get '/simple_get' expect_header(:content_type, 'application/json') end - it 'should find exact match for header content' do + it 'finds exact match for header content' do mock_get('simple_get', 'Content-Type' => 'json') get '/simple_get' expect { expect_header(:content_type, 'application/json') }.to raise_error(ExpectationNotMetError) end - it 'should ensure correct headers are present' do + it 'ensures correct headers are present' do mock_get('simple_get', 'Content-Type' => 'application/json') get '/simple_get' expect { expect_header(:foo, 'bar') }.to raise_error(ExpectationNotMetError) diff --git a/spec/airborne/expectations/expect_json_keys_path_spec.rb b/spec/airborne/expectations/expect_json_keys_path_spec.rb index 2d26efc..ebef68d 100644 --- a/spec/airborne/expectations/expect_json_keys_path_spec.rb +++ b/spec/airborne/expectations/expect_json_keys_path_spec.rb @@ -1,13 +1,15 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect_json_keys with path' do - it 'should ensure json keys with path' do + it 'ensures json keys with path' do mock_get('simple_nested_path') get '/simple_nested_path', {} expect_json_keys('address', [:street, :city]) end - it 'should fail when keys are missing with path' do + it 'fails when keys are missing with path' do mock_get('simple_nested_path') get '/simple_nested_path', {} expect { expect_json_keys('address', [:bad]) }.to raise_error(ExpectationNotMetError) diff --git a/spec/airborne/expectations/expect_json_keys_spec.rb b/spec/airborne/expectations/expect_json_keys_spec.rb index 2d51e51..c51bf7f 100644 --- a/spec/airborne/expectations/expect_json_keys_spec.rb +++ b/spec/airborne/expectations/expect_json_keys_spec.rb @@ -1,19 +1,21 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect_json_keys' do - it 'should fail when json keys are missing' do + it 'fails when json keys are missing' do mock_get('simple_json') get '/simple_json', {} expect { expect_json_keys([:foo, :bar, :baz, :bax]) }.to raise_error(ExpectationNotMetError) end - it 'should ensure correct json keys' do + it 'ensures correct json keys' do mock_get('simple_json') get '/simple_json', {} expect_json_keys([:foo, :bar, :baz]) end - it 'should ensure correct partial json keys' do + it 'ensures correct partial json keys' do mock_get('simple_json') get '/simple_json', {} expect_json_keys([:foo, :bar]) diff --git a/spec/airborne/expectations/expect_json_lambda_spec.rb b/spec/airborne/expectations/expect_json_lambda_spec.rb index 9bf444f..35f2b5d 100644 --- a/spec/airborne/expectations/expect_json_lambda_spec.rb +++ b/spec/airborne/expectations/expect_json_lambda_spec.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect_json lambda' do - it 'should invoke proc passed in' do + it 'invokes proc passed in' do mock_get('simple_get') get '/simple_get' expect_json(name: ->(name) { expect(name.length).to eq(4) }) diff --git a/spec/airborne/expectations/expect_json_options_spec.rb b/spec/airborne/expectations/expect_json_options_spec.rb index fcbb5d3..5e9a6d3 100644 --- a/spec/airborne/expectations/expect_json_options_spec.rb +++ b/spec/airborne/expectations/expect_json_options_spec.rb @@ -1,62 +1,64 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect_json options' do - describe 'match_expected', match_expected: true, match_actual: false do - it 'should require all expected properties' do + describe 'match_expected', match_actual: false, match_expected: true do + it 'requires all expected properties' do mock_get 'simple_get' get '/simple_get' - expect{ expect_json(name: 'Alex', other: 'other') }.to raise_error(ExpectationNotMetError) + expect { expect_json(name: 'Alex', other: 'other') }.to raise_error(ExpectationNotMetError) end - it 'should not require the actual properties' do + it 'does not require the actual properties' do mock_get 'simple_get' get '/simple_get' expect_json(name: 'Alex') end end - describe 'match_actual', match_expected: false, match_actual: true do - it 'should require all actual properties' do + describe 'match_actual', match_actual: true, match_expected: false do + it 'requires all actual properties' do mock_get 'simple_get' get '/simple_get' - expect{ expect_json(name: 'Alex') }.to raise_error(ExpectationError) + expect { expect_json(name: 'Alex') }.to raise_error(ExpectationError) end - it 'should not require the expected properties' do + it 'does not require the expected properties' do mock_get 'simple_get' get '/simple_get' expect_json(name: 'Alex', age: 32, address: nil, other: 'other') end end - describe 'match_both', match_expected: true, match_actual: true do - it 'should require all actual properties' do + describe 'match_both', match_actual: true, match_expected: true do + it 'requires all actual properties' do mock_get 'simple_get' get '/simple_get' - expect{ expect_json(name: 'Alex') }.to raise_error(ExpectationError) + expect { expect_json(name: 'Alex') }.to raise_error(ExpectationError) end - it 'should require all expected properties' do + it 'requires all expected properties' do mock_get 'simple_get' get '/simple_get' - expect{ expect_json(name: 'Alex', other: 'other') }.to raise_error(ExpectationNotMetError) + expect { expect_json(name: 'Alex', other: 'other') }.to raise_error(ExpectationNotMetError) end - it 'should require all expected properties' do + it 'requires all expected properties' do mock_get 'simple_get' get '/simple_get' - expect{ expect_json(name: 'Alex', nested: {}) }.to raise_error(ExpectationNotMetError) + expect { expect_json(name: 'Alex', nested: {}) }.to raise_error(ExpectationNotMetError) end end - describe 'match_none', match_expected: false, match_actual: false do - it 'should not require the actual properties' do + describe 'match_none', match_actual: false, match_expected: false do + it 'does not require the actual properties' do mock_get 'simple_get' get '/simple_get' expect_json(name: 'Alex') end - it 'should not require the expected properties' do + it 'does not require the expected properties' do mock_get 'simple_get' get '/simple_get' expect_json(name: 'Alex', age: 32, address: nil, other: 'other', nested: {}) diff --git a/spec/airborne/expectations/expect_json_path_spec.rb b/spec/airborne/expectations/expect_json_path_spec.rb index 3c1fe31..2df5c9c 100644 --- a/spec/airborne/expectations/expect_json_path_spec.rb +++ b/spec/airborne/expectations/expect_json_path_spec.rb @@ -1,104 +1,108 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect_json with path' do - it 'should allow simple path and verify only that path' do + it 'allows simple path and verify only that path' do mock_get('simple_path_get') get '/simple_path_get' expect_json('address', street: 'Area 51', city: 'Roswell', state: 'NM') end - it 'should allow nested paths' do + it 'allows nested paths' do mock_get('simple_nested_path') get '/simple_nested_path' - expect_json('address.coordinates', lattitude: 33.3872, longitutde: 104.5281) + expect_json('address.coordinates', latitude: 33.3872, longitude: 104.5281) end - it 'should index into array and test against specific element' do + it 'indexes into array and test against specific element' do mock_get('array_with_index') get '/array_with_index' expect_json('cars.0', make: 'Tesla', model: 'Model S') end - it 'should test against all elements in the array' do + it 'tests against all elements in the array' do mock_get('array_with_index') get '/array_with_index' expect_json('cars.?', make: 'Tesla', model: 'Model S') expect_json('cars.?', make: 'Lamborghini', model: 'Aventador') end - it 'should test against properties in the array' do + it 'tests against properties in the array' do mock_get('array_with_index') get '/array_with_index' expect_json('cars.?.make', 'Tesla') end - it 'should ensure at least one match' do + it 'ensures at least one match' do mock_get('array_with_index') get '/array_with_index' expect { expect_json('cars.?.make', 'Teslas') }.to raise_error(ExpectationNotMetError) end - it 'should check for at least one match' do + it 'checks for at least one match' do mock_get('array_with_nested') get '/array_with_nested' expect_json('cars.?.owners.?', name: 'Bart Simpson') end - it 'should ensure at least one match' do + it 'ensures at least one match' do mock_get('array_with_nested') get '/array_with_nested' expect { expect_json('cars.?.owners.?', name: 'Bart Simpsons') }.to raise_error(ExpectationNotMetError) end - it 'should check for one match that matches all ' do + it 'checks for one match that matches all' do mock_get('array_with_nested') get '/array_with_nested' expect_json('cars.?.owners.*', name: 'Bart Simpson') end - it 'should check for one match that matches all with lambda' do + it 'checks for one match that matches all with lambda' do mock_get('array_with_nested') get '/array_with_nested' expect_json('cars.?.owners.*', name: ->(name) { expect(name).to eq('Bart Simpson') }) end - it 'should ensure one match that matches all with lambda' do + it 'ensures one match that matches all with lambda' do mock_get('array_with_nested') get '/array_with_nested' - expect { expect_json('cars.?.owners.*', name: ->(name) { expect(name).to eq('Bart Simpsons') }) }.to raise_error(ExpectationNotMetError) + expect do + expect_json('cars.?.owners.*', name: ->(name) { expect(name).to eq('Bart Simpsons') }) + end.to raise_error(ExpectationNotMetError) end - it 'should ensure one match that matches all' do + it 'ensures one match that matches all' do mock_get('array_with_nested') get '/array_with_nested' expect { expect_json('cars.?.owners.*', name: 'Bart Simpsons') }.to raise_error(ExpectationNotMetError) end - it 'should allow indexing' do + it 'allows indexing' do mock_get('array_with_nested') get '/array_with_nested' expect_json('cars.0.owners.0', name: 'Bart Simpson') end - it 'should allow strings (String) to be tested against a path' do + it 'allows strings (String) to be tested against a path' do mock_get('simple_nested_path') get '/simple_nested_path' expect_json('address.city', 'Roswell') end - it 'should allow floats (Float) to be tested against a path' do + it 'allows floats (Float) to be tested against a path' do mock_get('simple_nested_path') get '/simple_nested_path' - expect_json('address.coordinates.lattitude', 33.3872) + expect_json('address.coordinates.latitude', 33.3872) end - it 'should allow integers (Fixnum, Bignum) to be tested against a path' do + it 'allows integers (Fixnum, Bignum) to be tested against a path' do mock_get('simple_get') get '/simple_get' expect_json('age', 32) end - it 'should raise ExpectationError when expectation expects an object instead of value' do + it 'raises ExpectationError when expectation expects an object instead of value' do mock_get('array_with_index') get '/array_with_index' expect do diff --git a/spec/airborne/expectations/expect_json_regex_spec.rb b/spec/airborne/expectations/expect_json_regex_spec.rb index e66905b..9c1de9b 100644 --- a/spec/airborne/expectations/expect_json_regex_spec.rb +++ b/spec/airborne/expectations/expect_json_regex_spec.rb @@ -1,33 +1,35 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect_json regex' do - it 'should test against regex' do + it 'tests against regex' do mock_get('simple_get') get '/simple_get' expect_json(name: regex('^A')) end - it 'should raise an error if regex does not match' do + it 'raises an error if regex does not match' do mock_get('simple_get') get '/simple_get' expect { expect_json(name: regex('^B')) }.to raise_error(ExpectationNotMetError) end - it 'should allow regex(Regexp) to be tested against a path' do + it 'allows regex(Regexp) to be tested against a path' do mock_get('simple_nested_path') get '/simple_nested_path' expect_json('address.city', regex('^R')) end - it 'should allow testing regex against numbers directly' do + it 'allows testing regex against numbers directly' do mock_get('simple_nested_path') get '/simple_nested_path' - expect_json('address.coordinates.lattitude', regex('^3')) + expect_json('address.coordinates.latitude', regex('^3')) end - it 'should allow testing regex against numbers in the hash' do + it 'allows testing regex against numbers in the hash' do mock_get('simple_nested_path') get '/simple_nested_path' - expect_json('address.coordinates', lattitude: regex('^3')) + expect_json('address.coordinates', latitude: regex('^3')) end end diff --git a/spec/airborne/expectations/expect_json_sizes_spec.rb b/spec/airborne/expectations/expect_json_sizes_spec.rb index 18826cf..82caa2a 100644 --- a/spec/airborne/expectations/expect_json_sizes_spec.rb +++ b/spec/airborne/expectations/expect_json_sizes_spec.rb @@ -1,31 +1,33 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect_json_sizes' do - it 'should detect sizes' do + it 'detects sizes' do mock_get('array_of_values') get '/array_of_values' expect_json_sizes(grades: 4, bad: 3, emptyArray: 0) end - it 'should allow full object graph' do + it 'allows full object graph' do mock_get('array_with_nested') get '/array_with_nested' expect_json_sizes(cars: { 0 => { owners: 1 }, 1 => { owners: 1 } }) end - it 'should allow properties to be tested against a path' do + it 'allows properties to be tested against a path' do mock_get('array_with_nested') get '/array_with_nested' expect_json_sizes('cars.0.owners', 1) end - it 'should test against all elements in the array when path contains * AND expectation is an Integer' do + it 'tests against all elements in the array when path contains * AND expectation is an Integer' do mock_get('array_with_nested') get '/array_with_nested' expect_json_sizes('cars.*.owners', 1) end - it 'should test against all elements in the array when path contains * AND expectation is a Hash' do + it 'tests against all elements in the array when path contains * AND expectation is a Hash' do mock_get('array_with_nested') get '/array_with_nested' expect_json_sizes('cars.*', owners: 1) diff --git a/spec/airborne/expectations/expect_json_spec.rb b/spec/airborne/expectations/expect_json_spec.rb index 9abed97..c8b3b48 100644 --- a/spec/airborne/expectations/expect_json_spec.rb +++ b/spec/airborne/expectations/expect_json_spec.rb @@ -1,25 +1,27 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect_json' do - it 'should ensure correct json values' do + it 'ensures correct json values' do mock_get('simple_get') get '/simple_get' expect_json(name: 'Alex', age: 32) end - it 'should allow array response' do + it 'allows array response' do mock_get('array_response') get '/array_response' expect_json([{ name: 'Seth' }]) end - it 'should fail when incorrect json is tested' do + it 'fails when incorrect json is tested' do mock_get('simple_get') get '/simple_get' expect { expect_json(bad: 'data') }.to raise_error(ExpectationNotMetError) end - it 'should allow full object graph' do + it 'allows full object graph' do mock_get('simple_path_get') get '/simple_path_get' expect_json(name: 'Alex', address: { street: 'Area 51', city: 'Roswell', state: 'NM' }) diff --git a/spec/airborne/expectations/expect_json_types_date_spec.rb b/spec/airborne/expectations/expect_json_types_date_spec.rb index 47d091d..3d24a28 100644 --- a/spec/airborne/expectations/expect_json_types_date_spec.rb +++ b/spec/airborne/expectations/expect_json_types_date_spec.rb @@ -1,14 +1,16 @@ +# frozen_string_literal: true + require 'spec_helper' require 'date' describe 'expect_json_types with date' do - it 'should verify correct date types' do + it 'verifies correct date types' do mock_get('date_response') get '/date_response' expect_json_types(createdAt: :date) end - it 'should verify correct date types with path' do + it 'verifies correct date types with path' do mock_get('date_response') get '/date_response' expect_json_types('createdAt', :date) @@ -16,7 +18,7 @@ end describe 'expect_json with date' do - it 'should verify correct date value' do + it 'verifies correct date value' do mock_get('date_response') get '/date_response' prev_day = DateTime.new(2014, 10, 19) @@ -26,25 +28,25 @@ end describe 'expect_json_types with date_or_null' do - it 'should verify date_or_null when date is null' do + it 'verifies date_or_null when date is null' do mock_get('date_is_null_response') get '/date_is_null_response' expect_json_types(dateDeleted: :date_or_null) end - it 'should verify date_or_null when date is null with path' do + it 'verifies date_or_null when date is null with path' do mock_get('date_is_null_response') get '/date_is_null_response' expect_json_types('dateDeleted', :date_or_null) end - it 'should verify date_or_null with date' do + it 'verifies date_or_null with date' do mock_get('date_response') get '/date_response' expect_json_types(createdAt: :date_or_null) end - it 'should verify date_or_null with date with path' do + it 'verifies date_or_null with date with path' do mock_get('date_response') get '/date_response' expect_json_types('createdAt', :date_or_null) diff --git a/spec/airborne/expectations/expect_json_types_lambda_spec.rb b/spec/airborne/expectations/expect_json_types_lambda_spec.rb index 219b714..2ce6c03 100644 --- a/spec/airborne/expectations/expect_json_types_lambda_spec.rb +++ b/spec/airborne/expectations/expect_json_types_lambda_spec.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect_json_types lambda' do - it 'should invoke proc passed in' do + it 'invokes proc passed in' do mock_get('simple_get') get '/simple_get' expect_json_types(name: ->(name) { expect(name.length).to eq(4) }) diff --git a/spec/airborne/expectations/expect_json_types_optional_spec.rb b/spec/airborne/expectations/expect_json_types_optional_spec.rb index b66a135..502ae3a 100644 --- a/spec/airborne/expectations/expect_json_types_optional_spec.rb +++ b/spec/airborne/expectations/expect_json_types_optional_spec.rb @@ -1,15 +1,17 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect_json_types optional' do - it 'should test optional nested hash when exists' do + it 'tests optional nested hash when exists' do mock_get('simple_nested_path') get '/simple_nested_path' - expect_json_types('address.coordinates', optional(lattitude: :float, longitutde: :float)) + expect_json_types('address.coordinates', optional(latitude: :float, longitude: :float)) end - it 'should allow optional nested hash' do + it 'allows optional nested hash' do mock_get('simple_path_get') get '/simple_path_get' - expect_json_types('address.coordinates', optional(lattitude: :float, longitutde: :float)) + expect_json_types('address.coordinates', optional(latitude: :float, longitude: :float)) end end diff --git a/spec/airborne/expectations/expect_json_types_options_spec.rb b/spec/airborne/expectations/expect_json_types_options_spec.rb index c4eef96..e1ddb13 100644 --- a/spec/airborne/expectations/expect_json_types_options_spec.rb +++ b/spec/airborne/expectations/expect_json_types_options_spec.rb @@ -1,56 +1,58 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect_json_types options' do - describe 'match_expected', match_expected: true, match_actual: false do - it 'should require all expected properties' do + describe 'match_expected', match_actual: false, match_expected: true do + it 'requires all expected properties' do mock_get 'simple_get' get '/simple_get' - expect{ expect_json_types(name: :string, other: :string) }.to raise_error(ExpectationNotMetError) + expect { expect_json_types(name: :string, other: :string) }.to raise_error(ExpectationNotMetError) end - it 'should not require the actual properties' do + it 'does not require the actual properties' do mock_get 'simple_get' get '/simple_get' expect_json_types(name: :string) end end - describe 'match_actual', match_expected: false, match_actual: true do - it 'should require all actual properties' do + describe 'match_actual', match_actual: true, match_expected: false do + it 'requires all actual properties' do mock_get 'simple_get' get '/simple_get' - expect{ expect_json_types(name: :string) }.to raise_error(ExpectationError) + expect { expect_json_types(name: :string) }.to raise_error(ExpectationError) end - it 'should not require the expected properties' do + it 'does not require the expected properties' do mock_get 'simple_get' get '/simple_get' expect_json_types(name: :string, age: :int, address: :null, other: :string) end end - describe 'match_both', match_expected: true, match_actual: true do - it 'should require all actual properties' do + describe 'match_both', match_actual: true, match_expected: true do + it 'requires all actual properties' do mock_get 'simple_get' get '/simple_get' - expect{ expect_json_types(name: :string) }.to raise_error(ExpectationError) + expect { expect_json_types(name: :string) }.to raise_error(ExpectationError) end - it 'should require all expected properties' do + it 'requires all expected properties' do mock_get 'simple_get' get '/simple_get' - expect{ expect_json_types(name: :string, other: :string) }.to raise_error(ExpectationNotMetError) + expect { expect_json_types(name: :string, other: :string) }.to raise_error(ExpectationNotMetError) end end - describe 'match_none', match_expected: false, match_actual: false do - it 'should not require the actual properties' do + describe 'match_none', match_actual: false, match_expected: false do + it 'does not require the actual properties' do mock_get 'simple_get' get '/simple_get' expect_json_types(name: :string) end - it 'should not require the expected properties' do + it 'does not require the expected properties' do mock_get 'simple_get' get '/simple_get' expect_json_types(name: :string, age: :int, address: :null, other: :string) diff --git a/spec/airborne/expectations/expect_json_types_path_spec.rb b/spec/airborne/expectations/expect_json_types_path_spec.rb index b965a5e..4ac99f9 100644 --- a/spec/airborne/expectations/expect_json_types_path_spec.rb +++ b/spec/airborne/expectations/expect_json_types_path_spec.rb @@ -1,61 +1,63 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect_json_types wih path' do - it 'should allow simple path and verify only that path' do + it 'allows simple path and verify only that path' do mock_get('simple_path_get') get '/simple_path_get' expect_json_types('address', street: :string, city: :string, state: :string) end - it 'should allow nested paths' do + it 'allows nested paths' do mock_get('simple_nested_path') get '/simple_nested_path' - expect_json_types('address.coordinates', lattitude: :float, longitutde: :float) + expect_json_types('address.coordinates', latitude: :float, longitude: :float) end - it 'should index into array and test against specific element' do + it 'indexes into array and test against specific element' do mock_get('array_with_index') get '/array_with_index' expect_json_types('cars.0', make: :string, model: :string) end - it 'should allow properties to be tested against a path' do + it 'allows properties to be tested against a path' do mock_get('array_with_index') get '/array_with_index' expect_json_types('cars.0.make', :string) end - it 'should test against all elements in the array' do + it 'tests against all elements in the array' do mock_get('array_with_index') get '/array_with_index' expect_json_types('cars.*', make: :string, model: :string) end - it 'should ensure all elements of array are valid' do + it 'ensures all elements of array are valid' do mock_get('array_with_index') get '/array_with_index' expect { expect_json_types('cars.*', make: :string, model: :int) }.to raise_error(ExpectationNotMetError) end - it 'should deep symbolize array responses' do + it 'deeps symbolize array responses' do mock_get('array_response') get '/array_response' expect_json_types('*', name: :string) end - it 'should check all nested arrays for specified elements' do + it 'checks all nested arrays for specified elements' do mock_get('array_with_nested') get '/array_with_nested' expect_json_types('cars.*.owners.*', name: :string) end - it 'should ensure all nested arrays contain correct data' do + it 'ensures all nested arrays contain correct data' do mock_get('array_with_nested_bad_data') get '/array_with_nested_bad_data' expect { expect_json_types('cars.*.owners.*', name: :string) }.to raise_error(ExpectationNotMetError) end - it 'should raise ExpectationError when expectation expects an object instead of type' do + it 'raises ExpectationError when expectation expects an object instead of type' do mock_get('array_with_index') get '/array_with_index' expect do diff --git a/spec/airborne/expectations/expect_json_types_spec.rb b/spec/airborne/expectations/expect_json_types_spec.rb index 48a644e..4cba898 100644 --- a/spec/airborne/expectations/expect_json_types_spec.rb +++ b/spec/airborne/expectations/expect_json_types_spec.rb @@ -1,43 +1,45 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect_json_types' do - it 'should detect current type' do + it 'detects current type' do mock_get('simple_get') get '/simple_get' expect_json_types(name: :string, age: :int) end - it 'should fail when incorrect json types tested' do + it 'fails when incorrect json types tested' do mock_get('simple_get') get '/simple_get' expect { expect_json_types(bad: :bool) }.to raise_error(ExpectationNotMetError) end - it 'should not fail when optional property is not present' do + it 'does not fail when optional property is not present' do mock_get('simple_get') get '/simple_get' expect_json_types(name: :string, age: :int, optional: :bool_or_null) end - it 'should allow full object graph' do + it 'allows full object graph' do mock_get('simple_path_get') get '/simple_path_get' - expect_json_types({name: :string, address: { street: :string, city: :string, state: :string }}) + expect_json_types({ name: :string, address: { street: :string, city: :string, state: :string } }) end - it 'should check all types in a simple array' do + it 'checks all types in a simple array' do mock_get('array_of_values') get '/array_of_values' expect_json_types(grades: :array_of_ints) end - it 'should ensure all valid types in a simple array' do + it 'ensures all valid types in a simple array' do mock_get('array_of_values') get '/array_of_values' expect { expect_json_types(bad: :array_of_ints) }.to raise_error(ExpectationNotMetError) end - it "should allow array of types to be null" do + it 'allows array of types to be null' do mock_get('array_of_types') get '/array_of_types' expect_json_types(nil_array: :array_or_null) @@ -51,7 +53,7 @@ expect_json_types(nil_array: :array_of_arrays_or_null) end - it "should check array types when not null" do + it 'checks array types when not null' do mock_get('array_of_types') get '/array_of_types' expect_json_types(array_of_ints: :array_or_null) @@ -65,19 +67,19 @@ expect_json_types(array_of_arrays: :array_of_arrays_or_null) end - it 'should allow empty array' do + it 'allows empty array' do mock_get('array_of_values') get '/array_of_values' expect_json_types(emptyArray: :array_of_ints) end - it 'should be able to test for a nil type' do + it 'is able to test for a nil type' do mock_get('simple_get') get '/simple_get' expect_json_types(name: :string, age: :int, address: :null) end - it 'Should throw bad type error' do + it 'throws bad type error' do mock_get('simple_get') get '/simple_get' expect { expect_json_types(name: :foo) }.to raise_error(ExpectationError, "Expected type foo\nis an invalid type") diff --git a/spec/airborne/expectations/expect_status_spec.rb b/spec/airborne/expectations/expect_status_spec.rb index d7817f5..f15d704 100644 --- a/spec/airborne/expectations/expect_status_spec.rb +++ b/spec/airborne/expectations/expect_status_spec.rb @@ -1,19 +1,21 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect_status' do - it 'should verify correct status code' do + it 'verifies correct status code' do mock_get('simple_get') get '/simple_get' expect_status 200 end - it 'should fail when incorrect status code is returned' do + it 'fails when incorrect status code is returned' do mock_get('simple_get') get '/simple_get' expect { expect_status 123 }.to raise_error(ExpectationNotMetError) end - it 'should translate symbol codes to whatever is appropriate for the request' do + it 'translates symbol codes to whatever is appropriate for the request' do mock_get('simple_get') get '/simple_get' expect_status :ok diff --git a/spec/airborne/head_spec.rb b/spec/airborne/head_spec.rb index 9e8dd12..4cd7d01 100644 --- a/spec/airborne/head_spec.rb +++ b/spec/airborne/head_spec.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'head' do - it 'should allow testing on head requests' do + it 'allows testing on head requests' do mock_head('simple_head', 'foo' => 'foo') head '/simple_head', {} expect_status 200 diff --git a/spec/airborne/options_spec.rb b/spec/airborne/options_spec.rb index 545312a..7218e79 100644 --- a/spec/airborne/options_spec.rb +++ b/spec/airborne/options_spec.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'head' do - it 'should allow testing on options requests' do + it 'allows testing on options requests' do mock_options('simple_options', 'foo' => 'foo') options '/simple_options', {} expect_status 200 diff --git a/spec/airborne/patch_spec.rb b/spec/airborne/patch_spec.rb index e2f7208..637cdd7 100644 --- a/spec/airborne/patch_spec.rb +++ b/spec/airborne/patch_spec.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'patch' do - it 'should allow testing on patch requests' do + it 'allows testing on patch requests' do mock_patch('simple_patch') patch '/simple_patch', {} expect_json_types(status: :string, someNumber: :int) diff --git a/spec/airborne/path_spec.rb b/spec/airborne/path_spec.rb index 367a074..0023db2 100644 --- a/spec/airborne/path_spec.rb +++ b/spec/airborne/path_spec.rb @@ -1,32 +1,34 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'expect path' do describe 'errors' do - before :each do + before do mock_get('array_with_index') get '/array_with_index' end - it 'should raise PathError when incorrect path containing .. is used' do + it 'raises PathError when incorrect path containing .. is used' do expect do expect_json('cars..make', 'Tesla') end.to raise_error(PathError, "Invalid Path, contains '..'") end - it 'should raise PathError when trying to call property on an array' do + it 'raises PathError when trying to call property on an array' do expect do expect_json('cars.make', 'Tesla') end.to raise_error(PathError, "Expected Array\nto be an object with property make") end end - it 'should work with numberic properties' do + it 'works with numberic properties' do mock_get('numeric_property') get '/numeric_property' expect_json('cars.0.make', 'Tesla') end - it 'should work with numberic properties' do + it 'works with numberic properties' do mock_get('numeric_property') get '/numeric_property' expect_json_keys('cars.0', [:make, :model]) diff --git a/spec/airborne/post_spec.rb b/spec/airborne/post_spec.rb index 012a5d1..4057774 100644 --- a/spec/airborne/post_spec.rb +++ b/spec/airborne/post_spec.rb @@ -1,24 +1,27 @@ +# frozen_string_literal: true + require 'spec_helper' require 'webmock/rspec' describe 'post' do - it 'should allow testing on post requests' do + it 'allows testing on post requests' do mock_post('simple_post') post '/simple_post', {} expect_json_types(status: :string, someNumber: :int) end - it 'should allow testing on post requests' do + it 'allows testing on post requests' do url = 'http://www.example.com/simple_post' stub_request(:post, url) post '/simple_post', 'hello', content_type: 'text/plain' expect(WebMock).to have_requested(:post, url).with(body: 'hello', headers: { 'Content-Type' => 'text/plain' }) end - it 'should allow testing on post requests with IO body' do + it 'allows testing on post requests with IO body' do url = 'http://www.example.com/simple_post' stub_request(:post, url) post '/simple_post', StringIO.new('hello'), content_type: 'application/octet-stream' - expect(WebMock).to have_requested(:post, url).with(body: 'hello', headers: { 'Content-Type' => 'application/octet-stream' }) + expect(WebMock).to have_requested(:post, url) + .with(body: 'hello', headers: { 'Content-Type' => 'application/octet-stream' }) end end diff --git a/spec/airborne/put_spec.rb b/spec/airborne/put_spec.rb index 730b39a..cd3ddd9 100644 --- a/spec/airborne/put_spec.rb +++ b/spec/airborne/put_spec.rb @@ -1,25 +1,27 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'put' do - it 'should allow testing on put requests w/no body' do + it 'allows testing on put requests w/no body' do mock_put('simple_put') put '/simple_put' expect_json_types(status: :string, someNumber: :int) end - it 'should allow testing on put requests w/empty body' do + it 'allows testing on put requests w/empty body' do mock_put('simple_put') put '/simple_put', {} expect_json_types(status: :string, someNumber: :int) end - it 'should allow testing on put requests w/body' do + it 'allows testing on put requests w/body' do mock_put('simple_put') - put '/simple_put', {:key=>:value} + put '/simple_put', { key: :value } expect_json_types(status: :string, someNumber: :int) end - it 'should allow testing on put requests w/body, empty string' do + it 'allows testing on put requests w/body, empty string' do mock_put('simple_put') put '/simple_put', '' expect_json_types(status: :string, someNumber: :int) diff --git a/spec/airborne/rack/rack_sinatra_spec.rb b/spec/airborne/rack/rack_sinatra_spec.rb index f32235e..f775768 100644 --- a/spec/airborne/rack/rack_sinatra_spec.rb +++ b/spec/airborne/rack/rack_sinatra_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'json' require 'sinatra' @@ -16,28 +18,28 @@ class SampleApp < Sinatra::Application end describe 'rack app' do - it 'should allow requests against a sinatra app' do + it 'allows requests against a sinatra app' do get '/' expect_json_types(foo: :string) end - it 'should ensure correct values from sinatra app' do + it 'ensures correct values from sinatra app' do get '/' expect { expect_json_types(foo: :int) }.to raise_error(ExpectationNotMetError) end - it 'Should set json_body even when not using the airborne http requests' do - Response = Struct.new(:body, :headers) - @response = Response.new({ foo: 'bar' }.to_json) + it 'sets json_body even when not using the airborne http requests' do + response_class = Struct.new(:body, :headers) + @response = response_class.new({ foo: 'bar' }.to_json) expect(json_body).to eq(foo: 'bar') end - it 'Should work with consecutive requests' do - Response = Struct.new(:body, :headers) - @response = Response.new({ foo: 'bar' }.to_json) + it 'works with consecutive requests' do + response_class = Struct.new(:body, :headers) + @response = response_class.new({ foo: 'bar' }.to_json) expect(json_body).to eq(foo: 'bar') - @response = Response.new({ foo: 'boo' }.to_json) + @response = response_class.new({ foo: 'boo' }.to_json) expect(json_body).to eq(foo: 'boo') end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0801c56..ba80968 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'coveralls' Coveralls.wear! require 'airborne' diff --git a/spec/stub_helper.rb b/spec/stub_helper.rb index a231d52..6950319 100644 --- a/spec/stub_helper.rb +++ b/spec/stub_helper.rb @@ -1,32 +1,39 @@ +# frozen_string_literal: true + require 'webmock/rspec' module StubHelper - def initialize(*args) + def initialize(*) @base_url = 'http://www.example.com/' end def mock_get(url, response_headers = {}, status = 200) - stub_request(:get, @base_url + url).to_return(headers: response_headers, body: get_json_response_file(url), status: status) + stub_request(:get, @base_url + url) + .to_return(headers: response_headers, body: get_json_response_file(url), status: status) end def mock_post(url, options = {}, status = 200) - stub_request(:post, @base_url + url).with(body: options[:request_body] || {}) + stub_request(:post, @base_url + url) + .with(body: options[:request_body] || {}) .to_return(headers: options[:response_headers] || {}, body: get_json_response_file(url), status: status) end def mock_put(url, options = {}, status = 200) - stub_request(:put, @base_url + url).with(body: options[:request_body] || {}) + stub_request(:put, @base_url + url) + .with(body: options[:request_body] || {}) .to_return(headers: options[:response_headers] || {}, body: get_json_response_file(url), status: status) end def mock_patch(url, options = {}, status = 200) - stub_request(:patch, @base_url + url).with(body: options[:request_body] || {}) + stub_request(:patch, @base_url + url) + .with(body: options[:request_body] || {}) .to_return(headers: options[:response_headers] || {}, body: get_json_response_file(url), status: status) end def mock_delete(url, options = {}, status = 200) - stub_request(:delete, @base_url + url).with(body: options[:request_body] || {}) - .to_return(headers: options[:response_headers] || {}, body: get_json_response_file(url), status: status) + stub_request(:delete, @base_url + url) + .with(body: options[:request_body] || {}) + .to_return(headers: options[:response_headers] || {}, body: get_json_response_file(url), status: status) end def mock_head(url, response_headers = {}, status = 200) @@ -40,6 +47,6 @@ def mock_options(url, response_headers = {}, status = 200) private def get_json_response_file(name) - IO.read("spec/test_responses/#{name}.json") + File.read("spec/test_responses/#{name}.json") end end diff --git a/spec/test_responses/simple_nested_path.json b/spec/test_responses/simple_nested_path.json index 985fff7..6ad4777 100644 --- a/spec/test_responses/simple_nested_path.json +++ b/spec/test_responses/simple_nested_path.json @@ -5,8 +5,8 @@ "city": "Roswell", "state": "NM", "coordinates":{ - "lattitude": 33.3872, - "longitutde": 104.5281 + "latitude": 33.3872, + "longitude": 104.5281 } } } \ No newline at end of file