Merge "Added headers argument support to get_object()"

This commit is contained in:
Jenkins 2013-08-02 19:23:23 +00:00 committed by Gerrit Code Review
commit c626c2a87e
2 changed files with 27 additions and 4 deletions

View File

@ -691,7 +691,7 @@ def delete_container(url, token, container, http_conn=None,
def get_object(url, token, container, name, http_conn=None,
resp_chunk_size=None, query_string=None,
response_dict=None):
response_dict=None, headers=None):
"""
Get an object
@ -708,6 +708,8 @@ def get_object(url, token, container, name, http_conn=None,
:param query_string: if set will be appended with '?' to generated path
:param response_dict: an optional dictionary into which to place
the response - status, reason and headers
:param headers: an optional dictionary with additional headers to include
in the request
:returns: a tuple of (response headers, the object's contents) The response
headers will be a dict and all header names will be lowercase.
:raises ClientException: HTTP GET request failed
@ -720,7 +722,8 @@ def get_object(url, token, container, name, http_conn=None,
if query_string:
path += '?' + query_string
method = 'GET'
headers = {'X-Auth-Token': token}
headers = headers.copy() if headers else {}
headers['X-Auth-Token'] = token
conn.request(method, path, '', headers)
resp = conn.getresponse()
@ -1178,12 +1181,12 @@ class Connection(object):
return self._retry(None, head_object, container, obj)
def get_object(self, container, obj, resp_chunk_size=None,
query_string=None, response_dict=None):
query_string=None, response_dict=None, headers=None):
"""Wrapper for :func:`get_object`"""
return self._retry(None, get_object, container, obj,
resp_chunk_size=resp_chunk_size,
query_string=query_string,
response_dict=response_dict)
response_dict=response_dict, headers=headers)
def put_object(self, container, obj, contents, content_length=None,
etag=None, chunk_size=None, content_type=None,

View File

@ -520,6 +520,26 @@ class TestGetObject(MockHttpTest):
c.get_object('http://www.test.com', 'asdf', 'asdf', 'asdf',
query_string="hello=20")
def test_request_headers(self):
request_args = {}
def fake_request(method, url, body=None, headers=None):
request_args['method'] = method
request_args['url'] = url
request_args['body'] = body
request_args['headers'] = headers
return
conn = self.fake_http_connection(200)('http://www.test.com/')
conn[1].request = fake_request
headers = {'Range': 'bytes=1-2'}
c.get_object('url_is_irrelevant', 'TOKEN', 'container', 'object',
http_conn=conn, headers=headers)
self.assertFalse(request_args['headers'] is None,
"No headers in the request")
self.assertTrue('Range' in request_args['headers'],
"No Range header in the request")
self.assertEquals(request_args['headers']['Range'], 'bytes=1-2')
class TestHeadObject(MockHttpTest):