def test_send_get_request_includes_given_parameters(self): self.setup_responses(url='https://retdec.com/service/api') conn = APIConnection('https://retdec.com/service/api', 'KEY') conn.send_get_request(params={'key': 'value'}) self.assertEqual(responses.calls[0].request.url, 'https://retdec.com/service/api?key=value')
def test_send_get_request_sends_correct_user_agent_header(self): self.setup_responses(url='https://retdec.com/service/api') conn = APIConnection('https://retdec.com/service/api', 'KEY') conn.send_get_request() self.assertEqual(responses.calls[0].request.headers['User-Agent'], 'retdec-python/' + platform.system())
def test_send_get_request_sends_get_request(self): self.setup_responses(method=responses.GET, url='https://retdec.com/service/api') conn = APIConnection('https://retdec.com/service/api', 'KEY') conn.send_get_request() self.assertEqual(len(responses.calls), 1) self.assertEqual(responses.calls[0].request.method, responses.GET)
def test_send_get_request_sends_request_to_path_when_path_is_nonempty( self): self.setup_responses(url='https://retdec.com/service/api/decompiler') conn = APIConnection('https://retdec.com/service/api', 'KEY') conn.send_get_request(path='/decompiler') self.assertEqual(responses.calls[0].request.url, 'https://retdec.com/service/api/decompiler')
def test_send_get_request_raises_exception_when_there_is_connection_error( self, requests_session): requests_session.side_effect = requests.exceptions.ConnectionError( 'Connection refused.') conn = APIConnection('https://retdec.com/service/api', 'KEY') with self.assertRaises(ConnectionError) as cm: conn.send_get_request() self.assertIn('Connection refused.', str(cm.exception))
def test_send_get_request_sends_correct_user_agent_header(self): self.setup_responses(url='https://retdec.com/service/api') conn = APIConnection('https://retdec.com/service/api', 'KEY') conn.send_get_request() self.assertEqual( responses.calls[0].request.headers['User-Agent'], 'retdec-python/' + platform.system() )
def test_send_get_request_includes_given_parameters(self): self.setup_responses(url='https://retdec.com/service/api') conn = APIConnection('https://retdec.com/service/api', 'KEY') conn.send_get_request(params={'key': 'value'}) self.assertEqual( responses.calls[0].request.url, 'https://retdec.com/service/api?key=value' )
def test_send_get_request_sends_request_to_path_when_path_is_nonempty(self): self.setup_responses(url='https://retdec.com/service/api/decompiler') conn = APIConnection('https://retdec.com/service/api', 'KEY') conn.send_get_request(path='/decompiler') self.assertEqual( responses.calls[0].request.url, 'https://retdec.com/service/api/decompiler' )
def test_send_get_request_raises_exception_when_there_is_connection_error( self, requests_session): requests_session.side_effect = requests.exceptions.ConnectionError( 'Connection refused.' ) conn = APIConnection('https://retdec.com/service/api', 'KEY') with self.assertRaises(ConnectionError) as cm: conn.send_get_request() self.assertIn('Connection refused.', str(cm.exception))
def test_send_get_request_raises_exception_when_authentication_fails(self): self.setup_responses( url='https://retdec.com/service/api', status=401, body='{"code": 401, "message": "failure", "description": "auth failed"}', ) conn = APIConnection('https://retdec.com/service/api', 'KEY') with self.assertRaises(AuthenticationError): conn.send_get_request()
def test_send_get_request_sends_correct_authentication_header(self): self.setup_responses(url='https://retdec.com/service/api') conn = APIConnection('https://retdec.com/service/api', 'KEY') conn.send_get_request() # http://en.wikipedia.org/wiki/Basic_access_authentication#Client_side self.assertEqual( responses.calls[0].request.headers['Authorization'], 'Basic S0VZOg==' # base64-encoded string "KEY:" )
def test_send_get_request_sends_get_request(self): self.setup_responses( method=responses.GET, url='https://retdec.com/service/api' ) conn = APIConnection('https://retdec.com/service/api', 'KEY') conn.send_get_request() self.assertEqual(len(responses.calls), 1) self.assertEqual(responses.calls[0].request.method, responses.GET)
def test_send_get_request_raises_exception_when_authentication_fails(self): self.setup_responses( url='https://retdec.com/service/api', status=401, body= '{"code": 401, "message": "failure", "description": "auth failed"}', ) conn = APIConnection('https://retdec.com/service/api', 'KEY') with self.assertRaises(AuthenticationError): conn.send_get_request()
def test_send_get_request_raises_exception_when_api_returns_unknown_error( self): self.setup_responses( url='https://retdec.com/service/api', status=408, body=('{"code": 408, "message": "Request Timeout", ' '"description": "The request timeouted."}'), ) conn = APIConnection('https://retdec.com/service/api', 'KEY') with self.assertRaises(UnknownAPIError) as cm: conn.send_get_request() self.assertEqual(cm.exception.code, 408) self.assertEqual(cm.exception.message, 'Request Timeout') self.assertEqual(cm.exception.description, 'The request timeouted.')
def test_send_get_request_raises_exception_when_api_returns_unknown_error(self): self.setup_responses( url='https://retdec.com/service/api', status=408, body=( '{"code": 408, "message": "Request Timeout", ' '"description": "The request timeouted."}' ), ) conn = APIConnection('https://retdec.com/service/api', 'KEY') with self.assertRaises(UnknownAPIError) as cm: conn.send_get_request() self.assertEqual(cm.exception.code, 408) self.assertEqual(cm.exception.message, 'Request Timeout') self.assertEqual(cm.exception.description, 'The request timeouted.')
def test_send_get_request_returns_json_body_when_request_succeeds(self): self.setup_responses(url='https://retdec.com/service/api', body='{"key": "value"}') conn = APIConnection('https://retdec.com/service/api', 'KEY') response = conn.send_get_request() self.assertEqual(response, {'key': 'value'})
def test_send_get_request_returns_json_body_when_request_succeeds(self): self.setup_responses( url='https://retdec.com/service/api', body='{"key": "value"}' ) conn = APIConnection('https://retdec.com/service/api', 'KEY') response = conn.send_get_request() self.assertEqual(response, {'key': 'value'})