Exemplo n.º 1
0
        ///// <summary>
        ///// Alls the star wars ships helper.
        ///// </summary>
        ///// <param name="uri">The URI.</param>
        ///// <returns>StringBuilder object with content of response.</returns>
        ///// <exception cref="System.Exception">Failure to GET from URI: "
        /////                                             + uri
        /////                                             + Uri
        /////                                             + $"\nHttp Response Code is : {response.StatusCode}</exception>
        public StarWarsShips CallEndpoint(string uri)
        {
            StarWarsShips contentResponse      = new StarWarsShips();
            var           contentStringBuilder = new StringBuilder();

            using (var httpClientHandler = new HttpClientHandler())
            {
                using (var httpClient = new HttpClient(httpClientHandler, true))
                {
                    httpClient.Timeout     = Timeout;
                    httpClient.BaseAddress = Uri;

                    HttpResponseMessage response = null;

                    // Make Get call to API
                    response = Task.Run(() =>
                    {
                        return(httpClient.GetAsync($"{uri}"));
                    }).Result;

                    // the response has a status code OK
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        using (StreamReader contentStream = new StreamReader(Task.Run(() =>
                        {
                            return(response.Content.ReadAsStreamAsync());
                        }).Result))
                        {
                            contentStringBuilder.Append(contentStream.ReadToEndAsync().Result);
                            contentStream.Dispose();
                            contentResponse = JsonConvert.DeserializeObject <StarWarsShips>(contentStringBuilder.ToString());
                        }
                    }
                    // response.StatusCode != HttpStatusCode.OK. This means we had a error in the http call
                    else
                    {
                        throw new Exception($"Failure to GET from URI: "
                                            + Uri
                                            + uri
                                            + $"\nHttp Response Code is : {response.StatusCode}");
                    }

                    return(contentResponse);
                }
            }
        }
Exemplo n.º 2
0
        public void GetAllStarWarsShips_ShouldReturnString_When_Data_IsValid_And_Next_Has_Value()
        {
            StarWarsShipsTest = new StarWarsShips()
            {
                Count    = 1,
                Next     = $"starships/?page=2",
                Previous = null,
            };
            StarWarsShipsTest.Results.Add(new Ship()
            {
                Name        = "executor",
                Consumables = "2 days",
                MGLT        = "100"
            });

            StarWarsShipsTest2 = new StarWarsShips()
            {
                Count    = 1,
                Next     = null,
                Previous = null,
            };
            StarWarsShipsTest2.Results.Add(new Ship()
            {
                Name        = "deathstar",
                Consumables = "2 years",
                MGLT        = "60"
            });

            mockHttpRequest.Setup(h => h.CallEndpoint(It.IsAny <string>())).Returns(StarWarsShipsTest);

            mockHttpRequest.Setup(h => h.CallEndpoint(StarWarsShipsTest.Next)).Returns(StarWarsShipsTest2);

            var response = StarWarsShipService.GetAllStarWarsShips("1000");

            Assert.IsNotNull(response);

            Assert.AreEqual("executor: 0\ndeathstar: 0\n", response);
        }
Exemplo n.º 3
0
        public void GetAllStarWarsShips_ShouldReturnString_When_Data_IsValid_And_Last_Stop_Is_A_Refuel()
        {
            StarWarsShipsTest = new StarWarsShips()
            {
                Count    = 1,
                Next     = null,
                Previous = null,
            };
            StarWarsShipsTest.Results.Add(new Ship()
            {
                Name        = "executor",
                Consumables = "1 day",
                MGLT        = "100"
            });

            mockHttpRequest.Setup(h => h.CallEndpoint(It.IsAny <string>())).Returns(StarWarsShipsTest);

            var response = StarWarsShipService.GetAllStarWarsShips("24000");

            Assert.IsNotNull(response);

            Assert.AreEqual("executor: 9\n", response);
        }
Exemplo n.º 4
0
        /// <summary>
        /// HTTP request.
        /// </summary>
        /// <param name="uri">The URI to call.</param>
        /// <returns>StarWarsShips Model.</returns>
        private StarWarsShips StarWarsShipsHttpRequest(string uri)
        {
            StarWarsShips contentResponse = HttpRequest.CallEndpoint(uri);

            return(contentResponse);
        }