Skip to content

Commit

Permalink
Fix on old versions of requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Roguelazer committed Apr 2, 2015
2 parents 89b932e + f7f9d6f commit 8d60450
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cassette/http_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
import ssl
from httplib import HTTPConnection, HTTPSConnection

import semver

log = logging.getLogger("cassette")


class CassetteConnectionMixin(object):
_delete_sock_when_returning_from_library = False

def request(self, method, url, body=None, headers=None):
"""Send HTTP request."""
Expand All @@ -31,6 +34,10 @@ def request(self, method, url, body=None, headers=None):
if self._cassette_name in lib:
self._response = lib[self._cassette_name]

if self._delete_sock_when_returning_from_library:
if hasattr(self, 'sock') and self.sock is None:
delattr(self, 'sock')

return

log.warning("Making external HTTP request: %s" % self._cassette_name)
Expand Down Expand Up @@ -117,13 +124,20 @@ def connect(self):

try:
from requests.packages import urllib3 as requests_urllib3
import requests
except ImportError:
pass
else:
class UL3CassetteHTTPConnection(CassetteConnectionMixin,
requests_urllib3.connection.HTTPConnection):

_baseclass = requests_urllib3.connection.HTTPConnection
# requests 2.3.0 and below have an issue where they get confused if
# the HTTPConnection has a "sock" attribute which is set to None at
# the end of a request/response cycle. So we delete the attribute in
# those cases
_delete_sock_when_returning_from_library = True if \
semver.compare(requests.__version__, '2.4.0') == -1 else False

class UL3CassetteHTTPSConnection(UL3CassetteHTTPConnection,
CassetteConnectionMixin,
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
PyYAML>=3.10
semver>=2.0

0 comments on commit 8d60450

Please sign in to comment.