Merge "Add content_length to header in upload_blob"

This commit is contained in:
Jenkins 2017-08-15 10:53:40 +00:00 committed by Gerrit Code Review
commit 601c73af30
2 changed files with 48 additions and 3 deletions

View File

@ -13,6 +13,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import io
import os
import tempfile
import mock
from oslo_serialization import jsonutils
import testtools
@ -110,13 +114,38 @@ class TestController(testtools.TestCase):
self.mock_http_client.delete.assert_called_once_with(
'/artifacts/checked_name/test-id')
def test_upload_blob(self):
def test_upload_blob_str(self):
self.c.upload_blob('test-id', 'blob-prop', 'data',
type_name='test-type',
content_type='application/test')
self.mock_http_client.put.assert_called_once_with(
'/artifacts/checked_name/test-id/blob-prop',
data='data', headers={'Content-Type': 'application/test'})
data='data', headers={'Content-Length': '4',
'Content-Type': 'application/test'})
def test_upload_blob_file(self):
tfd, path = tempfile.mkstemp()
try:
os.write(tfd, b'data')
tfile = open(path, "rb")
self.c.upload_blob('test-id', 'blob-prop', tfile,
type_name='test-type',
content_type='application/test')
self.mock_http_client.put.assert_called_once_with(
'/artifacts/checked_name/test-id/blob-prop',
data=tfile, headers={'Content-Length': '4',
'Content-Type': 'application/test'})
finally:
os.remove(path)
def test_upload_blob_stream(self):
data = io.BytesIO(b'data')
self.c.upload_blob('test-id', 'blob-prop', data,
type_name='test-type',
content_type='application/test')
self.mock_http_client.put.assert_called_once_with(
'/artifacts/checked_name/test-id/blob-prop',
data=data, headers={'Content-Type': 'application/test'})
def test_get_type_list(self):
schemas = {'schemas': {'a': {'version': 1}, 'b': {'version': 2}}}

View File

@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
from oslo_serialization import jsonutils
from oslo_utils import encodeutils
import six
@ -244,8 +246,22 @@ class Controller(object):
:param blob_property: blob property name
"""
content_type = content_type or 'application/octet-stream'
type_name = self._check_type_name(type_name)
hdrs = {'Content-Type': content_type}
type_name = self._check_type_name(type_name)
content_length = None
if isinstance(data, six.string_types):
content_length = len(data)
else:
try:
content_length = os.path.getsize(data.name)
except Exception:
# if for some reason we can't get the file size, then we just
# ignore it.
pass
if content_length is not None:
hdrs['Content-Length'] = str(content_length)
url = '/artifacts/%s/%s/%s' % (type_name, artifact_id, blob_property)
self.http_client.put(url, headers=hdrs, data=data)