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
This commit is contained in:
parent
7926406568
commit
cefa2432f6
@ -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
|
||||
|
||||
|
@ -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."""
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user