From cbc0a9928a973345a9084b2cc05880a79b5425f0 Mon Sep 17 00:00:00 2001 From: Renato Recio Date: Fri, 2 Sep 2016 10:50:31 -0500 Subject: [PATCH] cast to str for allowable types Change-Id: Ie2680328ff352718ab266f6533e27eee532d68bb Closes-Bug: 1617762 --- mistral/actions/std_actions.py | 6 +++ .../unit/actions/test_std_http_action.py | 43 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/mistral/actions/std_actions.py b/mistral/actions/std_actions.py index 68d5133a3..37f06321a 100644 --- a/mistral/actions/std_actions.py +++ b/mistral/actions/std_actions.py @@ -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 diff --git a/mistral/tests/unit/actions/test_std_http_action.py b/mistral/tests/unit/actions/test_std_http_action.py index 91de7dac0..5d4c10a68 100644 --- a/mistral/tests/unit/actions/test_std_http_action.py +++ b/mistral/tests/unit/actions/test_std_http_action.py @@ -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 + )