cast to str for allowable types

Change-Id: Ie2680328ff352718ab266f6533e27eee532d68bb
Closes-Bug: 1617762
This commit is contained in:
Renato Recio 2016-09-02 10:50:31 -05:00
parent 471ce2d043
commit cbc0a9928a
2 changed files with 49 additions and 0 deletions

View File

@ -20,6 +20,7 @@ from email.mime import text
import json
import requests
import six
import smtplib
import time
@ -143,6 +144,11 @@ class HTTPAction(base.Action):
else:
self.auth = auth
if isinstance(headers, dict):
for key, val in headers.items():
if isinstance(val, (six.integer_types, float)):
headers[key] = str(val)
self.url = url
self.method = method
self.params = params

View File

@ -162,3 +162,46 @@ class HTTPActionTest(base.BaseTest):
proxies=None,
verify=None
)
@mock.patch.object(requests, 'request')
def test_http_action_with_headers(self, mocked_method):
mocked_method.return_value = get_success_fake_response()
headers = {'int_header': 33, 'bool_header': True,
'float_header': 3.0, 'regular_header': 'teststring'}
safe_headers = {'int_header': '33', 'bool_header': 'True',
'float_header': '3.0', 'regular_header': 'teststring'}
action = std.HTTPAction(
url=URL,
method='POST',
body=DATA,
headers=headers.copy(),
)
data_str = json.dumps(DATA)
self.assertEqual(data_str, action.body)
self.assertEqual(URL, action.url)
result = action.run()
self.assertIsInstance(result, dict)
self.assertEqual(DATA, result['content'])
self.assertIn('headers', result)
self.assertEqual(200, result['status'])
mocked_method.assert_called_with(
'POST',
URL,
data=data_str,
headers=safe_headers,
cookies=None,
params=None,
timeout=None,
auth=None,
allow_redirects=None,
proxies=None,
verify=None
)