fix contextlib.closing bug for sessions where content is not consumed (issue #2593)

This commit is contained in:
colin
2015-05-24 09:59:35 -04:00
parent 3c850b3339
commit 8d0889b91b
2 changed files with 26 additions and 0 deletions
+3
View File
@@ -842,4 +842,7 @@ class Response(object):
*Note: Should not normally need to be called explicitly.*
"""
if not self._content_consumed:
return self.raw.close()
return self.raw.release_conn()
+23
View File
@@ -9,6 +9,8 @@ import os
import pickle
import unittest
import collections
import contextlib
import random
import io
import requests
@@ -1252,6 +1254,27 @@ class TestCaseInsensitiveDict(unittest.TestCase):
assert frozenset(cid) == keyset
class TestSessionContextClosing(unittest.TestCase):
def _perform_session_without_consuming(self):
session = requests.Session()
urls = [
'http://reddit.com',
'http://github.com',
'http://bitbucket.org',
]
for _ in range(25):
with contextlib.closing(session.get(random.choice(urls), stream=True)) as r:
pass
return "OK"
def test_stream_session_content_not_consumed(self):
self.assertEqual(self._perform_session_without_consuming(), "OK")
class UtilsTestCase(unittest.TestCase):
def test_super_len_io_streams(self):