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_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_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_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_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_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_get_file_sets_no_name_when_response_does_not_provide_header(self): self.setup_responses(method=responses.GET, url='https://retdec.com/service/api', body='data', stream=True) conn = APIConnection('https://retdec.com/service/api', 'KEY') file = conn.get_file() self.assertIsNone(file.name)
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_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_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_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_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_get_file_sets_no_name_when_response_header_does_not_contain_name( self): self.setup_responses(method=responses.GET, url='https://retdec.com/service/api', body='data', headers={'Content-Disposition': 'attachment'}, stream=True) conn = APIConnection('https://retdec.com/service/api', 'KEY') file = conn.get_file() self.assertIsNone(file.name)
def test_get_file_sets_no_name_when_response_does_not_provide_header(self): self.setup_responses( method=responses.GET, url='https://retdec.com/service/api', body='data', stream=True ) conn = APIConnection('https://retdec.com/service/api', 'KEY') file = conn.get_file() self.assertIsNone(file.name)
def test_get_file_sends_get_request(self): self.setup_responses( method=responses.GET, url='https://retdec.com/service/api', adding_headers={'Content-Disposition': 'filename=test.c'}, stream=True) conn = APIConnection('https://retdec.com/service/api', 'KEY') conn.get_file() self.assertEqual(len(responses.calls), 1) self.assertEqual(responses.calls[0].request.method, responses.GET)
def test_send_post_request_includes_given_files(self): self.setup_responses(method=responses.POST, url='https://retdec.com/service/api') conn = APIConnection('https://retdec.com/service/api', 'KEY') files = {'input': ('test.c', io.StringIO('main()'))} conn.send_post_request(files=files) body = str(responses.calls[0].request.body) self.assertIn( 'Content-Disposition: form-data; name="input"; filename="test.c"', body) self.assertIn('main()', body)
def test_get_file_returns_file_with_correct_name_when_header_has_quotes( self): self.setup_responses( method=responses.GET, url='https://retdec.com/service/api', body='data', headers={'Content-Disposition': 'attachment; filename="test.c"'}, stream=True) conn = APIConnection('https://retdec.com/service/api', 'KEY') file = conn.get_file() self.assertEqual(file.name, 'test.c')
def test_get_file_sets_no_name_when_response_header_does_not_contain_name(self): self.setup_responses( method=responses.GET, url='https://retdec.com/service/api', body='data', adding_headers={'Content-Disposition': 'attachment'}, stream=True ) conn = APIConnection('https://retdec.com/service/api', 'KEY') file = conn.get_file() self.assertIsNone(file.name)
def test_get_file_sends_get_request(self): self.setup_responses( method=responses.GET, url='https://retdec.com/service/api', adding_headers={'Content-Disposition': 'filename=test.c'}, stream=True ) conn = APIConnection('https://retdec.com/service/api', 'KEY') conn.get_file() self.assertEqual(len(responses.calls), 1) self.assertEqual(responses.calls[0].request.method, responses.GET)
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_get_file_returns_file_with_correct_name_when_header_has_quotes(self): self.setup_responses( method=responses.GET, url='https://retdec.com/service/api', body='data', adding_headers={ 'Content-Disposition': 'attachment; filename="test.c"' }, stream=True ) conn = APIConnection('https://retdec.com/service/api', 'KEY') file = conn.get_file() self.assertEqual(file.name, 'test.c')
def test_repr_returns_correct_value(self): conn = APIConnection('https://retdec.com/service/api', 'KEY') self.assertEqual( repr(conn), "<retdec.conn.APIConnection base_url='https://retdec.com/service/api'>" )
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_post_request_includes_given_files(self): self.setup_responses( method=responses.POST, url='https://retdec.com/service/api' ) conn = APIConnection('https://retdec.com/service/api', 'KEY') files = {'input': ('test.c', io.StringIO('main()'))} conn.send_post_request(files=files) body = str(responses.calls[0].request.body) self.assertIn( 'Content-Disposition: form-data; name="input"; filename="test.c"', body ) self.assertIn('main()', body)
def _create_new_api_connection(self, path): """Creates a new API connection from the given path. :param str path: Path that is appended after the API URL. """ return APIConnection(self.api_url + path, self.api_key)