def test_api_request_with_bad_request_with_error_detail(self): connection = Connection() connection.request = MagicMock() error = requests.exceptions.HTTPError() res = requests.models.Response() res.url = 'https://xxxxxxxx.com' res.status_code = 400 api_response = { 'error': 'test', 'error_description': 'test error description', 'error_detail': { 'source_data': ['unknown field'] } } res._content = json.dumps(api_response).encode('utf-8') error.response = res connection.request.side_effect = error method = 'GET' path = '/dummy' with self.assertRaises(BadRequest) as e: connection.api_request(method, path, headers=None) self.assertEqual( str(e.exception), '<BadRequest "test": test error description' ' (400 from https://xxxxxxxx.com)' ', {\'source_data\': [\'unknown field\']}>')
def test_api_request(self): Connection.BASE_URL = 'http://localhost:8080' connection = Connection() connection.request = MagicMock() method = 'GET' path = '/dummy' connection.api_request(method, path, headers=None) connection.request.assert_called_with( method, 'http://localhost:8080{}'.format(path), data=None, headers={'User-Agent': 'abeja-platform-sdk/{}'.format(VERSION)}, json=None, params=None)
def test_api_request_error_with_none_json_response(self): connection = Connection() connection.request = MagicMock() error = requests.exceptions.HTTPError() res = requests.models.Response() res.status_code = 500 res._content = 'raw text'.encode('utf-8') error.response = res connection.request.side_effect = error method = 'GET' path = '/dummy' with self.assertRaises(InternalServerError) as e: connection.api_request(method, path, headers=None) self.assertEqual( str(e.exception), '<InternalServerError "Internal Server Error": raw text>')
def test_api_request_error_with_undefined_error_code(self): connection = Connection() connection.request = MagicMock() error = requests.exceptions.HTTPError() res = requests.models.Response() res.status_code = 501 api_response = {'error_description': 'test error description'} res._content = json.dumps(api_response).encode('utf-8') error.response = res connection.request.side_effect = error method = 'GET' path = '/dummy' with self.assertRaises(HttpError) as e: connection.api_request(method, path, headers=None) self.assertEqual( str(e.exception), '<HttpError "Not Implemented": test error description>')