From cf9477d7c4fd1ec21f555f5861860f4a8ddad2ba Mon Sep 17 00:00:00 2001 From: JonMidhir Date: Fri, 6 Nov 2015 00:18:36 +0000 Subject: [PATCH 1/2] Fix inaccurate step definition Step def appears to check against user defined class - In reality always checking against String. Change fixes this and also adds support for nested classes. --- features/steps/httparty_response_steps.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/features/steps/httparty_response_steps.rb b/features/steps/httparty_response_steps.rb index 2d5f1c6..7dc64d5 100644 --- a/features/steps/httparty_response_steps.rb +++ b/features/steps/httparty_response_steps.rb @@ -11,8 +11,8 @@ def constantize(camel_cased_word) constant end -Then /it should return an? (\w+)$/ do |class_string| - expect(@response_from_httparty).to be_a(class_string.class) +Then /it should return an? ([\w\:]+)$/ do |class_string| + expect(@response_from_httparty.parsed_response).to be_a(Object.const_get(class_string)) end Then /the return value should match '(.*)'/ do |expected_text| @@ -20,7 +20,7 @@ Then /the return value should match '(.*)'/ do |expected_text| end Then /it should return a Hash equaling:/ do |hash_table| - expect(@response_from_httparty).to be_a(Hash) + expect(@response_from_httparty.parsed_response).to be_a(Hash) expect(@response_from_httparty.keys.length).to eq(hash_table.rows.length) hash_table.hashes.each do |pair| key, value = pair["key"], pair["value"] @@ -30,7 +30,7 @@ Then /it should return a Hash equaling:/ do |hash_table| end Then /it should return an Array equaling:/ do |array| - expect(@response_from_httparty).to be_a(Array) + expect(@response_from_httparty.parsed_response).to be_a(Array) expect(@response_from_httparty.parsed_response).to eq(array.raw) end From c4a031d70d09a5d1dff7661ad71030bddbcf6e71 Mon Sep 17 00:00:00 2001 From: JonMidhir Date: Fri, 6 Nov 2015 00:22:18 +0000 Subject: [PATCH 2/2] Add support for is_a? and kind_of? to HTTParty::Response. Previously these were delegated to the parsed_response, leading to inaccurate output and unpredictable behaviour. For example: response.class == HTTParty::Response #true response.is_a?(HTTParty::Response) # false --- lib/httparty/response.rb | 6 ++++++ spec/httparty/response_spec.rb | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/httparty/response.rb b/lib/httparty/response.rb index 6c7d7aa..bd4a66b 100644 --- a/lib/httparty/response.rb +++ b/lib/httparty/response.rb @@ -27,6 +27,12 @@ module HTTParty Response end + def is_a?(klass) + self.class == klass || self.class < klass + end + + alias_method :kind_of?, :is_a? + def code response.code.to_i end diff --git a/spec/httparty/response_spec.rb b/spec/httparty/response_spec.rb index 296f179..5967ed6 100644 --- a/spec/httparty/response_spec.rb +++ b/spec/httparty/response_spec.rb @@ -114,6 +114,24 @@ RSpec.describe HTTParty::Response do end end + describe "#is_a?" do + subject { HTTParty::Response.new(@request_object, @response_object, @parsed_response) } + + it { is_expected.to respond_to(:is_a?).with(1).arguments } + it { expect(subject.is_a?(HTTParty::Response)).to be_truthy } + it { expect(subject.is_a?(BasicObject)).to be_truthy } + it { expect(subject.is_a?(Object)).to be_falsey } + end + + describe "#kind_of?" do + subject { HTTParty::Response.new(@request_object, @response_object, @parsed_response) } + + it { is_expected.to respond_to(:kind_of?).with(1).arguments } + it { expect(subject.kind_of?(HTTParty::Response)).to be_truthy } + it { expect(subject.kind_of?(BasicObject)).to be_truthy } + it { expect(subject.kind_of?(Object)).to be_falsey } + end + describe "semantic methods for response codes" do def response_mock(klass) response = klass.new('', '', '')