public void CreateAndPostUsersInLoop()
        {
            for (int i = 0; i < 10; i++)
            {
                var randomNumber   = new Random().Next(1000, 9999);
                var randomUserName = $"RandomUser{randomNumber}";
                var newUser        = new User {
                    Name = randomUserName, Age = 20, Location = "NY", Work = new Work()
                    {
                        Name = "Sela", Location = "BB", Rating = 5
                    }
                };

                string collection     = "users";
                string uriRequestPost = $"api/{collection}";

                var client = HttpTool.CreateClient(BaseAddressUri, AcceptHeader);
                AddCleanupAction(() => client.Dispose());
                HttpContent httpContent = HttpTool.ConvertObjectToHttpContent((UserDTO)newUser);

                var httpResponseMessagePost = HttpTool.MakeRequestToServer(client, HttpMethod.Post, uriRequestPost, httpContent);
                var responseUserAfterPost   = HttpTool.DeserializeFromResponseMessage <User>(httpResponseMessagePost);
                var responsedUserId         = responseUserAfterPost.Id;

                var uriRequestGet = $"api/{collection}/{responsedUserId}";

                var httpResponseMessageGet = HttpTool.MakeRequestToServer(client, HttpMethod.Get, uriRequestGet);
                var userAfterGet           = HttpTool.DeserializeFromResponseMessage <User>(httpResponseMessageGet);
                Assert.IsTrue(userAfterGet.Equals(newUser), $"The User in the Response is not the expected one!");
            }
        }
        // For FakeServer
        public void ResponseContainsCollectionsAfterRequestListOfAllCollections()
        {
            string uriRequest          = $"api";
            var    expectedCollections = new List <string> {
                "users", "movies", "families"
            };

            var client = HttpTool.CreateClient(BaseAddressUri, AcceptHeader);

            AddCleanupAction(() => client.Dispose());
            var httpResponseMessage = HttpTool.MakeRequestToServer(client, HttpMethod.Get, uriRequest);
            var collections         = HttpTool.DeserializeFromResponseMessage <List <string> >(httpResponseMessage);

            Assert.IsTrue(collections.DoesIncludeList(expectedCollections), $"Not all expected collections were found in the Response!\nExpected: {string.Join(", ", expectedCollections.ToArray())}");
            collections.PrintAllList();
        }
        public void ResponseContainsUserNamesAfterRequestOfUsers()
        {
            string collection    = "users";
            string uriRequest    = $"api/{collection}";
            var    expectedNames = new List <string> {
                "Avraham", "Itzhak", "Yaakov"
            };

            var client = HttpTool.CreateClient(BaseAddressUri, AcceptHeader);

            AddCleanupAction(() => client.Dispose());
            var httpResponseMessage = HttpTool.MakeRequestToServer(client, HttpMethod.Get, uriRequest);
            var users     = HttpTool.DeserializeFromResponseMessage <List <User> >(httpResponseMessage);
            var userNames = users.Select(u => u.Name).ToList();

            Assert.IsTrue(userNames.DoesIncludeList(expectedNames), $"Not all expected Names were found in the Response!\nExpected: {string.Join(", ", expectedNames.ToArray())}");
            users.PrintAllList();
        }
        public void PostAndDeleteUsersInLoop()
        {
            for (int i = 0; i < 100; i++)
            {
                // Post
                var randomNumber   = new Random().Next(1000, 9999);
                var randomUserName = $"RandomUser{randomNumber}";
                var newUser        = new User {
                    Name = randomUserName, Age = 20, Location = "NY", Work = new Work()
                    {
                        Name = "Sela", Location = "BB", Rating = 5
                    }
                };

                string collection     = "users";
                string uriRequestPost = $"api/{collection}";

                var client = HttpTool.CreateClient(BaseAddressUri, AcceptHeader);
                AddCleanupAction(() => client.Dispose());
                HttpContent httpContent = HttpTool.ConvertObjectToHttpContent((UserDTO)newUser);

                var httpResponseMessagePost = HttpTool.MakeRequestToServer(client, HttpMethod.Post, uriRequestPost, httpContent);
                var responseUserAfterPost   = HttpTool.DeserializeFromResponseMessage <User>(httpResponseMessagePost);
                var responsedUserId         = responseUserAfterPost.Id;

                var uriRequestGet = $"api/{collection}/{responsedUserId}";

                var httpResponseMessageGet = HttpTool.MakeRequestToServer(client, HttpMethod.Get, uriRequestGet);
                var userAfterGet           = HttpTool.DeserializeFromResponseMessage <User>(httpResponseMessageGet);
                Assert.IsTrue(userAfterGet.Equals(newUser), $"The User in the Response is not the expected one!");

                // Delete
                var    userId           = userAfterGet.Id;
                string uriRequestDelete = $"api/{collection}/{userId}";

                var           httpResponseMessageDelete = HttpTool.MakeRequestToServer(client, HttpMethod.Delete, uriRequestDelete);
                Task <string> readTask = HttpTool.ReadContentFromMessage(httpResponseMessageDelete);

                var httpResponseMessage = HttpTool.EnsureObjectIsNotFound(client, uriRequestDelete);

                Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.NotFound, $"The User was not deleted as expected!");
            }
        }
        public void ResponseContainsUserAfterUserIsCreated()
        {
            // DEMO 01: Creation of random User
            var randomNumber   = new Random().Next(1000, 9999);
            var randomUserName = $"RandomUser{randomNumber}";
            var newUser        = new User {
                Name = randomUserName, Age = 20, Location = "NY", Work = new Work()
                {
                    Name = "Sela", Location = "BB", Rating = 5
                }
            };

            string collection     = "users";
            string uriRequestPost = $"api/{collection}";

            // DEMO 02: HTTP Client Creation - with URI and Headers
            var client = HttpTool.CreateClient(BaseAddressUri, AcceptHeader);

            AddCleanupAction(() => client.Dispose());
            // DEMO 05: Serialization of Json Object
            HttpContent httpContent = HttpTool.ConvertObjectToHttpContent((UserDTO)newUser);

            // DEMO 08: Client makes Request to server, by providing Method, URI and HttpContent
            var httpResponseMessagePost = HttpTool.MakeRequestToServer(client, HttpMethod.Post, uriRequestPost, httpContent);

            // DEMO 13: Deserialization by specific Class, if I want to crop any specific data from large Json Object.
            var responseUserAfterPost = HttpTool.DeserializeFromResponseMessage <JsonResponseUserId>(httpResponseMessagePost);
            //var responseUserAfterPost = HttpTool.DeserializeFromResponseMessage<User>(httpResponseMessagePost);
            var responsedUserId = responseUserAfterPost.Id;

            AddCleanupAction(() => HttpTool.DeleteUser(client, responsedUserId));

            // DEMO 16: Making of GET Request, to get the User just created
            var uriRequestGet = $"api/{collection}/{responsedUserId}";

            var httpResponseMessageGet = HttpTool.MakeRequestToServer(client, HttpMethod.Get, uriRequestGet);
            // DEMO 17: Deserialization by specific Class, this time by User
            var userAfterGet = HttpTool.DeserializeFromResponseMessage <User>(httpResponseMessageGet);

            // DEMO 18: Compare between users - [User after Get Request from the Server] vs. [User created in the beginning]
            Assert.IsTrue(userAfterGet.Equals(newUser), $"The User in the Response is not the expected one!");
        }
        public void ResponseContainsUserDetailsAfterRequestOfUserById()
        {
            string collection   = "users";
            int    id           = 6;
            string uriRequest   = $"api/{collection}/{id}";
            var    expectedUser = new User()
            {
                Id = 6, Name = "Avraham", Age = 32, Location = "LA", Work = new Work()
                {
                    Name = "Samsung", Location = "SK", Rating = 5.0
                }
            };

            var client = HttpTool.CreateClient(BaseAddressUri, AcceptHeader);

            AddCleanupAction(() => client.Dispose());
            var httpResponseMessage = HttpTool.MakeRequestToServer(client, HttpMethod.Get, uriRequest);
            var user = HttpTool.DeserializeFromResponseMessage <User>(httpResponseMessage);

            Assert.IsTrue(user.Equals(expectedUser), $"Response does not include the expected User by Id = {id}");
            Console.WriteLine(user);
        }