public void testgetinJson_deseraialize() { IRestClient restClient = new RestClient(); IRestRequest restRequest = new RestRequest(url); restRequest.AddHeader("Accept", "application/json"); //IRestResponse restResponse = restClient.Get(restRequest); //IRestResponse<List<JsonRootObject>> restResponse = restClient.Get<List<JsonRootObject>>(restRequest); IRestResponse <List <JsonRootObject> > restResponse = restClient.Get <List <JsonRootObject> >(restRequest); if (restResponse.IsSuccessful) { Console.WriteLine(restResponse.StatusCode); Assert.AreEqual(200, (int)restResponse.StatusCode); Console.WriteLine(restResponse.Content); Console.WriteLine(restResponse.Data.Count); List <JsonRootObject> jsondata = restResponse.Data; JsonRootObject data = jsondata.Find((x) => { return(x.Id == 3); }); Assert.IsTrue(data.Features.Feature.Contains("1 TB is added"), "Such entry does not exists"); } else { Console.WriteLine(restResponse.IsSuccessful); Console.WriteLine(restResponse.ErrorMessage); Console.WriteLine(restResponse.ErrorException); } }
//[TestMethodWithReport] public void TestGetWithJson_Deserialize() { IRestClient restClient = new RestClient(); IRestRequest restRequest = new RestRequest(getUrl); restRequest.AddHeader("Accept", "application/json"); IRestResponse <List <JsonRootObject> > restResponse = restClient.Get <List <JsonRootObject> >(restRequest); if (restResponse.IsSuccessful) { Console.WriteLine("Status Code " + restResponse.StatusCode); Assert.AreEqual(200, (int)restResponse.StatusCode); Console.WriteLine("Size of List " + restResponse.Data.Count); List <JsonRootObject> data = restResponse.Data; JsonRootObject jsonRootObject = data.Find((x) => { return(x.Id == 1); }); Assert.AreEqual("Alienware M17", jsonRootObject?.LaptopName); Assert.IsTrue(jsonRootObject.Features.Feature.Contains("Windows 10 Home 64 - bit English"), "Element is not Present"); } else { Console.WriteLine("Error Msg " + restResponse.ErrorMessage); Console.WriteLine("Stack Trace " + restResponse.ErrorException); } }
public void SecurePostEndPointTestusingPostHelper() { string postUrl = "http://localhost:8080/laptop-bag/webapi/secure/add"; int id = random.Next(1001); string jsonData = "{" + "\"BrandName\": \"Animalware\"," + "\"Features\": {" + "\"Feature\": [" + "\"8th Generation Intel® Core™ i5-8300H\"," + "\"Windows 10 Home 64-bit English\"," + "\"NVIDIA® GeForce® GTX 1660 Ti 6GB GDDR6\"," + "\"8GB, 2x4GB, DDR4, 2666MHz\"" + "]" + "}," + "\"Id\":" + id + "," + "\"LaptopName\": \"Alienware M17\"" + "}"; string authHeader = "Basic " + Base64AuthenticationHelper.GetBase64String("admin", "welcome"); Dictionary <string, string> httpHeader = new Dictionary <string, string>(); httpHeader.Add("Accept", "application/json"); httpHeader.Add("Authorization", authHeader); HttpContent httpContent = new StringContent(jsonData, Encoding.UTF8, jsonMediaType); RestResponse restResponse = HttpClientHelper.PerformPostRequest(postUrl, httpContent, httpHeader); Console.WriteLine(restResponse.ToString()); JsonRootObject jsonRootObject = JsonConvert.DeserializeObject <JsonRootObject>(restResponse.ResponseData); Assert.AreEqual("ware M17", jsonRootObject.LaptopName); }
public void TestGetWithJsonDeserialize() { IRestClient restClient = new RestClient(); IRestRequest restRequest = new RestRequest(getUrl); restRequest.AddHeader("Accept", "application/json"); IRestResponse <List <JsonRootObject> > restResponse = restClient.Get <List <JsonRootObject> >(restRequest); if (restResponse.IsSuccessful) { Assert.Equal(200, (int)restResponse.StatusCode); // restResponse.Data retorna o objeto depois da deserialização List <JsonRootObject> data = restResponse.Data; //expressao lambda para filtrar a lista e pegar o objeto com o id igual a 1 JsonRootObject jsonRootObject = data.Find((x) => { return(x.Id == 1); }); Assert.Equal("Alienware M17", jsonRootObject.LaptopName); Assert.Contains("8th Generation Intel® Core™ i5-8300H", jsonRootObject.Features.Feature); } else { output.WriteLine("Error msg: " + restResponse.ErrorMessage); output.WriteLine("Stack Trace: " + restResponse.ErrorException); } }
public void testPutUsingJSON() { string contentjson = "{" + "\"BrandName\": \"Alienware\"," + "\"Features\": {" + "\"Feature\": [" + "\"8th Generation Intel® Core™ i5-8300H\"," + "\"Windows 10 Home 64-bit English\"," + "\"NVIDIA® GeForce® GTX 1660 Ti 6GB GDDR6\"," + "\"8GB, 2x4GB, DDR4, 2666MHz\"" + "]" + "}," + "\"Id\": " + id + "," + "\"LaptopName\": \"Alienware M16\"" + "}"; Dictionary <string, string> header = new Dictionary <string, string> { { "Accept", "application/json" } }; restResponse = HttpClientHelper.PerformPostRequest(posturl, contentjson, jsondataformat, header); Assert.AreEqual(200, restResponse.StatusCode); contentjson = "{" + "\"BrandName\": \"Alienware\"," + "\"Features\": {" + "\"Feature\": [" + "\"8th Generation Intel® Core™ i5-8300H\"," + "\"Windows 10 Home 64-bit English\"," + "\"NVIDIA® GeForce® GTX 1660 Ti 6GB GDDR6\"," + "\"8GB, 2x4GB, DDR4, 2666MHz\"," + "\"1 TB SSD added\"" + "]" + "}," + "\"Id\": " + id + "," + "\"LaptopName\": \"Alienware M16\"" + "}"; using (HttpClient httpClient = new HttpClient()) { HttpContent httpContent = new StringContent(contentjson, Encoding.UTF8, jsondataformat); Task <HttpResponseMessage> httpResponseMessage = httpClient.PutAsync(puturl, httpContent); restResponse = new RestResponse((int)httpResponseMessage.Result.StatusCode, httpResponseMessage.Result.Content.ReadAsStringAsync().Result); Assert.AreEqual(200, restResponse.StatusCode); } restResponse = HttpClientHelper.PerformGetRequest(geturl + id, header); Assert.AreEqual(200, restResponse.StatusCode); //Laptop xmlresponsedata = ResponseDataHelper.DeserializeXMLResponse<Laptop>(restResponse.ResponseContent); ////Console.WriteLine(xmlresponsedata.Features.Feature.ToString()); //Assert.IsTrue(xmlresponsedata.Features.Feature.Contains("1 TB is added"), "Failed to add data"); JsonRootObject JSONresponsedata = ResponseDataHelper.DeserializeJSONResponse <JsonRootObject>(restResponse.ResponseContent); Console.WriteLine(JSONresponsedata.Features.Feature); Assert.IsTrue(JSONresponsedata.Features.Feature.Contains("1 TB SSD added")); }
private int AddGiveaways(JsonRootObject json) { var count = 0; if (json != null) { foreach (var giveaway in json.Giveaways) { var lot = new GameMinerGiveaway(); if (giveaway.Golden && giveaway.Price != 0) { break; } if (lot.Price > Points) { break; } if (lot.Price >= JoinPointsLimit) { break; } if (lot.Price > PointsReserv) { break; } lot.Name = giveaway.Game.Name; lot.Id = giveaway.Code; lot.IsRegular = giveaway.Sandbox == null; lot.IsSandbox = giveaway.Sandbox != null; lot.IsGolden = giveaway.Golden; lot.Page = json.Page; lot.Price = giveaway.Price; if (giveaway.RegionlockTypeId != null) { lot.Region = giveaway.RegionlockTypeId; } if (giveaway.Game.Url != "javascript:void(0);") { lot.StoreId = giveaway.Game.Url.Split('/')[4]; } else { break; } Giveaways?.Add(lot); count++; } } return(count); }
public void TestPutUsingJsonData() { int id = random.Next(1000); string jsonData = "{" + "\"BrandName\": \"Alienware\"," + "\"Features\": {" + "\"Feature\": [" + "\"8th Generation Intel® Core™ i5-8300H\"," + "\"Windows 10 Home 64-bit English\"," + "\"NVIDIA® GeForce® GTX 1660 Ti 6GB GDDR6\"," + "\"8GB, 2x4GB, DDR4, 2666MHz\"" + "]" + "}," + "\"Id\": " + id + "," + "\"LaptopName\": \"Alienware M17\"" + "}"; Dictionary <string, string> headers = new Dictionary <string, string>() { { "Accept", "application/json" } }; restResponse = HttpClientHelper.PerformPostRequest(postUrl, jsonData, jsonMediaType, headers); Assert.AreEqual(200, restResponse.StatusCode); jsonData = "{" + "\"BrandName\": \"Alienware\"," + "\"Features\": {" + "\"Feature\": [" + "\"8th Generation Intel® Core™ i5-8300H\"," + "\"Windows 10 Home 64-bit English\"," + "\"NVIDIA® GeForce® GTX 1660 Ti 6GB GDDR6\"," + "\"1 TB of SSD\"," + "\"8GB, 2x4GB, DDR4, 2666MHz\"" + "]" + "}," + "\"Id\": " + id + "," + "\"LaptopName\": \"Alienware M17\"" + "}"; using (HttpClient httpClient = new HttpClient()) { HttpContent httpContent = new StringContent(jsonData, Encoding.UTF8, jsonMediaType); Task <HttpResponseMessage> httpResponseMessage = httpClient.PutAsync(putUrl, httpContent); restResponse = new RestResponse((int)httpResponseMessage.Result.StatusCode, httpResponseMessage.Result.Content.ReadAsStringAsync().Result); Assert.AreEqual(200, restResponse.StatusCode); } restResponse = HttpClientHelper.PerformGetRequest(getUrl + id, headers); Assert.AreEqual(200, restResponse.StatusCode); JsonRootObject jsonObject = ResponseDataHelper.DeserializeJsonResponse <JsonRootObject>(restResponse.ResponseContent); Assert.IsTrue(jsonObject.Features.Feature.Contains("1 TB of SSD"), "item not found"); }
public void TestPutWithJsonAndHelperClass() { int id = random.Next(1000); string jsonData = "{" + "\"BrandName\": \"Alienware\"," + "\"Features\": {" + "\"Feature\": [" + "\"8th Generation Intel® Core™ i5-8300H\"," + "\"Windows 10 Home 64-bit English\"," + "\"NVIDIA® GeForce® GTX 1660 Ti 6GB GDDR6\"," + "\"8GB, 2x4GB, DDR4, 2666MHz\"" + "]" + "}," + "\"Id\": " + id + "," + "\"LaptopName\": \"Alienware M17\"" + "}"; Dictionary <string, string> headers = new Dictionary <string, string>() { { "Accept", "application/json" } }; restResponse = HttpClientHelper.PerformPostRequest(postUrl, jsonData, jsonMediaType, headers); Assert.Equal(200, restResponse.StatusCode); jsonData = "{" + "\"BrandName\": \"Alienware\"," + "\"Features\": {" + "\"Feature\": [" + "\"8th Generation Intel® Core™ i5-8300H\"," + "\"Windows 10 Home 64-bit English\"," + "\"NVIDIA® GeForce® GTX 1660 Ti 6GB GDDR6\"," + "\"1 TB of SSD\"," + "\"8GB, 2x4GB, DDR4, 2666MHz\"" + "]" + "}," + "\"Id\": " + id + "," + "\"LaptopName\": \"Alienware M17\"" + "}"; restResponse = HttpClientHelper.PerformPutRequest(putUrl, jsonData, jsonMediaType, headers); Assert.Equal(200, restResponse.StatusCode); restResponse = HttpClientHelper.PerformGetRequest(getUrl + id, headers); Assert.Equal(200, restResponse.StatusCode); JsonRootObject jsonObject = ResponseDataHelper.DeserializeJsonResponse <JsonRootObject> (restResponse.responseContent); Assert.Contains("1 TB of SSD", jsonObject.Features.Feature); }
public void TestGetAllEndPointusingGetHelper() { var getUrl = "http://localhost:8080/laptop-bag/webapi/api/find/1"; Dictionary <string, string> httpHeader = new Dictionary <string, string>(); httpHeader.Add("Accept", "application/json"); RestResponse restResponse = HttpClientHelper.PerformGetRequest(getUrl, httpHeader); Console.WriteLine(restResponse.ToString()); JsonRootObject JsonData = JsonConvert.DeserializeObject <JsonRootObject>(restResponse.ResponseData); Assert.AreEqual("Alienware", JsonData.BrandName); }
public void TestPostEndpointWithJson() { //Method - Post //Body along with request -- httpContent class //header - info about data format int id = random.Next(1000); string jsonData = "{" + "\"BrandName\": \"Alienware\"," + "\"Features\": {" + "\"Feature\": [" + "\"8th Generation Intel® Core™ i5-8300H\"," + "\"Windows 10 Home 64-bit English\"," + "\"NVIDIA® GeForce® GTX 1660 Ti 6GB GDDR6\"," + "\"8GB, 2x4GB, DDR4, 2666MHz\"" + "]" + "}," + "\"Id\": " + id + "," + "\"LaptopName\": \"Alienware M17\"" + "}"; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Accept", jsonMediaType); HttpContent httpContent = new StringContent(jsonData, Encoding.UTF8, jsonMediaType); Task <HttpResponseMessage> postResponse = httpClient.PostAsync(postUrl, httpContent); HttpStatusCode statusCode = postResponse.Result.StatusCode; HttpContent responseContent = postResponse.Result.Content; string responseData = responseContent.ReadAsStringAsync().Result; restResponse = new RestResponse((int)statusCode, responseData); Assert.AreEqual(200, restResponse.StatusCode); Assert.IsNotNull(restResponse.ResponseContent, "Response data is null/empty"); Task <HttpResponseMessage> getResponse = httpClient.GetAsync(getUrl + id); restResponseForGet = new RestResponse((int)getResponse.Result.StatusCode, getResponse.Result.Content.ReadAsStringAsync().Result); JsonRootObject jsonObject = JsonConvert.DeserializeObject <JsonRootObject>(restResponseForGet.ResponseContent); Assert.AreEqual(id, jsonObject.Id); Assert.AreEqual("Alienware", jsonObject.BrandName); } }
public static void Crawlera() { ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true; var myProxy = new WebProxy("http://proxy.crawlera.com:8010"); myProxy.Credentials = new NetworkCredential("36f14b90c38c4005a81ccbed16a31f58", ""); //string url = "https://twitter.com/"; string url = "https://api.upcitemdb.com/prod/trial/search?s=nike%20859524-005&match_mode=1&type=product"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); var encodedApiKey = Base64Encode("36f14b90c38c4005a81ccbed16a31f58:"); request.Headers.Add("Proxy-Authorization", "Basic " + encodedApiKey); //request.Proxy = proxy; //request.PreAuthenticate = true; request.Proxy = myProxy; request.PreAuthenticate = true; request.Method = "GET"; request.Accept = "application/json"; WebResponse response = request.GetResponse(); Console.WriteLine("Response Status: " + ((HttpWebResponse)response).StatusDescription); Console.WriteLine("\nResponse Headers:\n" + ((HttpWebResponse)response).Headers); Stream dataStream = response.GetResponseStream(); var reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); Console.WriteLine("Response Body:\n" + responseFromServer); reader.Close(); response.Close(); JsonRootObject jsonObj = JsonConvert.DeserializeObject <JsonRootObject>(responseFromServer); }
public void TestGetUsingRestSharpJsonDeserialization() { IRestClient restClient = new RestClient(); IRestRequest restRequest = new RestRequest(getUrl); restRequest.AddHeader("Accept", "application/json"); IRestResponse <List <JsonRootObject> > restResponse = restClient.Get <List <JsonRootObject> >(restRequest); if (restResponse.IsSuccessful) { Console.WriteLine((int)restResponse.StatusCode); Assert.AreEqual(200, (int)restResponse.StatusCode); List <JsonRootObject> data = restResponse.Data; JsonRootObject laptop = data.Find((x) => { return(x.Id == 761); //passing Anonymous function to Find method of List class. }); Assert.AreEqual("Alienware M17", laptop.LaptopName); } }
public void TestGetUsingRestRequestHelperDeserialiZerJsonData() { Dictionary <string, string> httpHeaders = new Dictionary <string, string>(); httpHeaders.Add("Accept", "application/json"); //Deserializing JSON data IRestResponse <List <JsonRootObject> > restResponse = RestRequestHelper.PerformGetRequest <List <JsonRootObject> >(getUrl, httpHeaders); if (restResponse.IsSuccessful) { Console.WriteLine((int)restResponse.StatusCode); Console.WriteLine(restResponse.Content); Assert.AreEqual(200, (int)restResponse.StatusCode); List <JsonRootObject> data = restResponse.Data; JsonRootObject laptop = data.Find((x) => { return(x.Id == 761); }); Assert.AreEqual("Alienware M17", laptop.LaptopName); } }
public void TestGetUsingExecute() { IRestClient restClient = new RestClient(); IRestRequest restRequest = new RestRequest(getUrl); restRequest.AddHeader("Accept", "application/json"); restRequest.Method = Method.GET; IRestResponse <List <JsonRootObject> > restResponse = restClient.Execute <List <JsonRootObject> >(restRequest); if (restResponse.IsSuccessful) { Console.WriteLine((int)restResponse.StatusCode); Assert.AreEqual(200, (int)restResponse.StatusCode); List <JsonRootObject> data = restResponse.Data; JsonRootObject laptop = data.Find((x) => { return(x.Id == 761); }); Assert.AreEqual("Alienwaree M17", laptop.LaptopName); } }
public void PutEndPointTestusingPuttAsync() { int id = 965; string jsonData = "{" + "\"BrandName\": \"Gadhaware\"," + "\"Features\": {" + "\"Feature\": [" + "\"8th Generation Intel® Core™ i5-8300H\"," + "\"Windows 10 Home 64-bit English\"," + "\"NVIDIA® GeForce® GTX 1660 Ti 6GB GDDR6\"," + "\"8GB, 2x4GB, DDR4, 2666MHz\"" + "]" + "}," + "\"Id\":" + id + "," + "\"LaptopName\": \"Alienware M17\"" + "}"; using (HttpClient httpclient = new HttpClient()) { HttpContent httpcontent = new StringContent(jsonData, Encoding.UTF8, jsonMediaType); Task <HttpResponseMessage> httpResponse = httpclient.PutAsync(putUrl, httpcontent); using (HttpResponseMessage httpResponseMessage = httpResponse.Result) { restResponse = new RestResponse((int)httpResponseMessage.StatusCode, httpResponseMessage.Content.ReadAsStringAsync().Result); Console.WriteLine(restResponse.ToString()); Assert.AreEqual(200, restResponse.StatusCode); Assert.IsNotNull(restResponse.ResponseData, "Response data is not null/empty"); httpclient.DefaultRequestHeaders.Add("Accept", jsonMediaType); Task <HttpResponseMessage> httpGetResponse = httpclient.GetAsync(getUrl + id); restResponseForGet = new RestResponse((int)httpGetResponse.Result.StatusCode, httpGetResponse.Result.Content.ReadAsStringAsync().Result); Console.WriteLine(httpGetResponse.Result.Content.ReadAsStringAsync().Result); JsonRootObject jsonRootObject = ResponseDataHelper.DeserializeJsonResponse <JsonRootObject>(restResponseForGet.ResponseData); Assert.AreEqual(200, restResponseForGet.StatusCode); Assert.AreEqual("Gadhaware", jsonRootObject.BrandName); } } }
public void PostEndpointWithGetWithID() { int id = random.Next(1000); string jSonData = "{" + "\"BrandName\": \"Lenovo\"," + "\"Features\": {" + "\"Feature\": [" + "\"4th Generation Intel® Core™ i2-8300H\"," + "\"Windows 4 Home 64-bit English\"," + "\"NVIDIA® GeForce® GTX 1260 Ti 1GB GDDR6\"," + "\"1GB, 4GB, DDR4, 2000MHz\"" + "]" + "}," + "\"Id\": " + id + "," + "\"LaptopName\": \"Len P07\"" + "}"; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Accept", "application/json"); HttpContent httpContent = new StringContent(jSonData, Encoding.UTF8, JsonMediaType); Task <HttpResponseMessage> postResponse = httpClient.PostAsync(POSTURL, httpContent); HttpStatusCode statusCode = postResponse.Result.StatusCode; HttpContent responseContent = postResponse.Result.Content; string responseData = responseContent.ReadAsStringAsync().Result; restResponse = new RestResponse((int)statusCode, responseData); //Assert.AreEqual(200, restResponse.StatusCode); //Assert.IsNotNull(restResponse.ResponseContent, "Response data is null/empty"); Task <HttpResponseMessage> getResponse = httpClient.GetAsync(GETURL + id); restResponseForGet = new RestResponse((int)getResponse.Result.StatusCode, getResponse.Result.Content.ReadAsStringAsync().Result); JsonRootObject jsonRootObject = JsonConvert.DeserializeObject <JsonRootObject>(restResponseForGet.ResponseContent); Assert.AreEqual(id, jsonRootObject.Id); Assert.AreEqual("Lenovo", jsonRootObject.BrandName); } }
public void testpostasyncjson() { string content = "{" + "\"BrandName\": \"Alienware\"," + "\"Features\": {" + "\"Feature\": [" + "\"8th Generation Intel® Core™ i5-8300H\"," + "\"Windows 10 Home 64-bit English\"," + "\"NVIDIA® GeForce® GTX 1660 Ti 6GB GDDR6\"," + "\"8GB, 2x4GB, DDR4, 2666MHz\"" + "]" + "}," + "\"Id\": " + id + "," + "\"LaptopName\": \"Alienware M16\"" + "}"; using (HttpClient httpClient = new HttpClient()) { HttpContent httpContent = new StringContent(content, Encoding.UTF8, jsondataformat); httpClient.DefaultRequestHeaders.Add("Accept", "application/json"); Task <HttpResponseMessage> httpResponseMessage = httpClient.PostAsync(posturl, httpContent); HttpStatusCode responseStatus = httpResponseMessage.Result.StatusCode; HttpContent responseContent = httpResponseMessage.Result.Content; string responsedata = responseContent.ReadAsStringAsync().Result; restResponse = new RestResponse((int)responseStatus, responsedata); Assert.AreEqual(200, restResponse.StatusCode, "Status code are not equal"); Assert.IsNotNull(restResponse.ResponseContent); Task <HttpResponseMessage> httpgetMessage = httpClient.GetAsync(geturl + id); GetResponse = new RestResponse((int)httpgetMessage.Result.StatusCode, httpgetMessage.Result.Content.ReadAsStringAsync().Result); JsonRootObject jsonrootdeserelize = JsonConvert.DeserializeObject <JsonRootObject>(GetResponse.ResponseContent); Assert.AreEqual(id, jsonrootdeserelize.Id, "Id are not equal"); Assert.AreEqual("Alienware M16", jsonrootdeserelize.LaptopName, "Laptop Name are not same"); } }