Fix the elapsed test and #1133
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Ian Cordasco · committed by Ian Cordasco · parent 207a274 · Browse files · 2 files·+53 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+53 added−15 removed
- other:+53 added−15 removed
Provenance
- Commit
a95bfd3- Tree
d1b36be- Author
Ian Cordasco <graffatcolmingov@gmail.com>- Committer
Ian Cordasco <graffatcolmingov@gmail.com>- Parent
207a274
2 files changed+53 added−15 removed
modified:requests/sessions.py+48 added-14 removed
73 unmodified lines74 75 76 class SessionRedirectMixin(object):77 78- def resolve_redirects(self, resp, req, stream=False, timeout=None, verify=True, cert=None, proxies=None):78+ def resolve_redirects(self, resp, req, stream=False, timeout=None,79+ verify=True, cert=None, proxies=None):80 """Receives a Response. Returns a generator of Responses."""81 82 i = 083 192 unmodified lines276 # Prepare the Request.277 prep = req.prepare()278 279 # Send the request.279- resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)280+ send_kwargs = {281+ 'stream': stream,282+ 'timeout': timeout,283+ 'verify': verify,284+ 'cert': cert,285+ 'proxies': proxies,286+ 'allow_redirects': allow_redirects,287+ 'req': req,288+ }289+ resp = self.send(prep, **send_kwargs)290 291 # Persist cookies.292 for cookie in resp.cookies:293 self.cookies.set_cookie(cookie)294 285- # Redirect resolving generator.286- gen = self.resolve_redirects(resp, req, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)287-288- # Resolve redirects if allowed.289- history = [r for r in gen] if allow_redirects else []290-291- # Shuffle things around if there's history.292- if history:293- history.insert(0, resp)294- resp = history.pop()295- resp.history = tuple(history)296-295 return resp296 297 def get(self, url, **kwargs):298 """Sends a GET request. Returns :class:`Response` object.65 unmodified lines364 return self.request('DELETE', url, **kwargs)365 366 def send(self, request, **kwargs):367 """Send a given PreparedRequest."""368+ # Set up variables needed for resolve_redirects and dispatching of369+ # hooks370+ allow_redirects = True371+ if 'allow_redirects' in kwargs:372+ allow_redirects = kwargs.pop('allow_redirects')373+ req = None374+ if 'req' in kwargs:375+ req = kwargs.pop('req')376+ stream = kwargs.get('stream', False)377+ timeout = kwargs.get('timeout')378+ verify = kwargs.get('verify')379+ cert = kwargs.get('cert')380+ proxies = kwargs.get('proxies')381 hooks = request.hooks382+383+ # Get the appropriate adapter to use384 adapter = self.get_adapter(url=request.url)385+386+ # Start time (approximately) of the request387 start = datetime.utcnow()388+ # Send the request389 r = adapter.send(request, **kwargs)390+ # Total elapsed time of the request (approximately)391 r.elapsed = datetime.utcnow() - start392+393+ # Redirect resolving generator.394+ gen = self.resolve_redirects(r, req, stream=stream, timeout=timeout,395+ verify=verify, cert=cert, proxies=proxies)396+397+ # Resolve redirects if allowed.398+ history = [resp for resp in gen] if allow_redirects else []399+400+ # Shuffle things around if there's history.401+ if history:402+ # Insert the first (original) request at the start403+ history.insert(0, r)404+ # Get the last request made405+ r = history.pop()406+ r.history = tuple(history)407+408 # Response manipulation hooks409 r = dispatch_hook('response', hooks, r)410+411 return r412 413 def get_adapter(self, url):414 """Returns the appropriate connnection adapter for the given URL."""29 unmodified linesmodified:test_requests.py+5 added-1 removed
1 unmodified line2 # -*- coding: utf-8 -*-3 4 """Tests for Requests."""5 6+from __future__ import division7 import json8 import os9 import unittest10 312 unmodified lines323 self.assertEqual(cookie._rest['HttpOnly'], rest['HttpOnly'])324 325 def test_time_elapsed_blank(self):326 r = requests.get(httpbin('get'))326- self.assertTrue(r.elapsed.total_seconds() > 0.0)327+ td = r.elapsed328+ total_seconds = ((td.microseconds + (td.seconds + td.days * 24 * 3600)329+ * 10**6) / 10**6)330+ self.assertTrue(total_seconds > 0.0)331 332 def test_response_is_iterable(self):333 r = requests.Response()334 io = StringIO.StringIO('abc')6 unmodified lines