Convert headers to bytes testing request_headers

In requests 1.2 and python 3 headers are bytes so we need to optionally
convert unicode text to bytes when testing that the request headers
match those being checked for.

Change-Id: I02dadc05dd5a93769da646686b938c2b26f6f267
Closes-Bug: #1361528
This commit is contained in:
Jamie Lennox 2014-08-26 17:39:25 +10:00
parent 79cd58d9d1
commit befd86afdf

View File

@ -163,14 +163,25 @@ class _Matcher(_RequestHistoryTracker):
def _match_headers(self, request):
for k, vals in six.iteritems(self._request_headers):
try:
header = request.headers[k]
except KeyError:
return False
else:
if header != vals:
# NOTE(jamielennox): This seems to be a requests 1.2/2
# difference, in 2 they are just whatever the user inputted in
# 1 they are bytes. Let's optionally handle both and look at
# removing this when we depend on requests 2.
if not isinstance(k, six.text_type):
return False
try:
header = request.headers[k.encode('utf-8')]
except KeyError:
return False
if header != vals:
return False
return True
def _match(self, request):