Expose Fake objects in compat

The FakeHTTPMessage and FakeHTTPResponse objects were only defined for
old versions of requests to provide compatibility. It turns out we need
these for cookie extraction in new versions as well so always define the
object and leave the old compatibility check.

Change-Id: Ifb42fd1c6b1b895b26bee59632ad0dedd78a6256
This commit is contained in:
Jamie Lennox 2015-11-18 10:08:25 +11:00
parent ba28c09408
commit 1b10e30f1e
1 changed files with 30 additions and 20 deletions

View File

@ -20,31 +20,41 @@ def _versiontuple(v):
_requests_version = _versiontuple(requests.__version__)
if _requests_version < (2, 3):
class _FakeHTTPMessage(object):
def __init__(self, headers):
self.headers = headers
def getheaders(self, name):
try:
return [self.headers[name]]
except KeyError:
return []
def get_all(self, name, failobj=None):
# python 3 only, overrides email.message.Message.get_all
try:
return [self.headers[name]]
except KeyError:
return failobj
class _FakeHTTPResponse(object):
def __init__(self, headers):
self.msg = _FakeHTTPMessage(headers)
def isclosed(self):
# Don't let urllib try to close me
return False
if _requests_version < (2, 3):
# NOTE(jamielennox): There is a problem with requests < 2.3.0 such that it
# needs a httplib message for use with cookie extraction. It has been fixed
# but it is needed until we can rely on a recent enough requests version.
class _FakeHTTPMessage(object):
def getheaders(self, name):
return None
def get_all(self, name, failobj=None):
# python 3 only, overrides email.message.Message.get_all
return failobj
class _FakeHTTPResponse(object):
def __init__(self):
self.msg = _FakeHTTPMessage()
def isclosed(self):
# Don't let urllib try to close me
return False
_fake_http_response = _FakeHTTPResponse()
_fake_http_response = _FakeHTTPResponse({})
else:
_fake_http_response = None