async def test(): conn = await ConnectionManager.create( base_url='http://127.0.0.1:4010', credentials=Credential(key='', secret='')) self.assertIsNone(conn.connector) self.assertIsNone(conn.rate_limiter) self.assertIsInstance(conn.token_manager, TokenManager) await conn.close() conn = await ConnectionManager.create( base_url='http://127.0.0.1:4010', credentials=Credential(key='', secret=''), max_connections=20) self.assertIsInstance(conn.connector, aiohttp.TCPConnector) self.assertIsNone(conn.rate_limiter) self.assertIsInstance(conn.token_manager, TokenManager) await conn.close() conn = await ConnectionManager.create( base_url='http://127.0.0.1:4010', credentials=Credential(key='', secret=''), rate_limit_token_regen_rate=10) self.assertIsInstance(conn.rate_limiter, RateLimiter) self.assertIsInstance(conn.token_manager, TokenManager) await conn.close()
async def test(): conn = await ConnectionManager.create( base_url='http://127.0.0.1:4010', credentials=Credential(key='', secret='')) try: await conn.get(endpoint='/') except TreillageHTTPException: pass mock_session.return_value.get.assert_called_with( url='http://127.0.0.1:4010/', params=None, headers={ 'x-fv-sessionid': 'mock_refresh_token', 'Authorization': 'Bearer mock_access_token' }) try: await conn.get( endpoint='/test2', params={'firstName': 'Joe'}, headers={"Accept-Encoding": "gzip, deflate, br"}) except TreillageHTTPException: pass mock_session.return_value.get.assert_called_with( url='http://127.0.0.1:4010/test2', params={'firstName': 'Joe'}, headers={ 'Accept-Encoding': 'gzip, deflate, br', 'x-fv-sessionid': 'mock_refresh_token', 'Authorization': 'Bearer mock_access_token' }) await conn.close()
def setUp(self) -> None: self.base_url = 'http://127.0.0.1' self.credentials = Credential(key='', secret='') patcher = patch('treillage.token_manager.ClientSession.post') mock_post = patcher.start() mock_response = mock_post.return_value.__aenter__.return_value mock_response.json.side_effect = mock_json mock_response.status = 200 self.addCleanup(patcher.stop)
def get_credentials(test_cred_data): creds_file = 'creds.yml' with open(creds_file, 'w') as out: for k in test_cred_data: out.write(f"{k}: {test_cred_data[k]}\n") try: credentials = Credential.get_credentials(creds_file) return credentials finally: os.remove(creds_file)
async def test(): conn = await ConnectionManager.create( base_url='http://127.0.0.1:4010', rate_limit_token_regen_rate=10, credentials=Credential(key='', secret='')) response = MockResponse(200) with self.assertRaises(TreillageHTTPException): await conn._ConnectionManager__handle_response(response, 500) self.assertFalse( mock_rate_limiter.return_value.last_try_success.called) await conn.close()
async def test(): conn = await ConnectionManager.create( base_url='http://127.0.0.1:4010', credentials=Credential(key='', secret='')) i = {'index': -1} try: resp = await get_response(i, conn) except TreillageRateLimitException as ex: assert False self.assertEqual(i['index'], 3) self.assertEqual(resp, {'items': []}) await conn.close()
async def test(): conn = await ConnectionManager.create( base_url='http://127.0.0.1:4010', rate_limit_token_regen_rate=10, credentials=Credential(key='', secret='')) response = MockResponse(204) self.assertEqual( await conn._ConnectionManager__handle_response(response, 204), response.data) mock_rate_limiter.return_value.\ last_try_success.assert_called_with(True) await conn.close()
async def test(): conn = await ConnectionManager.create( base_url='http://127.0.0.1:4010', rate_limit_token_regen_rate=10, credentials=Credential(key='', secret='')) # test 429 Rate Limit error with self.assertRaises(TreillageRateLimitException): response = MockResponse(429) await conn._ConnectionManager__handle_response(response, 200) mock_rate_limiter.return_value.\ last_try_success.assert_called_with(False) await conn.close()
async def test(): conn = await ConnectionManager.create( base_url='http://127.0.0.1:4010', credentials=Credential(key='', secret='')) try: await conn.delete(endpoint='/delete') except TreillageHTTPException: pass mock_session.return_value.delete.assert_called_with( url='http://127.0.0.1:4010/delete', headers={ 'x-fv-sessionid': 'mock_refresh_token', 'Authorization': 'Bearer mock_access_token' }) await conn.close()
async def test(): conn = await ConnectionManager.create( base_url='http://127.0.0.1:4010', credentials=Credential(key='', secret='')) headers = conn._ConnectionManager__setup_headers({ "Accept-Encoding": "gzip, deflate, br", "Connection": "keep-alive" }) self.assertEqual(headers["x-fv-sessionid"], conn.token_manager.refresh_token) self.assertEqual(headers["Authorization"], f"Bearer {conn.token_manager.access_token}") self.assertEqual(headers["Accept-Encoding"], "gzip, deflate, br") self.assertEqual(headers["Connection"], "keep-alive") await conn.close()
async def test(): conn = await ConnectionManager.create( base_url='http://127.0.0.1:4010', credentials=Credential(key='', secret='')) try: await conn.put(endpoint='/put', body={ 'firstName': 'John', 'lastName': 'Doe' }) except TreillageHTTPException: pass mock_session.return_value.put.assert_called_with( url='http://127.0.0.1:4010/put', json={ 'firstName': 'John', 'lastName': 'Doe' }, headers={ 'x-fv-sessionid': 'mock_refresh_token', 'Authorization': 'Bearer mock_access_token' }) await conn.close()
async def test(): conn = await ConnectionManager.create( base_url='http://127.0.0.1:4010', credentials=Credential(key='', secret='')) response = MockResponse(200) self.assertEqual( await conn._ConnectionManager__handle_response(response, 200), response.data) # test 500 server error with self.assertRaises(TreillageHTTPException): response = MockResponse(500) await conn._ConnectionManager__handle_response(response, 200) # test 429 Rate Limit error with self.assertRaises(TreillageRateLimitException): response = MockResponse(429) await conn._ConnectionManager__handle_response(response, 200) # test unexpected 2xx success code with self.assertRaises(TreillageHTTPException): response = MockResponse(200) await conn._ConnectionManager__handle_response(response, 204) await conn.close()