Merge "Py3: Fixes header key dict"

This commit is contained in:
Jenkins 2016-11-11 13:32:07 +00:00 committed by Gerrit Code Review
commit 7b69efe1b6
3 changed files with 40 additions and 1 deletions

View File

@ -40,8 +40,10 @@ class HeaderKeyDict(dict):
def __setitem__(self, key, value):
if value is None:
self.pop(key.title(), None)
elif isinstance(value, six.text_type):
elif six.PY2 and isinstance(value, six.text_type):
return dict.__setitem__(self, key.title(), value.encode('utf-8'))
elif six.PY3 and isinstance(value, six.binary_type):
return dict.__setitem__(self, key.title(), value.decode('latin-1'))
else:
return dict.__setitem__(self, key.title(), str(value))

View File

@ -58,6 +58,42 @@ class TestHeaderKeyDict(unittest.TestCase):
self.assertEqual(headers['Content-Length'], '0')
self.assertEqual(headers['Content-Type'], 'text/plain')
def test_set_none(self):
headers = HeaderKeyDict()
headers['test'] = None
self.assertNotIn('test', headers)
headers['test'] = 'something'
self.assertEqual('something', headers['test']) # sanity check
headers['test'] = None
self.assertNotIn('test', headers)
def test_init_from_dict(self):
headers = HeaderKeyDict({'Content-Length': 20,
'Content-Type': 'text/plain'})
self.assertEqual('20', headers['Content-Length'])
self.assertEqual('text/plain', headers['Content-Type'])
headers = HeaderKeyDict(headers)
self.assertEqual('20', headers['Content-Length'])
self.assertEqual('text/plain', headers['Content-Type'])
def test_set(self):
# mappings = ((<tuple of input vals>, <expected output val>), ...)
mappings = (((1.618, '1.618', b'1.618', u'1.618'), '1.618'),
((20, '20', b'20', u'20'), '20'),
((True, 'True', b'True', u'True'), 'True'),
((False, 'False', b'False', u'False'), 'False'))
for vals, expected in mappings:
for val in vals:
headers = HeaderKeyDict(test=val)
actual = headers['test']
self.assertEqual(expected, actual,
'Expected %s but got %s for val %s' %
(expected, actual, val))
self.assertIsInstance(
actual, str,
'Expected type str but got %s for val %s of type %s' %
(type(actual), val, type(val)))
def test_get(self):
headers = HeaderKeyDict()
headers['content-length'] = 20

View File

@ -30,6 +30,7 @@ setenv = VIRTUAL_ENV={envdir}
commands =
nosetests \
test/unit/common/test_exceptions.py \
test/unit/common/test_header_key_dict.py \
test/unit/common/test_splice.py
[testenv:py35]