mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Allow ActiveResource subclasses to inherit headers from parent
This commit is contained in:
parent
11d1bdc09e
commit
0101d27de6
2 changed files with 52 additions and 0 deletions
|
@ -588,6 +588,12 @@ module ActiveResource
|
|||
|
||||
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 @@ class BaseTest < ActiveSupport::TestCase
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue