public void TestGetWithXmlDeserialize() { IRestClient restClient = new RestClient(); IRestRequest restRequest = new RestRequest(getUrl); restRequest.AddHeader("Accept", "application/xml"); var dotNetXmlDeserializer = new RestSharp.Deserializers.DotNetXmlDeserializer(); //IRestResponse<LaptopDetailss> restResponse = restClient.Get<LaptopDetailss>(restRequest); IRestResponse restResponse = restClient.Get(restRequest); if (restResponse.IsSuccessful) { Assert.Equal(200, (int)restResponse.StatusCode); // restResponse.Data retorna o objeto depois da deserialização LaptopDetailss data = dotNetXmlDeserializer.Deserialize <LaptopDetailss>(restResponse); Laptop laptop = data.Laptop.Find((x) => { return(x.Id.Equals("1", StringComparison.OrdinalIgnoreCase)); }); Assert.Equal("Alienware M17", laptop.LaptopName); Assert.Contains("8th Generation Intel® Core™ i5-8300H", laptop.Features.Feature); } else { output.WriteLine("Error msg: " + restResponse.ErrorMessage); output.WriteLine("Stack Trace: " + restResponse.ErrorException); } }
//[TestMethodWithReport] public void TestGetWithXml_Deserialize() { IRestClient restClient = new RestClient(); IRestRequest restRequest = new RestRequest(getUrl); restRequest.AddHeader("Accept", "application/xml"); var dotNetXmlDeserializer = new RestSharp.Deserializers.DotNetXmlDeserializer(); //IRestResponse<LaptopDetailss> restResponse = restClient.Get<LaptopDetailss>(restRequest); IRestResponse restResponse = restClient.Get(restRequest); if (restResponse.IsSuccessful) { Console.WriteLine("Status Code " + restResponse.StatusCode); Assert.AreEqual(200, (int)restResponse.StatusCode); LaptopDetailss data = dotNetXmlDeserializer.Deserialize <LaptopDetailss>(restResponse); Console.WriteLine("Size of List " + data.Laptop.Count); Laptop laptop = data.Laptop.Find((x) => { return(x.Id.Equals("1", StringComparison.OrdinalIgnoreCase)); }); Assert.AreEqual("Alienware M17", laptop?.LaptopName); Assert.IsTrue(laptop.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 Response <T> HandleResponse <T>(IRestResponse response) { if (response.StatusCode == HttpStatusCode.NotAcceptable || response.StatusCode.ToString() == "422") { RestSharp.Deserializers.DotNetXmlDeserializer deserial = new RestSharp.Deserializers.DotNetXmlDeserializer(); try { var errors = deserial.Deserialize <ErrorsResponse>(response); return(new Response <T>(default(T), errors, response.StatusCode)); } catch (Exception ex) { var error = new ErrorsResponse(); error.Errors.Add("DESERALIZE EXCEPTION!"); return(new Response <T>(default(T), error, response.StatusCode)); } } else if (response.StatusCode == HttpStatusCode.OK) { RestSharp.Deserializers.XmlDeserializer deserial = new RestSharp.Deserializers.XmlDeserializer(); var deserialResponse = deserial.Deserialize <T>(response); return(new Response <T>(deserialResponse, null, response.StatusCode)); } //TODO:HANDLE INTERNAL SERVER ERROR, REPRODUCED BY SETTING WRONG AUTHENTICTY TOKEN return(new Response <T>(default(T), null, response.StatusCode)); }
private IRestResponse <T> SendRequest <T>(IRestRequest restRequest) where T : new() { IRestClient restClient = GetRestClient(); IRestResponse <T> restResponse = restClient.Execute <T>(restRequest); if (restResponse.ContentType.Equals("application/xml")) { var deserializer = new RestSharp.Deserializers.DotNetXmlDeserializer(); restResponse.Data = deserializer.Deserialize <T>(restResponse); } return(restResponse); }
public void TestGetUsingRestSharpXmlDeserialization() { IRestClient restClient = new RestClient(); IRestRequest restRequest = new RestRequest(getUrl); restRequest.AddHeader("Accept", "application/xml"); var dotNetXmlSerializer = new RestSharp.Deserializers.DotNetXmlDeserializer(); //XML Deserialization Step:1 IRestResponse restResponse = restClient.Get(restRequest); // if (restResponse.IsSuccessful) { Console.WriteLine((int)restResponse.StatusCode); Assert.AreEqual(200, (int)restResponse.StatusCode); LaptopDetailss data = dotNetXmlSerializer.Deserialize <LaptopDetailss>(restResponse); //XML Deserialization Step:2 Assert.AreEqual("Alienware M17", data.Laptop[1].LaptopName); } }
/// <summary> /// Executes the client with given request and returns the response for data collection represented by arbitary type T /// </summary> private IRestResponse <T> SendRequest <T>(IRestRequest restRequest) where T : new() { IRestClient restClient = GetRestClient(); //restRequest.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }; IRestResponse <T> restResponse = restClient.Execute <T>(restRequest); if (restResponse.ContentType.Equals("application/xml") || restResponse.ContentType.Equals("text/html; charset=utf-8")) { var deserializer = new RestSharp.Deserializers.DotNetXmlDeserializer(); restResponse.Data = deserializer.Deserialize <T>(restResponse); } else { restResponse.Data = JsonConvert.DeserializeObject <T>(restResponse.Content); } return(restResponse); }
public void TestGetWithXml_Deserialize() { IRestClient restClient = new RestClient(); IRestRequest restRequest = new RestRequest(getUrlXml); restRequest.AddHeader("Accept", "application/xml"); var dotNetXmlDeserializer = new RestSharp.Deserializers.DotNetXmlDeserializer(); //IRestResponse<Slideshow> restResponse = restClient.Get<Slideshow>(restRequest); IRestResponse restResponse = restClient.Get(restRequest); if (restResponse.IsSuccessful) { Console.WriteLine("Status Code " + restResponse.StatusCode); Assert.AreEqual(200, (int)restResponse.StatusCode); Slideshow data = dotNetXmlDeserializer.Deserialize <Slideshow>(restResponse); Console.WriteLine("Size of Slides " + data.Slide.Count + " - Items " + data.Slide[1].Item.Count); } }
public void TestGetWithXml_Deserialize() { IRestClient restClient = new RestClient(); IRestRequest restRequest = new RestRequest(getUrl); restRequest.AddHeader("accept", "application/xml"); //Deserialize an XML response var dotNetXmlDeserializer = new RestSharp.Deserializers.DotNetXmlDeserializer(); IRestResponse restResponse = restClient.Get(restRequest); if (restResponse.IsSuccessful) { Console.WriteLine("Response code: " + (int)restResponse.StatusCode); Assert.AreEqual(200, (int)restResponse.StatusCode); LaptopDetailss data = dotNetXmlDeserializer.Deserialize <LaptopDetailss>(restResponse); Assert.AreEqual("Alienware", data.Laptop.BrandName); } else { Console.WriteLine("Error Msg : " + restResponse.ErrorMessage); Console.WriteLine("Stack trace : " + restResponse.ErrorException); } }
public Response <T, U> HandleResponse <T, U>(IRestResponse response, Func <bool> whenToDeserializeToSecondGenericParam) { if (response.StatusCode == HttpStatusCode.NotAcceptable || (int)response.StatusCode == 422) { RestSharp.Deserializers.DotNetXmlDeserializer deserial = new RestSharp.Deserializers.DotNetXmlDeserializer(); try { var errors = deserial.Deserialize <ErrorsResponse>(response); var headers = response.Headers.Select(x => new { Name = x.Name, Value = x.Value }).ToDictionary(t => t.Name, t => t.Value?.ToString()); return(new Response <T, U>(default(T), default(U), errors, response.StatusCode, headers)); } catch (Exception) { var error = new ErrorsResponse(); error.Errors.Add("DESERALIZE EXCEPTION!"); return(new Response <T, U>(default(T), default(U), error, response.StatusCode, null)); } } else if (response.StatusCode == HttpStatusCode.OK) { RestSharp.Deserializers.XmlDeserializer deserial = new RestSharp.Deserializers.XmlDeserializer(); if (whenToDeserializeToSecondGenericParam()) { var deserialResponse = deserial.Deserialize <U>(response); var headers = response.Headers.Select(x => new { Name = x.Name, Value = x.Value }).ToDictionary(t => t.Name, t => t.Value?.ToString()); return(new Response <T, U>(default(T), deserialResponse, null, response.StatusCode, headers)); } else { var deserialResponse = deserial.Deserialize <T>(response); var headers = response.Headers.Select(x => new { Name = x.Name, Value = x.Value }).ToDictionary(t => t.Name, t => t.Value?.ToString()); return(new Response <T, U>(deserialResponse, default(U), null, response.StatusCode, headers)); } } //TODO:HANDLE INTERNAL SERVER ERROR, REPRODUCED BY SETTING WRONG AUTHENTICTY TOKEN return(new Response <T, U>(default(T), default(U), null, response.StatusCode, null)); }
public void TestPutWithXmlData() { int id = random.Next(1000); string xmlData = "<Laptop>" + "<BrandName>Alienware</BrandName>" + "<Features>" + "<Feature>8th Generation Intel® Core™ i5 - 8300H</Feature>" + "<Feature>Windows 10 Home 64 - bit English</Feature>" + "<Feature>NVIDIA® GeForce® GTX 1660 Ti 6GB GDDR6</Feature>" + "<Feature>8GB, 2x4GB, DDR4, 2666MHz</Feature>" + "</Features>" + "<Id> " + id + "</Id>" + "<LaptopName>Alienware M17</LaptopName>" + "</Laptop>"; Dictionary <string, string> headers = new Dictionary <string, string>() { { "Content-Type", "application/xml" }, { "Accept", "application/xml" } }; RestClientHelper restClientHelper = new RestClientHelper(); IRestResponse restResponse = restClientHelper.PerformPostRequest(postUrl, headers, xmlData, RestSharp.DataFormat.Xml); Assert.Equal(200, (int)restResponse.StatusCode); xmlData = "<Laptop>" + "<BrandName>Alienware</BrandName>" + "<Features>" + "<Feature>8th Generation Intel® Core™ i5 - 8300H</Feature>" + "<Feature>Windows 10 Home 64 - bit English</Feature>" + "<Feature>NVIDIA® GeForce® GTX 1660 Ti 6GB GDDR6</Feature>" + "<Feature>8GB, 2x4GB, DDR4, 2666MHz</Feature>" + "<Feature>Updated Feature</Feature>" + "</Features>" + "<Id> " + id + "</Id>" + "<LaptopName>Alienware M17</LaptopName>" + "</Laptop>"; IRestClient restClient = new RestClient(); IRestRequest restRequest = new RestRequest() { Resource = putUrl }; restRequest.AddHeader("Content-Type", "application/xml"); restRequest.AddHeader("Accept", "application/xml"); restRequest.AddParameter("xmlBody", xmlData, ParameterType.RequestBody); IRestResponse restResponse1 = restClient.Put(restRequest); var deserializer = new RestSharp.Deserializers.DotNetXmlDeserializer(); var laptop = deserializer.Deserialize <Laptop>(restResponse1); Assert.True(laptop.Features.Feature.Contains("Updated Feature"), "Feature did not got updated"); headers = new Dictionary <string, string>() { { "Accept", "application/xml" } }; var restResponse2 = restClientHelper.PerformGetRequest <Laptop>(getUrl + id, headers); Assert.Equal(200, (int)restResponse2.StatusCode); Assert.True(restResponse2.Data.Features.Feature.Contains("Updated Feature"), "Feature did not got updated"); }
public void TestPutWithXmlData() { int id = random.Next(1000); string xmlData = "<Laptop>" + "<BrandName>Alienware</BrandName>" + "<Features>" + "<Feature>8th Generation Intel® Core™ i5-8300H</Feature>" + "<Feature>Windows 10 Home 64-bit English</Feature>" + "<Feature>NVIDIA® GeForce® GTX 1660 Ti 6GB GDDR6</Feature>" + "<Feature>8GB, 2x4GB, DDR4, 2666MHz</Feature>" + "</Features>" + "<Id>" + id + "</Id>" + "<LaptopName>Alienware M17</LaptopName>" + "</Laptop>"; Dictionary <string, string> httpHeader = new Dictionary <string, string>() { { "Content-Type", "application/xml" }, { "Accept", "application/xml" } }; RestClientHelper restClientHelper = new RestClientHelper(); IRestResponse restResponse = restClientHelper.PerformPostRequest(postUrl, httpHeader, xmlData, DataFormat.Xml); Assert.AreEqual(200, (int)restResponse.StatusCode); xmlData = "<Laptop>" + "<BrandName>Alienware</BrandName>" + "<Features>" + "<Feature>8th Generation Intel® Core™ i5-8300H</Feature>" + "<Feature>Windows 10 Home 64-bit English</Feature>" + "<Feature>NVIDIA® GeForce® GTX 1660 Ti 6GB GDDR6</Feature>" + "<Feature>8GB, 2x4GB, DDR4, 2666MHz</Feature>" + "<Feature>1TB of RAM</Feature>" + "</Features>" + "<Id>" + id + "</Id>" + "<LaptopName>Alienware M17</LaptopName>" + "</Laptop>"; IRestClient restClient = new RestClient(); IRestRequest restRequest = new RestRequest() { Resource = putUrl }; restRequest.AddHeader("Content-Type", "application/xml"); restRequest.AddHeader("Accept", "application/xml"); restRequest.RequestFormat = DataFormat.Xml; restRequest.AddParameter("xmlData", xmlData, ParameterType.RequestBody); IRestResponse putRestResponse = restClient.Put(restRequest); var deserializer = new RestSharp.Deserializers.DotNetXmlDeserializer(); Laptop laptop = deserializer.Deserialize <Laptop>(restResponse); Assert.AreEqual(laptop.Id, id); Dictionary <string, string> getHttpHeadr = new Dictionary <string, string>() { { "Accept", "application/xml" } }; IRestResponse <Laptop> getRestResponse = restClientHelper.PerformGetRequest <Laptop>(getUrl + id, getHttpHeadr); Assert.AreEqual(200, (int)getRestResponse.StatusCode); Assert.IsTrue(getRestResponse.Data.Features.Feature.Contains("1TB of RAM")); }
public void Testputendpointwithxml() { string contentxml = "<Laptop>" + "<BrandName>Alienware</BrandName>" + "<Features>" + "<Feature>8th Generation Intel® Core™ i5-8300H</Feature>" + "<Feature>Windows 10 Home 64-bit English</Feature>" + "<Feature>NVIDIA® GeForce® GTX 1660 Ti 6GB GDDR6</Feature>" + "<Feature>8GB, 2x4GB, DDR4, 2666MHz</Feature>" + "</Features>" + "<Id>" + id.ToString() + "</Id>" + "<LaptopName>Alienware M16</LaptopName>" + "</Laptop>"; Dictionary <string, string> header = new Dictionary <string, string>() { { "Content-Type", "application/xml" }, { "Accept", "application/xml" } }; RestClientHelper restClientHelper = new RestClientHelper(); IRestResponse restresponse = restClientHelper.PerformPostRequest(posturl, header, contentxml, DataFormat.Xml); Assert.AreEqual(200, (int)restresponse.StatusCode); contentxml = "<Laptop>" + "<BrandName>Alienware</BrandName>" + "<Features>" + "<Feature>8th Generation Intel® Core™ i5-8300H</Feature>" + "<Feature>Windows 10 Home 64-bit English</Feature>" + "<Feature>NVIDIA® GeForce® GTX 1660 Ti 6GB GDDR6</Feature>" + "<Feature>8GB, 2x4GB, DDR4, 2666MHz</Feature>" + "<Feature>New Feature</Feature>" + "</Features>" + "<Id>" + id.ToString() + "</Id>" + "<LaptopName>Alienware M16</LaptopName>" + "</Laptop>"; IRestClient restClient = new RestClient(); IRestRequest restRequest = new RestRequest() { Resource = puturl }; restRequest.AddHeader("Content-Type", "application/xml"); restRequest.AddHeader("Accept", "application/xml"); restRequest.RequestFormat = DataFormat.Xml; restRequest.AddParameter("xmlbody", contentxml, ParameterType.RequestBody); IRestResponse restResponse1 = restClient.Put(restRequest); var deserializer = new RestSharp.Deserializers.DotNetXmlDeserializer(); var laptop = deserializer.Deserialize <Laptop>(restResponse1); Assert.IsTrue(laptop.Features.Feature.Contains("New Feature"), "Data is not updated"); header = new Dictionary <string, string> { { "Accept", "application/xml" } }; restClientHelper = new RestClientHelper(); IRestResponse <Laptop> restResponse2 = restClientHelper.PerformGetRequest <Laptop>(geturl + id, header); Assert.AreEqual(200, (int)restResponse2.StatusCode); Assert.IsTrue(restResponse2.Data.Features.Feature.Contains("New Feature"), "Data is not present"); }