def test_fetch_not_modified_etag(self):
        with mock.patch.object(requests, 'get') as request_get:
            etag = 'test'
            test_json = {"test": "json"}
            fetcher = ConfigFetcher(sdk_key='', mode='m')

            response_mock = Mock()
            response_mock.json.return_value = test_json
            response_mock.status_code = 200
            response_mock.headers = {'Etag': etag}

            request_get.return_value = response_mock
            fetch_response = fetcher.get_configuration_json()
            self.assertTrue(fetch_response.is_fetched())
            self.assertEqual(test_json, fetch_response.json())

            response_not_modified_mock = Mock()
            response_not_modified_mock.json.return_value = {}
            response_not_modified_mock.status_code = 304
            response_not_modified_mock.headers = {'ETag': etag}

            request_get.return_value = response_not_modified_mock
            fetch_response = fetcher.get_configuration_json()
            self.assertFalse(fetch_response.is_fetched())

            args, kwargs = request_get.call_args
            request_headers = kwargs.get('headers')
            self.assertEqual(request_headers.get('If-None-Match'), etag)
    def test_sdk_redirect_loop(self, mock_get):
        # In this case
        # the first invocation should call https://cdn-global.configcat.com
        # with an immediate redirect to https://cdn-eu.configcat.com
        # with an immediate redirect to https://cdn-global.configcat.com
        # the second invocation should call https://cdn-eu.configcat.com
        # with an immediate redirect to https://cdn-global.configcat.com
        # with an immediate redirect to https://cdn-eu.configcat.com

        fetcher = ConfigFetcher(sdk_key='',
                                mode='m',
                                data_governance=DataGovernance.Global)

        # First fetch
        fetch_response = fetcher.get_configuration_json()
        self.assertTrue(fetch_response.is_fetched())
        self.assertEqual(test_json, fetch_response.json().get('f'))
        self.assertEqual(len(mock_get.call_args_list), 3)
        self.assertEqual(call_to_global, mock_get.call_args_list[0])
        self.assertEqual(call_to_eu, mock_get.call_args_list[1])
        self.assertEqual(call_to_global, mock_get.call_args_list[2])

        # Second fetch
        fetch_response = fetcher.get_configuration_json()
        self.assertTrue(fetch_response.is_fetched())
        self.assertEqual(test_json, fetch_response.json().get('f'))
        self.assertEqual(len(mock_get.call_args_list), 6)
        self.assertEqual(call_to_global, mock_get.call_args_list[0])
        self.assertEqual(call_to_eu, mock_get.call_args_list[1])
        self.assertEqual(call_to_global, mock_get.call_args_list[2])
        self.assertEqual(call_to_eu, mock_get.call_args_list[3])
        self.assertEqual(call_to_global, mock_get.call_args_list[4])
        self.assertEqual(call_to_eu, mock_get.call_args_list[5])
    def test_sdk_base_url_forced(self, mock_get):
        # In this case
        # the first invocation should call https://cdn-eu.configcat.com
        # with an immediate redirect to https://forced.configcat.com
        # and the second should call https://forced.configcat.com

        fetcher = ConfigFetcher(sdk_key='',
                                mode='m',
                                data_governance=DataGovernance.Global,
                                base_url='https://custom.configcat.com')

        # First fetch
        fetch_response = fetcher.get_configuration_json()
        self.assertTrue(fetch_response.is_fetched())
        self.assertEqual(test_json, fetch_response.json().get('f'))
        self.assertEqual(len(mock_get.call_args_list), 2)
        self.assertEqual(call_to_custom, mock_get.call_args_list[0])
        self.assertEqual(call_to_forced, mock_get.call_args_list[1])
        self.assertNotIn(call_to_eu, mock_get.call_args_list)
        self.assertNotIn(call_to_global, mock_get.call_args_list)

        # Second fetch
        fetch_response = fetcher.get_configuration_json()
        self.assertTrue(fetch_response.is_fetched())
        self.assertEqual(test_json, fetch_response.json().get('f'))
        self.assertEqual(len(mock_get.call_args_list), 3)
        self.assertEqual(call_to_custom, mock_get.call_args_list[0])
        self.assertEqual(call_to_forced, mock_get.call_args_list[1])
        self.assertEqual(call_to_forced, mock_get.call_args_list[2])
        self.assertNotIn(call_to_eu, mock_get.call_args_list)
        self.assertNotIn(call_to_global, mock_get.call_args_list)
    def test_sdk_global_organization_global(self, mock_get):
        # In this case
        # the first invocation should call https://cdn-global.configcat.com
        # and the second should call https://cdn-global.configcat.com
        # without force redirects

        fetcher = ConfigFetcher(sdk_key='',
                                mode='m',
                                data_governance=DataGovernance.Global)

        # First fetch
        fetch_response = fetcher.get_configuration_json()
        self.assertTrue(fetch_response.is_fetched())
        self.assertEqual(test_json, fetch_response.json().get('f'))
        self.assertEqual(len(mock_get.call_args_list), 1)
        self.assertEqual(call_to_global, mock_get.call_args_list[0])
        self.assertNotIn(call_to_eu, mock_get.call_args_list)

        # Second fetch
        fetch_response = fetcher.get_configuration_json()
        self.assertTrue(fetch_response.is_fetched())
        self.assertEqual(test_json, fetch_response.json().get('f'))
        self.assertEqual(len(mock_get.call_args_list), 2)
        self.assertEqual(call_to_global, mock_get.call_args_list[0])
        self.assertEqual(call_to_global, mock_get.call_args_list[1])
        self.assertNotIn(call_to_eu, mock_get.call_args_list)
Beispiel #5
0
 def test_server_side_etag(self):
     fetcher = ConfigFetcher(
         sdk_key='PKDVCLf-Hq-h-kCzMp-L7Q/HhOWfwVtZ0mb30i9wi17GQ',
         mode='m',
         base_url='https://cdn-li-fra-1.configcat.com')
     fetch_response = fetcher.get_configuration_json()
     self.assertTrue(fetch_response.is_fetched())
     self.assertFalse(fetch_response.is_not_modified())
     fetch_response = fetcher.get_configuration_json()
     self.assertFalse(fetch_response.is_fetched())
     self.assertTrue(fetch_response.is_not_modified())
 def test_simple_fetch_success(self):
     with mock.patch.object(requests, 'get') as request_get:
         test_json = {"test": "json"}
         response_mock = Mock()
         request_get.return_value = response_mock
         response_mock.json.return_value = test_json
         response_mock.status_code = 200
         response_mock.headers = {}
         fetcher = ConfigFetcher(sdk_key='', mode='m')
         fetch_response = fetcher.get_configuration_json()
         self.assertTrue(fetch_response.is_fetched())
         self.assertEqual(test_json, fetch_response.json())