add some unit tests for HTTPCommand

Sem-Ver: bugfix
Change-Id: Ibf9d255478869126da313ab54767d3d76126b899
This commit is contained in:
Vincent Llorens 2017-02-20 15:02:15 +01:00
parent 6485fa60e7
commit ea955b7405
1 changed files with 61 additions and 0 deletions

View File

@ -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)