def setUp(self): AwsExceptionsFactory._EX_REGISTRY.clear() AwsExceptionsFactory.add_exception("TestEx", TestEx) self._http_cls = Mock() self._http_module = Mock(return_value=self._http_cls) self._http_client = AwsHttpClient(self._http_module)
class AwsHttpClientTests(unittest.TestCase): '''Class used to provide all test cases for aws http client.''' def setUp(self): AwsExceptionsFactory._EX_REGISTRY.clear() AwsExceptionsFactory.add_exception("TestEx", TestEx) self._http_cls = Mock() self._http_module = Mock(return_value=self._http_cls) self._http_client = AwsHttpClient(self._http_module) def test_do_request_exception(self): '''Test case for checking that an http error response is correctly converted to a strong type exception.''' url = "/aws/test" headers = {"Content-Type": "application/json"} action = "TestAction" resp = Mock() resp.status = 403 content = {"ErrorResponse": {"Error": {"Code": "TestEx", "Type": "Sender", "Message": "Test"}, "RequestId": "123"}} self._http_cls.request = lambda url, method, headers: (resp, json.dumps(content).encode()) self.assertRaises(TestEx, self._http_client.do_request, *[url, headers, action, "GET"]) def test_do_request_no_ex(self): '''Test case for checking that a response is correctly retrieved when no error occurs.''' url = "/aws/test" headers = {"Content-Type": "application/json"} action = "Simple" resp = Mock() resp.status = 200 content = {"SimpleResponse": {"SimpleResult": "Cool stuff"}} self._http_cls.request = lambda url, method, headers: (resp, json.dumps(content).encode()) response = self._http_client.do_request(url, headers, action) self.assertIsNotNone(response) self.assertEquals("Cool stuff", response) def test_do_request_no_body(self): '''Test case for checking that a response is correctly retrieved when no body is returned.''' url = "/aws/test" headers = {"Content-Type": "application/json"} action = "TestAction" resp = Mock() resp.status = 201 self._http_cls.request = lambda url, method, headers: (resp, None) response = self._http_client.do_request(url, headers, action, "POST") self.assertIsNotNone(response) self.assertEquals({}, response)