From cefa2432f677b8812330950055f285b13790080a Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Mon, 11 Dec 2017 05:26:39 +0000 Subject: [PATCH] Pass headers to httpclient Right now, it is impossible to set HTTP headers in client because the 'headers' parameter is not set. This patch fixes it. Change-Id: Iff002aaca516e8d014074f120b7f713758ebf265 Partial-Bug: #1737473 --- neutronclient/client.py | 7 ++++--- neutronclient/tests/unit/test_http.py | 18 ++++++++++++++++++ neutronclient/v2_0/client.py | 3 ++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/neutronclient/client.py b/neutronclient/client.py index 395f7dbe5..2482eb434 100644 --- a/neutronclient/client.py +++ b/neutronclient/client.py @@ -190,7 +190,7 @@ class HTTPClient(object): # might be because the auth token expired, so try to # re-authenticate and try again. If it still fails, bail. try: - kwargs.setdefault('headers', {}) + kwargs['headers'] = kwargs.get('headers') or {} if self.auth_token is None: self.auth_token = "" kwargs['headers']['X-Auth-Token'] = self.auth_token @@ -199,7 +199,7 @@ class HTTPClient(object): return resp, body except exceptions.Unauthorized: self.authenticate() - kwargs.setdefault('headers', {}) + kwargs['headers'] = kwargs.get('headers') or {} kwargs['headers']['X-Auth-Token'] = self.auth_token resp, body = self._cs_request( self.endpoint_url + url, method, **kwargs) @@ -311,7 +311,7 @@ class SessionClient(adapter.Adapter): content_type = kwargs.pop('content_type', None) or 'application/json' - headers = kwargs.setdefault('headers', {}) + headers = kwargs.get('headers') or {} headers.setdefault('Accept', content_type) # NOTE(dbelova): osprofiler_web.get_trace_id_headers does not add any @@ -327,6 +327,7 @@ class SessionClient(adapter.Adapter): if kwargs.get('data'): headers.setdefault('Content-Type', content_type) + kwargs['headers'] = headers resp = super(SessionClient, self).request(*args, **kwargs) return resp, resp.text diff --git a/neutronclient/tests/unit/test_http.py b/neutronclient/tests/unit/test_http.py index 73b7a30b0..21b615592 100644 --- a/neutronclient/tests/unit/test_http.py +++ b/neutronclient/tests/unit/test_http.py @@ -123,6 +123,24 @@ class TestHTTPClient(TestHTTPClientMixin, testtools.TestCase): self.assertEqual(403, resp.status_code) self.assertEqual(text, resp_text) + def test_do_request_success(self): + text = 'test content' + self.requests.register_uri(METHOD, END_URL + URL, text=text) + + resp, resp_text = self.http.do_request(URL, METHOD) + self.assertEqual(200, resp.status_code) + self.assertEqual(text, resp_text) + + def test_do_request_with_headers_success(self): + text = 'test content' + self.requests.register_uri(METHOD, END_URL + URL, text=text, + request_headers={'key': 'value'}) + + resp, resp_text = self.http.do_request(URL, METHOD, + headers={'key': 'value'}) + self.assertEqual(200, resp.status_code) + self.assertEqual(text, resp_text) + class TestHTTPClientWithReqId(TestHTTPClientMixin, testtools.TestCase): """Tests for when global_request_id is set.""" diff --git a/neutronclient/v2_0/client.py b/neutronclient/v2_0/client.py index f4e49f895..36bb79321 100644 --- a/neutronclient/v2_0/client.py +++ b/neutronclient/v2_0/client.py @@ -277,7 +277,8 @@ class ClientBase(object): if body: body = self.serialize(body) - resp, replybody = self.httpclient.do_request(action, method, body=body) + resp, replybody = self.httpclient.do_request(action, method, body=body, + headers=headers) status_code = resp.status_code if status_code in (requests.codes.ok,