From ea3dd2bd872436b104af71c229e442a470693e03 Mon Sep 17 00:00:00 2001 From: Rohitt Vashishtha Date: Wed, 14 Jun 2017 03:44:53 +0530 Subject: [PATCH] bots: Add HTTP status code support to mock http conversations. --- bots/giphy/fixtures/test_1.json | 26 ++++++++++++++++++++++++ bots/giphy/fixtures/test_1_request.json | 7 ------- bots/giphy/fixtures/test_1_response.json | 12 ----------- bots_api/bots_test_lib.py | 22 ++++++++++++-------- 4 files changed, 39 insertions(+), 28 deletions(-) create mode 100644 bots/giphy/fixtures/test_1.json delete mode 100644 bots/giphy/fixtures/test_1_request.json delete mode 100644 bots/giphy/fixtures/test_1_response.json diff --git a/bots/giphy/fixtures/test_1.json b/bots/giphy/fixtures/test_1.json new file mode 100644 index 0000000..5a6eae8 --- /dev/null +++ b/bots/giphy/fixtures/test_1.json @@ -0,0 +1,26 @@ +{ + "request": { + "api_url": "http://api.giphy.com/v1/gifs/translate", + "params": { + "s": "Hello", + "api_key": "12345678" + } + }, + "response": { + "meta": { + "status": 200 + }, + "data": { + "images": { + "original": { + "url": "https://media4.giphy.com/media/3o6ZtpxSZbQRRnwCKQ/giphy.gif" + } + } + } + }, + "response-headers": { + "status": 200, + "ok": true, + "content-type": "application/json; charset=utf-8" + } +} diff --git a/bots/giphy/fixtures/test_1_request.json b/bots/giphy/fixtures/test_1_request.json deleted file mode 100644 index 89238c2..0000000 --- a/bots/giphy/fixtures/test_1_request.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "api_url": "http://api.giphy.com/v1/gifs/translate", - "params": { - "s": "Hello", - "api_key": "12345678" - } -} diff --git a/bots/giphy/fixtures/test_1_response.json b/bots/giphy/fixtures/test_1_response.json deleted file mode 100644 index edb8691..0000000 --- a/bots/giphy/fixtures/test_1_response.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "meta": { - "status": 200 - }, - "data": { - "images": { - "original": { - "url": "https://media4.giphy.com/media/3o6ZtpxSZbQRRnwCKQ/giphy.gif" - } - } - } -} diff --git a/bots_api/bots_test_lib.py b/bots_api/bots_test_lib.py index 3572f24..c6c279e 100644 --- a/bots_api/bots_test_lib.py +++ b/bots_api/bots_test_lib.py @@ -112,20 +112,24 @@ class BotTestCase(TestCase): assert test_name is not None base_path = os.path.realpath(os.path.join(os.path.dirname( os.path.abspath(__file__)), '..', 'bots', self.bot_name, 'fixtures')) - http_request_path = os.path.join(base_path, '{}_request.json'.format(test_name)) - http_response_path = os.path.join(base_path, '{}_response.json'.format(test_name)) - with open(http_request_path, 'r') as http_request_file, \ - open(http_response_path, 'r') as http_response_file: - http_request = json.load(http_request_file) - http_response = json.load(http_response_file) + http_data_path = os.path.join(base_path, '{}.json'.format(test_name)) + with open(http_data_path, 'r') as http_data_file: + http_data = json.load(http_data_file) + http_request = http_data.get('request') + http_response = http_data.get('response') + http_headers = http_data.get('response-headers') with patch('requests.get') as mock_get: mock_result = mock.MagicMock() mock_result.json.return_value = http_response - mock_result.ok.return_value = True + mock_result.status_code = http_headers.get('status', 200) + mock_result.ok.return_value = http_headers.get('ok', True) mock_get.return_value = mock_result yield - mock_get.assert_called_with(http_request['api_url'], - params=http_request['params']) + params = http_request.get('params', None) + if params is None: + mock_get.assert_called_with(http_request['api_url']) + else: + mock_get.assert_called_with(http_request['api_url'], params=params) def assert_bot_response(self, message, response, expected_method): # type: (Dict[str, Any], Dict[str, Any], str) -> None