From 328d6a8d457b8be6fc4b3dfdbb396a44f0b8710b Mon Sep 17 00:00:00 2001
From: Alistair Coles <alistair.coles@hp.com>
Date: Tue, 22 Sep 2015 11:09:44 +0100
Subject: [PATCH] Add tests and param definitions for headers parameter

Cleanups for change I35c3b266b3c733f6b1629de4c683ea7d40128032

Add missing param definitions to client get_container
and head_object docstrings.

For consistency, add headers parameter to the Connection class
head_object and head_container wrapper methods.

Add tests to verify that the headers parameter of Connection
get_container, head_container and head_object methods is passed to the
module functions.

Change-Id: Ib40d5b626b2793840727c58cffbf725bea55651f
---
 swiftclient/client.py          | 11 ++++---
 tests/unit/test_swiftclient.py | 53 ++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 4 deletions(-)

diff --git a/swiftclient/client.py b/swiftclient/client.py
index 1913ea36..2bbd978f 100644
--- a/swiftclient/client.py
+++ b/swiftclient/client.py
@@ -656,6 +656,7 @@ def get_container(url, token, container, marker=None, limit=None,
     :param full_listing: if True, return a full listing, else returns a max
                          of 10000 listings
     :param service_token: service auth token
+    :param headers: additional headers to include in the request
     :returns: a tuple of (response headers, a list of objects) The response
               headers will be a dict and all header names will be lowercase.
     :raises ClientException: HTTP GET request failed
@@ -735,6 +736,7 @@ def head_container(url, token, container, http_conn=None, headers=None,
     :param container: container name to get stats for
     :param http_conn: HTTP connection object (If None, it will create the
                       conn object)
+    :param headers: additional headers to include in the request
     :param service_token: service auth token
     :returns: a dict containing the response's headers (all header names will
               be lowercase)
@@ -974,6 +976,7 @@ def head_object(url, token, container, name, http_conn=None,
     :param http_conn: HTTP connection object (If None, it will create the
                       conn object)
     :param service_token: service auth token
+    :param headers: additional headers to include in the request
     :returns: a dict containing the response's headers (all header names will
               be lowercase)
     :raises ClientException: HTTP HEAD request failed
@@ -1453,9 +1456,9 @@ class Connection(object):
         return self._retry(None, post_account, headers,
                            response_dict=response_dict)
 
-    def head_container(self, container):
+    def head_container(self, container, headers=None):
         """Wrapper for :func:`head_container`"""
-        return self._retry(None, head_container, container)
+        return self._retry(None, head_container, container, headers=headers)
 
     def get_container(self, container, marker=None, limit=None, prefix=None,
                       delimiter=None, end_marker=None, path=None,
@@ -1484,9 +1487,9 @@ class Connection(object):
         return self._retry(None, delete_container, container,
                            response_dict=response_dict)
 
-    def head_object(self, container, obj):
+    def head_object(self, container, obj, headers=None):
         """Wrapper for :func:`head_object`"""
-        return self._retry(None, head_object, container, obj)
+        return self._retry(None, head_object, container, obj, headers=headers)
 
     def get_object(self, container, obj, resp_chunk_size=None,
                    query_string=None, response_dict=None, headers=None):
diff --git a/tests/unit/test_swiftclient.py b/tests/unit/test_swiftclient.py
index 410fd6f5..01e393c7 100644
--- a/tests/unit/test_swiftclient.py
+++ b/tests/unit/test_swiftclient.py
@@ -1708,6 +1708,59 @@ class TestConnection(MockHttpTest):
         finally:
             c.http_connection = orig_conn
 
+    def test_get_container(self):
+        headers = {'X-Favourite-Pet': 'Aardvark'}
+        with mock.patch('swiftclient.client.http_connection',
+                        self.fake_http_connection(200, body=b'{}')):
+            with mock.patch('swiftclient.client.get_auth',
+                            lambda *a, **k: ('http://url:8080/v1/a', 'token')):
+                conn = c.Connection()
+                conn.get_container('c1', prefix='p', limit=5,
+                                   headers=headers)
+        self.assertEqual(1, len(self.request_log), self.request_log)
+        self.assertRequests([
+            ('GET', '/v1/a/c1?format=json&limit=5&prefix=p', '', {
+                'x-auth-token': 'token',
+                'X-Favourite-Pet': 'Aardvark',
+            }),
+        ])
+        self.assertEqual(conn.attempts, 1)
+
+    def test_head_container(self):
+        headers = {'X-Favourite-Pet': 'Aardvark'}
+        with mock.patch('swiftclient.client.http_connection',
+                        self.fake_http_connection(200, body=b'{}')):
+            with mock.patch('swiftclient.client.get_auth',
+                            lambda *a, **k: ('http://url:8080/v1/a', 'token')):
+                conn = c.Connection()
+                conn.head_container('c1', headers=headers)
+        self.assertEqual(1, len(self.request_log), self.request_log)
+        self.assertRequests([
+            ('HEAD', '/v1/a/c1', '', {
+                'x-auth-token': 'token',
+                'X-Favourite-Pet': 'Aardvark',
+            }),
+        ])
+        self.assertEqual(conn.attempts, 1)
+
+    def test_head_object(self):
+        headers = {'X-Favourite-Pet': 'Aardvark'}
+        with mock.patch('swiftclient.client.http_connection',
+                        self.fake_http_connection(200)):
+            with mock.patch('swiftclient.client.get_auth',
+                            lambda *a, **k: ('http://url:8080/v1/a', 'token')):
+                conn = c.Connection()
+                conn.head_object('c1', 'o1',
+                                 headers=headers)
+        self.assertEqual(1, len(self.request_log), self.request_log)
+        self.assertRequests([
+            ('HEAD', '/v1/a/c1/o1', '', {
+                'x-auth-token': 'token',
+                'X-Favourite-Pet': 'Aardvark',
+            }),
+        ])
+        self.assertEqual(conn.attempts, 1)
+
 
 class TestResponseDict(MockHttpTest):
     """