Skip to content

Commit

Permalink
Merge pull request #383 from meanphil/fix-header-inheritance
Browse files Browse the repository at this point in the history
Fix subclasses not inheriting headers set in the superclass
  • Loading branch information
rafaelfranca authored Jan 24, 2024
2 parents df28dee + 47fd8a0 commit 1517cad
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/active_resource/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def default_header

# Builds headers for request to remote service.
def build_request_headers(headers, http_method, uri)
authorization_header(http_method, uri).update(default_header).update(http_format_header(http_method)).update(headers)
authorization_header(http_method, uri).update(default_header).update(http_format_header(http_method)).update(headers.to_hash)
end

def response_auth_header
Expand Down
19 changes: 19 additions & 0 deletions lib/active_resource/inheriting_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,24 @@ def initialize(parent_hash = {})
def [](key)
super || @parent_hash[key]
end

# Merges the flattened parent hash (if it's an InheritingHash)
# with ourself
def to_hash
@parent_hash.to_hash.merge(self)
end

# So we can see the merged object in IRB or the Rails console
def pretty_print(pp)
pp.pp_hash to_hash
end

def inspect
to_hash.inspect
end

def to_s
inspect
end
end
end
2 changes: 1 addition & 1 deletion test/abstract_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def setup_response
mock.get "/posts/1.json", {}, @post
mock.get "/posts/1/comments.json", {}, @comments
# products
mock.get "/products/1.json", {}, @product
mock.get "/products/1.json", { "Accept" => "application/json", "X-Inherited-Header" => "present" }, @product
mock.get "/products/1/inventory.json", {}, @inventory
# pets
mock.get "/people/1/pets.json", {}, @pets
Expand Down
1 change: 1 addition & 0 deletions test/cases/authorization_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "base64"
require "abstract_unit"

class AuthorizationTest < ActiveSupport::TestCase
Expand Down
19 changes: 19 additions & 0 deletions test/cases/inheritence_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require "abstract_unit"

require "fixtures/product"

class InheritenceTest < ActiveSupport::TestCase
def test_sub_class_retains_ancestor_headers
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/sub_products/1.json",
{ "Accept" => "application/json", "X-Inherited-Header" => "present" },
{ id: 1, name: "Sub Product" }.to_json,
200
end

sub_product = SubProduct.find(1)
assert_equal "SubProduct", sub_product.class.to_s
end
end
25 changes: 25 additions & 0 deletions test/cases/inheriting_hash_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

class InheritingHashTest < ActiveSupport::TestCase
def setup
@parent = ActiveResource::InheritingHash.new({ override_me: "foo", parent_key: "parent_value" })
@child = ActiveResource::InheritingHash.new(@parent)
@child[:override_me] = "bar"
@child[:child_only] = "baz"
end

def test_child_key_overrides_parent_key
assert_equal "bar", @child[:override_me]
end

def test_parent_key_available_on_lookup
assert_equal "parent_value", @child[:parent_key]
end

def test_conversion_to_regular_hash_includes_parent_keys
hash = @child.to_hash

assert_equal 3, hash.keys.length
assert_equal "parent_value", hash[:parent_key]
end
end
6 changes: 6 additions & 0 deletions test/fixtures/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

class Product < ActiveResource::Base
self.site = "http://37s.sunrise.i:3000"
# X-Inherited-Header is for testing that any subclasses
# include the headers of this class
self.headers["X-Inherited-Header"] = "present"
end

class SubProduct < Product
end

0 comments on commit 1517cad

Please sign in to comment.