Merge "Update session headers during initialization of AgentClient"

This commit is contained in:
Jenkins 2015-04-14 03:39:47 +00:00 committed by Gerrit Code Review
commit 7ff924574a
2 changed files with 8 additions and 11 deletions

View File

@ -37,6 +37,7 @@ class AgentClient(object):
"""Client for interacting with nodes via a REST API."""
def __init__(self):
self.session = requests.Session()
self.session.headers.update({'Content-Type': 'application/json'})
def _get_command_url(self, node):
agent_url = node.driver_internal_info.get('agent_url')
@ -64,13 +65,9 @@ class AgentClient(object):
request_params = {
'wait': str(wait).lower()
}
headers = {
'Content-Type': 'application/json'
}
response = self.session.post(url,
params=request_params,
data=body,
headers=headers)
data=body)
# TODO(russellhaering): real error handling
try:
@ -86,8 +83,7 @@ class AgentClient(object):
def get_commands_status(self, node):
url = self._get_command_url(node)
headers = {'Content-Type': 'application/json'}
res = self.session.get(url, headers=headers)
res = self.session.get(url)
return res.json()['commands']
def prepare_image(self, node, image_info, wait=False):

View File

@ -58,6 +58,11 @@ class TestAgentClient(base.TestCase):
self.client.session = mock.Mock(autospec=requests.Session)
self.node = MockNode()
def test_content_type_header(self):
client = agent_client.AgentClient()
self.assertEqual('application/json',
client.session.headers['Content-Type'])
def test__get_command_url(self):
command_url = self.client._get_command_url(self.node)
expected = self.node.driver_internal_info['agent_url'] + '/v1/commands'
@ -84,14 +89,12 @@ class TestAgentClient(base.TestCase):
url = self.client._get_command_url(self.node)
body = self.client._get_command_body(method, params)
headers = {'Content-Type': 'application/json'}
response = self.client._command(self.node, method, params)
self.assertEqual(response, response_data)
self.client.session.post.assert_called_once_with(
url,
data=body,
headers=headers,
params={'wait': 'false'})
def test__command_fail_json(self):
@ -103,7 +106,6 @@ class TestAgentClient(base.TestCase):
url = self.client._get_command_url(self.node)
body = self.client._get_command_body(method, params)
headers = {'Content-Type': 'application/json'}
self.assertRaises(exception.IronicException,
self.client._command,
@ -111,7 +113,6 @@ class TestAgentClient(base.TestCase):
self.client.session.post.assert_called_once_with(
url,
data=body,
headers=headers,
params={'wait': 'false'})
def test_get_commands_status(self):