Skip to content

Commit 71a3b7b

Browse files
committed
Merge pull request #296 from cganterh/master
There was a problem with headers when a binary string is passed (like b'Authorization').
2 parents df1cca4 + 1cf37bd commit 71a3b7b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

python3/httplib2/__init__.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,13 @@ def safename(filename):
192192

193193
NORMALIZE_SPACE = re.compile(r'(?:\r\n)?[ \t]+')
194194
def _normalize_headers(headers):
195-
return dict([ (key.lower(), NORMALIZE_SPACE.sub(value, ' ').strip()) for (key, value) in headers.items()])
195+
return dict([ (_convert_byte_str(key).lower(), NORMALIZE_SPACE.sub(_convert_byte_str(value), ' ').strip()) for (key, value) in headers.items()])
196196

197+
def _convert_byte_str(s):
198+
if not isinstance(s, str):
199+
return str(s, 'utf-8')
200+
return s
201+
197202
def _parse_cache_control(headers):
198203
retval = {}
199204
if 'cache-control' in headers:

python3/httplib2test.py

+6
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,12 @@ def testNormalizeHeaders(self):
12351235
self.assertTrue('cache-control' in h)
12361236
self.assertTrue('other' in h)
12371237
self.assertEqual('Stuff', h['other'])
1238+
1239+
def testConvertByteStr(self):
1240+
with self.assertRaises(TypeError):
1241+
httplib2._convert_byte_str(4)
1242+
self.assertEqual('Hello World', httplib2._convert_byte_str(b'Hello World'))
1243+
self.assertEqual('Bye World', httplib2._convert_byte_str('Bye World'))
12381244

12391245
def testExpirationModelTransparent(self):
12401246
# Test that no-cache makes our request TRANSPARENT

0 commit comments

Comments
 (0)