diff --git a/glanceclient/common/http.py b/glanceclient/common/http.py index 8a1f1721..0909e959 100644 --- a/glanceclient/common/http.py +++ b/glanceclient/common/http.py @@ -157,6 +157,10 @@ class HTTPClient(object): headers = kwargs.pop("headers", {}) headers = headers and copy.deepcopy(headers) or {} + if self.identity_headers: + for k, v in six.iteritems(self.identity_headers): + headers.setdefault(k, v) + # Default Content-Type is octet-stream content_type = headers.get('Content-Type', 'application/octet-stream') diff --git a/tests/test_http.py b/tests/test_http.py index 31cd6d53..54e56e2c 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -14,6 +14,7 @@ # under the License. import json +import mock from mox3 import mox import requests import six @@ -91,6 +92,31 @@ class TestClient(testtools.TestCase): self.assertIsNone(http_client_object.auth_token) self.assertNotIn('X-Auth-Token', http_client_object.session.headers) + def test_identity_headers_are_passed(self): + # Tests that if token or X-Auth-Token are not provided in the kwargs + # when creating the http client, the session headers don't contain + # the X-Auth-Token key. + identity_headers = { + 'X-User-Id': 'user', + 'X-Tenant-Id': 'tenant', + 'X-Roles': 'roles', + 'X-Identity-Status': 'Confirmed', + 'X-Service-Catalog': 'service_catalog', + } + kwargs = {'identity_headers': identity_headers} + http_client = http.HTTPClient(self.endpoint, **kwargs) + + def check_headers(*args, **kwargs): + headers = kwargs.get('headers') + for k, v in six.iteritems(identity_headers): + self.assertEqual(v, headers[k]) + + return utils.FakeResponse({}, six.StringIO('{}')) + + with mock.patch.object(http_client.session, 'request') as mreq: + mreq.side_effect = check_headers + http_client.get('http://example.com:9292/v1/images/my-image') + def test_connection_refused(self): """ Should receive a CommunicationError if connection refused.