From ea955b7405be30dc09e9f15238985c8aaf0f2e09 Mon Sep 17 00:00:00 2001 From: Vincent Llorens Date: Mon, 20 Feb 2017 15:02:15 +0100 Subject: [PATCH] add some unit tests for HTTPCommand Sem-Ver: bugfix Change-Id: Ibf9d255478869126da313ab54767d3d76126b899 --- .../unit/test_client_command_httpcommand.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 synergy/tests/unit/test_client_command_httpcommand.py diff --git a/synergy/tests/unit/test_client_command_httpcommand.py b/synergy/tests/unit/test_client_command_httpcommand.py new file mode 100644 index 0000000..1b8300c --- /dev/null +++ b/synergy/tests/unit/test_client_command_httpcommand.py @@ -0,0 +1,61 @@ +# coding: utf-8 +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +""" +Test the HTTPCommand class. + +""" + +import mock +import requests + +from synergy.client.command import HTTPCommand +from synergy.tests import base + + +class TestHTTPCommand(base.TestCase): + + def setUp(self): + super(TestHTTPCommand, self).setUp() + self.http_command = HTTPCommand(name="dummy_httpcmd") + + def test_get_name(self): + self.assertEqual("dummy_httpcmd", self.http_command.getName()) + + def test_configure_parser(self): + """This method should be implemented in sub-classes.""" + self.assertRaises( + NotImplementedError, + self.http_command.configureParser, + "dummy_subparser") + + def test_execute_request_fail(self): + mock_response = mock.Mock() + # bad status code that will raise an error + mock_response.status_code = 400 + + with mock.patch.object(requests, "get", return_value=mock_response): + self.http_command.execute("dummy_url") + + mock_response.raise_for_status.assert_called() + + def test_execute_success(self): + mock_response = mock.Mock() + mock_response.text = '{"test": true}' # mock a simple json response + + with mock.patch.object(requests, "get", return_value=mock_response)\ + as m: + result = self.http_command.execute("dummy_url") + + m.assert_called_once_with("dummy_url", params=None) + self.assertEqual({"test": True}, result)