mardi 23 juin 2020

What is the best way to create a generic Rule set to perform negative testing against API

I am testing API and I want to perform some negative testing when passing Json model. For example character limit, sending a property field empty, special characters, sending wrong datatype for a property. I was thinking creating an interface like ..IServiceAPI where I can define these rules. Then inherit the interface in the baseclass where I have all the http GET,POST,PUT and Delete methods. Any suggested would be really appreciated. For reference here is my baseclass.

 public class BaseCrudHandler 
    {
       
        public void SetOrGetToken()
        {
            if (token is null)
                token = Authentication.getAccessToken();
            else
                Console.WriteLine($"Token was already set to: {token}");
        }

        //HTTP Methods: GET POST PUT DELETE
        public static IRestResponse HttpGet(string baseURL, dynamic basePath, string token, RestClient client, Dictionary<string, string> queryParam = null, Dictionary<string, string> pathParams = null )
        {
            if (SetBaseURI(baseURL)) //if this is true it will send the request
            {
                RestRequest request = SendRequest(token, queryParam, pathParams);
                return GetResponse("GET", request, client, baseURL, basePath);
            }
            return null;
        }

        public static IRestResponse HttpPost(string baseURL, string basePath, Dictionary<string, string> paramsMap, object obj, string token, RestClient client)
        {
            Dictionary<string, string> pathParams = new Dictionary<string, string>();
            if (SetBaseURI(baseURL))
            {
                RestRequest request = SendRequest(token, paramsMap, pathParams);
                request.AddJsonBody(obj);
                return GetResponse("POST", request, client, baseURL, basePath);
            }
            return null;
        }

        public static IRestResponse HttpPut(string baseURL, string basePath, Dictionary<string, string> paramsMap, object obj, string token, RestClient client)
        {
            Dictionary<string, string> pathParams = new Dictionary<string, string>();
            if (SetBaseURI(baseURL))
            {
                RestRequest request = SendRequest(token, paramsMap, pathParams);
                request.AddJsonBody(obj);
                return GetResponse("PUT", request, client, baseURL, basePath);
            }
            return null;
        }

        public static IRestResponse HttpDelete(string baseURL, string basePath, Dictionary<string, string> paramsMap, string token, RestClient client, object obj = null)
        {
            Dictionary<string, string> pathParams = new Dictionary<string, string>();
            if (SetBaseURI(baseURL))
            {
                RestRequest request = SendRequest(token, paramsMap, pathParams);
                request.AddJsonBody(obj);
                return GetResponse("DELETE", request, client, baseURL, basePath);
            }
            return null;
        }

        private static bool SetBaseURI(string baseURL)
        {
            //if baseURL is Null or Empty check  
            if (string.IsNullOrEmpty(baseURL.ToString()))
                throw new Exception("Please pass the correct base URL...");
            
            try
            {
                RestClient client = new RestClient();
                client.BaseUrl = new Uri(baseURL); //Set the baseUrl
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace + "Some exception occured while seting baseUrl");
                return false;
            }
        }

        private static RestRequest SendRequest(string token, Dictionary<string, string> queryParams, Dictionary<string, string> pathParams)
        {
            RestRequest request = new RestRequest();

            //if token is null no need to add the token. Some request requires token, Some does not
            if (token != null)
                request.AddHeader("Authorization", "Bearer " + token);
            
            //Adding JSON content type as all APIs accept it. 
            request.AddHeader("Content-Type", "application/json; charset=utf-8");

            if (queryParams != null)
            {
                if (queryParams.TryGetValue("", out _))    
                     request.AddJsonBody(new List<string>((queryParams as Dictionary<string, string>).Values)); 
                else
                {
                    foreach (KeyValuePair<string, string> param in queryParams)
                        request.AddQueryParameter(param.Key.ToString(), param.Value.ToString());
                }
            }

            // add path parameter. 
            if (pathParams != null)
            {
                foreach (var param in pathParams)
                    request.AddParameter(param.Key, param.Value, ParameterType.UrlSegment);
            }
            return request;
        }


        private static IRestResponse GetResponse(string httpMethod, RestRequest request, RestClient client, string baseURL, dynamic basePath)
        {
            return ExecuteAPI(httpMethod, request, client, baseURL, basePath);
        }

        private static IRestResponse ExecuteAPI(string httpMethod, RestRequest request, RestClient client, string baseURL, dynamic basePath)
        {

            //if its GET it will call the GET, if its POST it will call the POST...and so on
            //set the resource url to basePath and return the request
            Method method;
            if (!Enum.TryParse(httpMethod, out method))
                throw new ArgumentException("Invalid HTTP method.", nameof(httpMethod));
            
            request.Method = method;
            request.Resource = baseURL + basePath;
            return client.Execute(request);
        }
    
        
    }
}

Aucun commentaire:

Enregistrer un commentaire