Esempio n. 1
0
 def test_with_api_key(self):
     apikey = 'xyz'
     instance = HttpRequestBuilder('test.com', apikey, self.test_config)
     self.assertIsNotNone(instance.api_key)
     self.assertEqual(dict(), instance.params)
     instance.with_api_key()
     self.assertTrue('APPID' in instance.params.keys())
     self.assertEqual(apikey, instance.params.get('APPID'))
Esempio n. 2
0
 def test_with_language(self):
     lang = 'fr'
     config = self.test_config.copy()
     config['language'] = lang
     instance = HttpRequestBuilder('test.com', 'apikey', config)
     self.assertEqual(dict(), instance.params)
     instance.with_language()
     self.assertTrue('lang' in instance.params.keys())
     self.assertEqual(lang, instance.params.get('lang'))
Esempio n. 3
0
 def test__set_proxies(self):
     with_proxies = self.test_config.copy()
     with_proxies['connection']['use_proxy'] = True
     instance = HttpRequestBuilder('test.com', 'apikey', with_proxies)
     self.assertEqual(with_proxies['proxies'], instance.proxies)
     without_proxies = self.test_config.copy()
     without_proxies['connection']['use_proxy'] = False
     instance = HttpRequestBuilder('test.com', 'apikey', without_proxies)
     self.assertEqual(dict(), instance.proxies)
Esempio n. 4
0
 def test__set_schema(self):
     with_ssl = self.test_config.copy()
     with_ssl['connection']['use_ssl'] = True
     instance = HttpRequestBuilder('test.com', 'apikey', with_ssl)
     self.assertEqual('https', instance.schema)
     without_ssl = self.test_config.copy()
     without_ssl['connection']['use_ssl'] = False
     instance = HttpRequestBuilder('test.com', 'apikey', without_ssl)
     self.assertEqual('http', instance.schema)
 def test_instantiation_with_wrong_params(self):
     with self.assertRaises(AssertionError):
         HttpRequestBuilder(None, 'apikey', self.test_config)
     with self.assertRaises(AssertionError):
         HttpRequestBuilder('test.com', None, self.test_config)
     with self.assertRaises(AssertionError):
         HttpRequestBuilder('test.com', 'apikey', None)
     with self.assertRaises(AssertionError):
         HttpRequestBuilder('test.com', 'apikey', self.test_config, has_subdomains=1234)
 def test_fluidity_of_interface(self):
     instance = HttpRequestBuilder('test.com/root', 'apikey', self.test_config)
     self.assertTrue(isinstance(instance.with_path('/path'), HttpRequestBuilder))
     self.assertTrue(isinstance(instance.with_headers({}), HttpRequestBuilder))
     self.assertTrue(isinstance(instance.with_query_params({}), HttpRequestBuilder))
     self.assertTrue(isinstance(instance.with_header('key', 'value'), HttpRequestBuilder))
     self.assertTrue(isinstance(instance.with_api_key(), HttpRequestBuilder))
     self.assertTrue(isinstance(instance.with_language(), HttpRequestBuilder))
 def test_with_header(self):
     instance = HttpRequestBuilder('test.com', 'apikey', self.test_config)
     self.assertEqual(dict(), instance.headers)
     hkey = 'key'
     hvalue = 'value'
     instance.with_header(hkey, hvalue)
     self.assertEqual({hkey: hvalue}, instance.headers)
     with self.assertRaises(AssertionError):
         instance.with_header(123, 'value')
     with self.assertRaises(ValueError):
         instance.with_header('key', bytes()) # any non-serializable value is OK here
Esempio n. 8
0
 def test_with_headers(self):
     headers = dict(a=1, b=2)
     instance = HttpRequestBuilder('test.com', 'apikey', self.test_config)
     self.assertEqual(dict(), instance.headers)
     instance.with_headers(headers)
     self.assertEqual(headers, instance.headers)
     more_headers = dict(c=3, d=4)
     headers.update(more_headers)
     instance.with_headers(more_headers)
     self.assertEqual(headers, instance.headers)
     with self.assertRaises(AssertionError):
         instance.with_headers('not-a-dict')
Esempio n. 9
0
 def test_with_query_params(self):
     params = dict(a=1, b=2)
     instance = HttpRequestBuilder('test.com', 'apikey', self.test_config)
     self.assertEqual(dict(), instance.params)
     instance.with_query_params(params)
     self.assertEqual(params, instance.params)
     more_params = dict(c=3, d=4)
     params.update(more_params)
     instance.with_query_params(more_params)
     self.assertEqual(params, instance.params)
     with self.assertRaises(AssertionError):
         instance.with_query_params('not-a-dict')
Esempio n. 10
0
 def test_build_without_subdomains(self):
     apikey = 'apikey'
     lang = 'ru'
     proxies = {'https': 'https://*****:*****@proxy:3128'}
     config = {
         'subscription_type': SubscriptionTypeEnum.PROFESSIONAL,
         'language': lang,
         'connection': {
             'use_ssl': True,
             'use_proxy': True
         },
         'proxies': proxies
     }
     expected_url = 'https://test.com/root/path-to-resource.json'
     expected_params = {'q': 'my-query', 'APPID': apikey, 'lang': lang}
     expected_headers = {
         'accept-encoding': 'gzip, deflate, br',
         'accept-language': 'it,en;q=0.9',
         'cache-control': 'no-cache'
     }
     expected_proxies = proxies
     instance = HttpRequestBuilder('test.com/root',
                                   apikey,
                                   config,
                                   has_subdomains=False)
     instance\
         .with_api_key()\
         .with_language()\
         .with_path('path-to-resource.json')\
         .with_query_params({'q': 'my-query'})\
         .with_headers({
             'accept-encoding': 'gzip, deflate, br',
             'accept-language': 'it,en;q=0.9'
         })\
         .with_header('cache-control', 'no-cache')
     result_url, result_params, result_headers, result_proxies = instance.build(
     )
     self.assertEqual(expected_url, result_url)
     self.assertEqual(expected_params, result_params)
     self.assertEqual(expected_headers, result_headers)
     self.assertEqual(expected_proxies, result_proxies)
Esempio n. 11
0
 def test_with_path(self):
     instance = HttpRequestBuilder('test.com', 'apikey', self.test_config)
     self.assertEqual(None, instance.path)
     path = 'test/path'
     instance.with_path(path)
     self.assertEqual(path, instance.path)
     with self.assertRaises(AssertionError):
         instance.with_path(1234)
    def test__set_subdomain(self):
        # in case of a root URL with subdomains
        for st in SubscriptionTypeEnum.items():
            config = self.test_config.copy()
            config['subscription_type'] = st
            instance = HttpRequestBuilder('test.com', 'apikey', config)
            self.assertEqual(st.subdomain, instance.subdomain)

        # in case of a root URL without subdomains
        instance = HttpRequestBuilder('nosubdomains.com', 'apikey', self.test_config, has_subdomains=False)
        self.assertIsNone(instance.subdomain)
        instance._set_subdomain()
        self.assertIsNone(instance.subdomain)
Esempio n. 13
0
 def test_repr(self):
     print(HttpRequestBuilder('/test', 'apikey', self.test_config))