From 2f31c0469dd48c3a7052faeb8b5d786d5a32c21a Mon Sep 17 00:00:00 2001 From: Lokesh S Date: Thu, 28 Jul 2016 13:31:47 +0000 Subject: [PATCH] Py3: Fixes header key dict Decode header to latin1 on python3 encode header to utf-8 on python2. Co-Authored-By: Alistair Coles Change-Id: I10f205a05bb3a566e52a597d9315b3a8b8c14664 --- swift/common/header_key_dict.py | 4 ++- test/unit/common/test_header_key_dict.py | 36 ++++++++++++++++++++++++ tox.ini | 1 + 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/swift/common/header_key_dict.py b/swift/common/header_key_dict.py index fc67bb0f..3c22afd1 100644 --- a/swift/common/header_key_dict.py +++ b/swift/common/header_key_dict.py @@ -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)) diff --git a/test/unit/common/test_header_key_dict.py b/test/unit/common/test_header_key_dict.py index dee1fa05..465bf95c 100644 --- a/test/unit/common/test_header_key_dict.py +++ b/test/unit/common/test_header_key_dict.py @@ -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 = ((, ), ...) + 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 diff --git a/tox.ini b/tox.ini index 4ffa99e3..98baa074 100644 --- a/tox.ini +++ b/tox.ini @@ -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]