示例#1
0
        public void TestDeserilizationOfXmlResponse()
        {
            using (HttpClient httpClient = new HttpClient())
            {
                using (HttpRequestMessage httpRequestMessage = new HttpRequestMessage())
                {
                    httpRequestMessage.RequestUri = new Uri(getUrl);
                    httpRequestMessage.Method     = HttpMethod.Get;
                    httpRequestMessage.Headers.Add("Accept", "application/xml");

                    Task <HttpResponseMessage> httpResponse = httpClient.SendAsync(httpRequestMessage);

                    using (HttpResponseMessage httpResponseMessage = httpResponse.Result)
                    {
                        Console.WriteLine(httpResponseMessage.ToString());


                        //status code
                        HttpStatusCode statusCode = httpResponseMessage.StatusCode;
                        //Console.WriteLine("Status code " + statusCode);
                        //Console.WriteLine("Status code " + (int)statusCode);

                        //response data
                        HttpContent   responseContent = httpResponseMessage.Content;
                        Task <string> responseData    = responseContent.ReadAsStringAsync();
                        String        data            = responseData.Result;
                        //Console.WriteLine(data);

                        RestResponse restResponse = new RestResponse((int)statusCode, responseData.Result);
                        //Console.WriteLine(restResponse.ToString());

                        //step 1
                        XmlSerializer xmlSerializer = new XmlSerializer(typeof(LaptopDetailss));

                        //step 2
                        TextReader textReader = new StringReader(restResponse.ResponseContent);

                        //step 3
                        LaptopDetailss xmlData = (LaptopDetailss)xmlSerializer.Deserialize(textReader);
                        Console.WriteLine(xmlData.ToString());

                        //1st checkpoint (assertion) for statu code
                        Assert.AreEqual(200, restResponse.StatusCode);


                        //2nd check point (assertion) for response data
                        Assert.IsNotNull(restResponse.ResponseContent);

                        //3rd assertion
                        Assert.IsTrue(xmlData.Laptop[0].Features.Feature.Contains("Windows 10 Home 64-bit English"), "ITem no found");

                        //4th
                        Assert.AreEqual("Alienware", xmlData.Laptop[0].BrandName);
                    }
                }
            }
        }
        public void TestGetEndpointDeserilizationOfXmlResponse()
        {
            using (HttpClient httpClient = new HttpClient())
            {
                using (HttpRequestMessage httpRequestMessage = new HttpRequestMessage())
                {
                    httpRequestMessage.RequestUri = new Uri(getUrl);
                    httpRequestMessage.Method     = HttpMethod.Get;
                    httpRequestMessage.Headers.Add("Accept", "application/xml");

                    Task <HttpResponseMessage> httpResponse = httpClient.SendAsync(httpRequestMessage);

                    using (HttpResponseMessage HttpResponseMessage = httpResponse.Result)
                    {
                        output.WriteLine(HttpResponseMessage.ToString());

                        //Status Code
                        HttpStatusCode statuscode = HttpResponseMessage.StatusCode;
                        output.WriteLine("Status Code " + statuscode);
                        output.WriteLine("Status Code " + (int)statuscode);

                        //Response Data
                        HttpContent   responseContent = HttpResponseMessage.Content;
                        Task <string> responseData    = responseContent.ReadAsStringAsync();
                        string        data            = responseData.Result;
                        output.WriteLine(data);

                        RestResponse restResponse = new RestResponse((int)statuscode, responseData.Result);
                        //output.WriteLine(restResponse.ToString());

                        //Deserialization of XML Response
                        XmlSerializer xmlSerializer = new XmlSerializer(typeof(LaptopDetailss));

                        TextReader textReader = new StringReader(restResponse.responseContent);

                        LaptopDetailss xmlData = (LaptopDetailss)xmlSerializer.Deserialize(textReader);
                        output.WriteLine(xmlData.ToString());


                        //Asserts (Status Code and Response)
                        Assert.Equal(200, restResponse.StatusCode);
                        Assert.NotNull(restResponse.responseContent);
                        Assert.Contains("Windows 10 Home 64-bit English", xmlData.Laptop[0].Features.Feature);
                        Assert.Equal("Alienware", xmlData.Laptop[0].BrandName);
                    }
                }
            }
        }