summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2018-01-11 07:12:08 +0000
committerGerrit Code Review <review@openstack.org>2018-01-11 07:12:08 +0000
commit17a953a899fc6a4ddb5cab164530763ffbd072af (patch)
tree21f40a444d565b1c75178fe063e32a7e51304359
parent05218d9500c899f2f4f145d03c9cda7f65df6a45 (diff)
parentcefa2432f677b8812330950055f285b13790080a (diff)
downloadpython-neutronclient-17a953a899fc6a4ddb5cab164530763ffbd072af.tar.gz
Merge "Pass headers to httpclient"
-rw-r--r--neutronclient/client.py7
-rw-r--r--neutronclient/tests/unit/test_http.py18
-rw-r--r--neutronclient/v2_0/client.py3
3 files changed, 24 insertions, 4 deletions
diff --git a/neutronclient/client.py b/neutronclient/client.py
index 395f7db..2482eb4 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 73b7a30..21b6155 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 2384705..232320b 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,