summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Buccella <chris.buccella@antallagon.com>2014-11-06 15:33:14 -0500
committerChris Buccella <chris.buccella@antallagon.com>2014-11-07 01:01:53 -0500
commit68f271aae2c45cd65d5a93659a812f9b8e823212 (patch)
tree1fdb3b86d24b2ff34c512ab9c3649c4e8890f98b
parenteace20c14a0d4e19d1cbd48ce73cfa81da5f6319 (diff)
downloadpython-swiftclient-68f271aae2c45cd65d5a93659a812f9b8e823212.tar.gz
Check that content_type header exists before using
When downloading an object, the content_type header is inspected to determine if a directory needs to be created. This header is assumed to always be in the response; if it isn't an Exception is raised. swiftclient should not assume content_type will always be set. Change-Id: I156195c02b6e0bc398fa962eb1f78c4dbddd1596
-rw-r--r--swiftclient/service.py3
-rw-r--r--tests/unit/test_shell.py28
2 files changed, 30 insertions, 1 deletions
diff --git a/swiftclient/service.py b/swiftclient/service.py
index 55e6daa..c74e0a5 100644
--- a/swiftclient/service.py
+++ b/swiftclient/service.py
@@ -1020,7 +1020,8 @@ class SwiftService(object):
try:
no_file = options['no_download']
content_type = headers.get('content-type')
- if content_type.split(';', 1)[0] == 'text/directory':
+ if (content_type and
+ content_type.split(';', 1)[0] == 'text/directory'):
make_dir = not no_file and out_file != "-"
if make_dir and not isdir(path):
mkdirs(path)
diff --git a/tests/unit/test_shell.py b/tests/unit/test_shell.py
index e6434dc..0e88e94 100644
--- a/tests/unit/test_shell.py
+++ b/tests/unit/test_shell.py
@@ -300,6 +300,34 @@ class TestShell(unittest.TestCase):
response_dict={})
mock_open.assert_called_with('object', 'wb')
+ @mock.patch('swiftclient.service.Connection')
+ def test_download_no_content_type(self, connection):
+ connection.return_value.get_object.return_value = [
+ {'etag': 'd41d8cd98f00b204e9800998ecf8427e'},
+ '']
+
+ # Test downloading whole container
+ connection.return_value.get_container.side_effect = [
+ [None, [{'name': 'object'}]],
+ [None, [{'name': 'pseudo/'}]],
+ [None, []],
+ ]
+ connection.return_value.auth_end_time = 0
+ connection.return_value.attempts = 0
+
+ with mock.patch(BUILTIN_OPEN) as mock_open:
+ argv = ["", "download", "container"]
+ swiftclient.shell.main(argv)
+ calls = [mock.call('container', 'object',
+ headers={}, resp_chunk_size=65536,
+ response_dict={}),
+ mock.call('container', 'pseudo/',
+ headers={}, resp_chunk_size=65536,
+ response_dict={})]
+ connection.return_value.get_object.assert_has_calls(
+ calls, any_order=True)
+ mock_open.assert_called_once_with('object', 'wb')
+
@mock.patch('swiftclient.shell.walk')
@mock.patch('swiftclient.service.Connection')
def test_upload(self, connection, walk):