Skip to content
Myr

Make hooks work with prepared requests

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by sprt · committed by sprt · parent 3491761 · Browse files · 3 files+31 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+31 added−5 removed

  • other:+31 added−5 removed

3 files+31 added−5 removed

modified:requests/models.py+17 added-1 removed
9 unmodified lines
10import collections11import logging1213from io import BytesIO14from .hooks import default_hooks14from .hooks import default_hooks, HOOKS15from .structures import CaseInsensitiveDict16from .status_codes import codes1718from .auth import HTTPBasicAuth
205 unmodified lines
224 p.prepare_body(self.data, self.files)225 # Note that prepare_auth must be last to enable authentication schemes226 # such as OAuth to work on a fully prepared request.227 p.prepare_auth(self.auth)228 p.prepare_hooks(self.hooks)229230 return p231232
182 unmodified lines
415 cookie_header = get_cookie_header(cookies, self)416 if cookie_header is not None:417 self.headers['Cookie'] = cookie_header418419 def prepare_hooks(self, hooks):420 """Prepares the given hooks."""421 for event in HOOKS:422 if event not in self.hooks:423 self.hooks[event] = []424 if event not in hooks:425 hooks[event] = []426427 if not hasattr(self.hooks[event], '__iter__'):428 self.hooks[event] = [self.hooks[event]]429 if not hasattr(hooks[event], '__iter__'):430 hooks[event] = [hooks[event]]431432 self.hooks[event].extend(hooks[event])433434435class Response(object):436 """The :class:`Response <Response>` object, which contains a437 server's response to an HTTP request.438 """
219 unmodified lines
modified:requests/sessions.py+1 added-4 removed
11 unmodified lines
1213from .compat import cookielib14from .cookies import cookiejar_from_dict15from .models import Request16from .hooks import dispatch_hook, default_hooks16from .hooks import default_hooks17from .utils import from_key_val_list, default_headers18from .exceptions import TooManyRedirects, InvalidSchema1920from .compat import urlparse, urljoin
272 unmodified lines
293 history.insert(0, resp)294 resp = history.pop()295 resp.history = tuple(history)296297 # Response manipulation hook.298 self.response = dispatch_hook('response', hooks, resp)299297 return resp298299 def get(self, url, **kwargs):300 """Sends a GET request. Returns :class:`Response` object.
104 unmodified lines
modified:test_requests.py+13 added
262 unmodified lines
263 'text/py-content-type')})264 self.assertEqual(r.status_code, 200)265 self.assertTrue(b"text/py-content-type" in r.request.body)266267 def test_prepared_request_hook(self):268 def hook(resp):269 resp.hook_working = True270 return resp271272 req = requests.Request('GET', HTTPBIN, hooks={'response': hook})273 prep = req.prepare()274275 s = requests.Session()276 resp = s.send(prep)277278 self.assertTrue(hasattr(resp, 'hook_working'))279280281if __name__ == '__main__':282 unittest.main()