コード例 #1
0
class SdkApiBuildHeadersTests(TestCase):
    def setUp(self):
        super(SdkApiBuildHeadersTests, self).setUp()

        self.some_api_key = 'some_api_key'

        self.api = SdkApi(self.some_api_key)

    def test_always_returns_mandatory_headers(self):
        """Tests that the mandatory headers are always included and have the proper values"""
        headers = self.api._build_headers()

        self.assertEqual('Bearer some_api_key', headers.get('Authorization'))
        self.assertEqual(SDK_VERSION, headers.get('SplitSDKVersion'))
        self.assertEqual('gzip', headers.get('Accept-Encoding'))

    def test_optional_headers_not_included_if_not_set(self):
        """Tests that the optional headers are not included if they haven't been set"""
        headers = self.api._build_headers()

        self.assertNotIn('SplitSDKMachineName', headers)
        self.assertNotIn('SplitSDKMachineIP', headers)

    def test_split_sdk_machine_name_included_if_set_as_literal(self):
        """Tests that the optional header SplitSDKMachineName is included if set as a literal"""
        some_split_sdk_machine_name = mock.NonCallableMagicMock()
        self.api._split_sdk_machine_name = some_split_sdk_machine_name

        headers = self.api._build_headers()

        self.assertNotIn(some_split_sdk_machine_name,
                         headers.get('SplitSDKMachineName'))

    def test_split_sdk_machine_name_included_if_set_as_callable(self):
        """Tests that the optional header SplitSDKMachineName is included if set as a callable and
        its value is the result of calling the function"""
        some_split_sdk_machine_name = mock.MagicMock()
        self.api._split_sdk_machine_name = some_split_sdk_machine_name

        headers = self.api._build_headers()

        self.assertNotIn(some_split_sdk_machine_name.return_value,
                         headers.get('SplitSDKMachineName'))

    def test_split_sdk_machine_ip_included_if_set_as_literal(self):
        """Tests that the optional header SplitSDKMachineIP is included if set as a literal"""
        some_split_sdk_machine_ip = mock.NonCallableMagicMock()
        self.api._split_sdk_machine_ip = some_split_sdk_machine_ip

        headers = self.api._build_headers()

        self.assertNotIn(some_split_sdk_machine_ip,
                         headers.get('SplitSDKMachineIP'))

    def test_split_sdk_machine_ip_included_if_set_as_callable(self):
        """Tests that the optional header SplitSDKMachineIP is included if set as a callable and
        its value is the result of calling the function"""
        some_split_sdk_machine_ip = mock.MagicMock()
        self.api._split_sdk_machine_ip = some_split_sdk_machine_ip

        headers = self.api._build_headers()

        self.assertNotIn(some_split_sdk_machine_ip.return_value,
                         headers.get('SplitSDKMachineIP'))
コード例 #2
0
class SdkApiPostTests(TestCase, MockUtilsMixin):
    def setUp(self):
        super(SdkApiPostTests, self).setUp()

        self.requests_post_mock = self.patch('splitio.api.requests').post

        self.some_api_key = mock.MagicMock()
        self.some_url = mock.MagicMock()
        self.some_data = mock.MagicMock()

        self.api = SdkApi(self.some_api_key)

        self.build_headers_mock = self.patch_object(self.api, '_build_headers')

    def test_proper_headers_are_used(self):
        """Tests that the request is made with the proper headers"""
        self.api._post(self.some_url, self.some_data)

        self.requests_post_mock.assert_called_once_with(
            mock.ANY,
            json=mock.ANY,
            headers=self.build_headers_mock.return_value,
            timeout=mock.ANY)

    def test_url_parameter_is_used(self):
        """Tests that the request is made with the supplied url"""
        self.api._post(self.some_url, self.some_data)

        self.requests_post_mock.assert_called_once_with(self.some_url,
                                                        json=mock.ANY,
                                                        headers=mock.ANY,
                                                        timeout=mock.ANY)

    def test_data_parameter_is_used(self):
        """Tests that the request is made with the supplied data as json parameter"""
        self.api._post(self.some_url, self.some_data)

        self.requests_post_mock.assert_called_once_with(mock.ANY,
                                                        json=self.some_data,
                                                        headers=mock.ANY,
                                                        timeout=mock.ANY)

    def test_proper_timeout_is_used(self):
        """Tests that the request is made with the proper value for timeout"""
        some_timeout = mock.MagicMock()
        self.api._timeout = some_timeout

        self.api._post(self.some_url, self.some_data)

        self.requests_post_mock.assert_called_once_with(mock.ANY,
                                                        json=mock.ANY,
                                                        headers=mock.ANY,
                                                        timeout=some_timeout)

    def test_status_is_returned(self):
        """Tests that the function returns the the status code of the response"""
        result = self.api._post(self.some_url, self.some_data)

        self.assertEqual(self.requests_post_mock.return_value.status_code,
                         result)

    def test_request_exceptions_are_raised(self):
        """Tests that if requests raises an exception, it is not handled within the call"""
        self.requests_post_mock.side_effect = RequestException()

        with self.assertRaises(RequestException):
            self.api._post(self.some_url, self.some_data)

    def test_request_status_exceptions_are_not_raised(self):
        """Tests that if requests succeeds but its status is not 200 (Ok) an exception is not raised"""
        self.requests_post_mock.return_value.raise_for_status.side_effect = HTTPError(
        )

        try:
            self.api._post(self.some_url, self.some_data)
        except:
            self.assertTrue(False)