mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
Fix #785 & add regression test.
This commit is contained in:
+4
-2
@@ -462,8 +462,10 @@ class Request(object):
|
||||
|
||||
def register_hook(self, event, hook):
|
||||
"""Properly register a hook."""
|
||||
|
||||
self.hooks[event].append(hook)
|
||||
if isinstance(hook, (list, tuple, set)):
|
||||
self.hooks[event].extend(hook)
|
||||
else:
|
||||
self.hooks[event].append(hook)
|
||||
|
||||
def deregister_hook(self, event, hook):
|
||||
"""Deregister a previously registered hook.
|
||||
|
||||
@@ -744,6 +744,40 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase):
|
||||
assert 'foo' in response.text
|
||||
assert 'bar' in response.text
|
||||
|
||||
def test_allow_list_of_hooks_to_register_hook(self):
|
||||
"""Issue 785: https://github.com/kennethreitz/requests/issues/785"""
|
||||
def add_foo_header(args):
|
||||
if not args.get('headers'):
|
||||
args['headers'] = {}
|
||||
|
||||
args['headers'].update({
|
||||
'X-Foo': 'foo'
|
||||
})
|
||||
|
||||
return args
|
||||
|
||||
def add_bar_header(args):
|
||||
if not args.get('headers'):
|
||||
args['headers'] = {}
|
||||
|
||||
args['headers'].update({
|
||||
'X-Bar': 'bar'
|
||||
})
|
||||
|
||||
return args
|
||||
|
||||
def assert_hooks_are_callable(hooks):
|
||||
for h in hooks['args']:
|
||||
assert callable(h) is True
|
||||
|
||||
hooks = [add_foo_header, add_bar_header]
|
||||
r = requests.models.Request()
|
||||
r.register_hook('args', hooks)
|
||||
assert_hooks_are_callable(r.hooks)
|
||||
|
||||
r = requests.models.Request(hooks={'args': hooks})
|
||||
assert_hooks_are_callable(r.hooks)
|
||||
|
||||
def test_session_persistent_cookies(self):
|
||||
|
||||
s = requests.session()
|
||||
|
||||
Reference in New Issue
Block a user