summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Burke <tim.burke@gmail.com>2016-02-26 11:25:10 -0800
committerTim Burke <tim.burke@gmail.com>2016-02-26 11:25:10 -0800
commitaa0edd00966237163451fc44cda2c593a5215cbe (patch)
tree3c1dc39a71ec08b02d638273b5985fc483c0fc50
parentee905477ac6fad7c43e875d7e52b22a494ef3d6f (diff)
downloadpython-swiftclient-aa0edd00966237163451fc44cda2c593a5215cbe.tar.gz
Force header keys/values to bytes/unicode before coercing to unicode
Previously, parse_header_string was only called with data coming out of requests, which would be either bytes or unicode. Now that we're sending it request headers as well (see related change), we need to be more defensive. If the value given is neither bytes nor unicode, convert it to a native string. This will allow developers using the client API to continue sending header dicts like {'X-Delete-After': 2} ...as in Swift's test/probe/test_object_expirer.py Change-Id: Ie57a93274507b184af5cad4260f244359a585f09 Related-Change: I43dd7254f7281d4db59b286aa2145643c64e1705
-rw-r--r--swiftclient/client.py2
-rw-r--r--tests/unit/test_swiftclient.py7
2 files changed, 7 insertions, 2 deletions
diff --git a/swiftclient/client.py b/swiftclient/client.py
index 8375fed..d2f089a 100644
--- a/swiftclient/client.py
+++ b/swiftclient/client.py
@@ -169,6 +169,8 @@ def http_log(args, kwargs, resp, body):
def parse_header_string(data):
+ if not isinstance(data, (six.text_type, six.binary_type)):
+ data = str(data)
if six.PY2:
if isinstance(data, six.text_type):
# Under Python2 requests only returns binary_type, but if we get
diff --git a/tests/unit/test_swiftclient.py b/tests/unit/test_swiftclient.py
index ae144e2..2c552be 100644
--- a/tests/unit/test_swiftclient.py
+++ b/tests/unit/test_swiftclient.py
@@ -1203,13 +1203,16 @@ class TestPostObject(MockHttpTest):
def test_ok(self):
c.http_connection = self.fake_http_connection(200)
+ delete_at = 2.1 # not str! we don't know what other devs will use!
args = ('http://www.test.com', 'token', 'container', 'obj',
- {'X-Object-Meta-Test': 'mymeta'})
+ {'X-Object-Meta-Test': 'mymeta',
+ 'X-Delete-At': delete_at})
c.post_object(*args)
self.assertRequests([
('POST', '/container/obj', '', {
'x-auth-token': 'token',
- 'X-Object-Meta-Test': 'mymeta'}),
+ 'X-Object-Meta-Test': 'mymeta',
+ 'X-Delete-At': delete_at}),
])
def test_unicode_ok(self):