Skip to content

Commit 1517cad

Browse files
authored
Merge pull request #383 from meanphil/fix-header-inheritance
Fix subclasses not inheriting headers set in the superclass
2 parents df28dee + 47fd8a0 commit 1517cad

File tree

7 files changed

+72
-2
lines changed

7 files changed

+72
-2
lines changed

lib/active_resource/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def default_header
211211

212212
# Builds headers for request to remote service.
213213
def build_request_headers(headers, http_method, uri)
214-
authorization_header(http_method, uri).update(default_header).update(http_format_header(http_method)).update(headers)
214+
authorization_header(http_method, uri).update(default_header).update(http_format_header(http_method)).update(headers.to_hash)
215215
end
216216

217217
def response_auth_header

lib/active_resource/inheriting_hash.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,24 @@ def initialize(parent_hash = {})
1111
def [](key)
1212
super || @parent_hash[key]
1313
end
14+
15+
# Merges the flattened parent hash (if it's an InheritingHash)
16+
# with ourself
17+
def to_hash
18+
@parent_hash.to_hash.merge(self)
19+
end
20+
21+
# So we can see the merged object in IRB or the Rails console
22+
def pretty_print(pp)
23+
pp.pp_hash to_hash
24+
end
25+
26+
def inspect
27+
to_hash.inspect
28+
end
29+
30+
def to_s
31+
inspect
32+
end
1433
end
1534
end

test/abstract_unit.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def setup_response
142142
mock.get "/posts/1.json", {}, @post
143143
mock.get "/posts/1/comments.json", {}, @comments
144144
# products
145-
mock.get "/products/1.json", {}, @product
145+
mock.get "/products/1.json", { "Accept" => "application/json", "X-Inherited-Header" => "present" }, @product
146146
mock.get "/products/1/inventory.json", {}, @inventory
147147
# pets
148148
mock.get "/people/1/pets.json", {}, @pets

test/cases/authorization_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require "base64"
34
require "abstract_unit"
45

56
class AuthorizationTest < ActiveSupport::TestCase

test/cases/inheritence_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
require "abstract_unit"
4+
5+
require "fixtures/product"
6+
7+
class InheritenceTest < ActiveSupport::TestCase
8+
def test_sub_class_retains_ancestor_headers
9+
ActiveResource::HttpMock.respond_to do |mock|
10+
mock.get "/sub_products/1.json",
11+
{ "Accept" => "application/json", "X-Inherited-Header" => "present" },
12+
{ id: 1, name: "Sub Product" }.to_json,
13+
200
14+
end
15+
16+
sub_product = SubProduct.find(1)
17+
assert_equal "SubProduct", sub_product.class.to_s
18+
end
19+
end

test/cases/inheriting_hash_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
class InheritingHashTest < ActiveSupport::TestCase
4+
def setup
5+
@parent = ActiveResource::InheritingHash.new({ override_me: "foo", parent_key: "parent_value" })
6+
@child = ActiveResource::InheritingHash.new(@parent)
7+
@child[:override_me] = "bar"
8+
@child[:child_only] = "baz"
9+
end
10+
11+
def test_child_key_overrides_parent_key
12+
assert_equal "bar", @child[:override_me]
13+
end
14+
15+
def test_parent_key_available_on_lookup
16+
assert_equal "parent_value", @child[:parent_key]
17+
end
18+
19+
def test_conversion_to_regular_hash_includes_parent_keys
20+
hash = @child.to_hash
21+
22+
assert_equal 3, hash.keys.length
23+
assert_equal "parent_value", hash[:parent_key]
24+
end
25+
end

test/fixtures/product.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@
22

33
class Product < ActiveResource::Base
44
self.site = "http://37s.sunrise.i:3000"
5+
# X-Inherited-Header is for testing that any subclasses
6+
# include the headers of this class
7+
self.headers["X-Inherited-Header"] = "present"
8+
end
9+
10+
class SubProduct < Product
511
end

0 commit comments

Comments
 (0)