Permalink
Browse files

Allow ActiveResource subclasses to inherit headers from parent

  • Loading branch information...
markmcspadden committed Feb 14, 2012
1 parent 11d1bdc commit 0101d27de606db25c244aae2243f337cfbba6ed0
Showing with 52 additions and 0 deletions.
  1. +6 −0 activeresource/lib/active_resource/base.rb
  2. +46 −0 activeresource/test/cases/base_test.rb
@@ -588,6 +588,12 @@ def connection(refresh = false)
def headers
@headers ||= {}
+
+ if superclass != Object && superclass.headers
+ @headers = superclass.headers.merge(@headers)
+ else
+ @headers
+ end
end
attr_writer :element_name
@@ -437,6 +437,52 @@ def test_updating_baseclass_timeout_wipes_descendent_cached_connection_objects
assert_not_equal(first_connection, second_connection, 'Connection should be re-created')
end
+ def test_header_inheritance
+ fruit = Class.new(ActiveResource::Base)
+ apple = Class.new(fruit)
+ fruit.site = 'http://market'
+
+ fruit.headers['key'] = 'value'
+ assert_equal 'value', apple.headers['key']
+ end
+
+ def test_header_inheritance_set_at_multiple_points
+ fruit = Class.new(ActiveResource::Base)
+ apple = Class.new(fruit)
+ fruit.site = 'http://market'
+
+ fruit.headers['key'] = 'value'
+ assert_equal 'value', apple.headers['key']
+
+ apple.headers['key2'] = 'value2'
+ fruit.headers['key3'] = 'value3'
+
+ assert_equal 'value', apple.headers['key']
+ assert_equal 'value2', apple.headers['key2']
+ assert_equal 'value3', apple.headers['key3']
+ end
+
+ def test_header_inheritance_should_not_leak_upstream
+ fruit = Class.new(ActiveResource::Base)
+ apple = Class.new(fruit)
+ fruit.site = 'http://market'
+
+ fruit.headers['key'] = 'value'
+
+ apple.headers['key2'] = 'value2'
+ assert_equal nil, fruit.headers['key2']
+ end
+
+ def test_header_inheritance_should_not_leak_upstream
+ fruit = Class.new(ActiveResource::Base)
+ apple = Class.new(fruit)
+ fruit.site = 'http://market'
+
+ fruit.headers['key'] = 'value'
+
+ apple.headers['key2'] = 'value2'
+ assert_equal nil, fruit.headers['key2']
+ end
########################################################################
# Tests for setting up remote URLs for a given model (including adding

0 comments on commit 0101d27

Please sign in to comment.