Permalink
Browse files

Fix #785 & add regression test.

  • Loading branch information...
1 parent a0df2cb commit 4dd3d1a1a2534f2996a368ebe26114bf974e15f9 @sigmavirus24 sigmavirus24 committed Sep 20, 2012
Showing with 38 additions and 2 deletions.
  1. +4 −2 requests/models.py
  2. +34 −0 tests/test_requests.py
View
@@ -462,8 +462,10 @@ def path_url(self):
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.
View
@@ -744,6 +744,40 @@ def add_bar_header(args):
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()

0 comments on commit 4dd3d1a

Please sign in to comment.