// For FakeServer public void ResponseContainsHeadersAfterHeadersRequest() { string uriRequest = $"api"; var expectedContentTypeHeader = "application/json; charset=utf-8"; DateTime expectedDateHeader; var expectedServerHeader = "Kestrel"; var client = HttpTool.CreateClient(BaseAddressUri, AcceptHeader); AddCleanupAction(() => client.Dispose()); var httpResponseMessage = HttpTool.MakeRequestToServer(client, HttpMethod.Get, uriRequest); expectedDateHeader = DateTime.Now; var headers = httpResponseMessage.Headers; var headersFromContent = httpResponseMessage.Content.Headers; var contentTypeHeader = headersFromContent.GetValues("content-type").FirstOrDefault(); var dateHeader = headers.GetValues("date").FirstOrDefault(); var etagHeader = headers.GetValues("etag").FirstOrDefault(); var serverHeader = headers.GetValues("server").FirstOrDefault(); var actualDateHeader = DateTime.Parse(dateHeader); var timeDiffTotalSeconds = (int)(actualDateHeader - expectedDateHeader).TotalSeconds; Assert.AreEqual(contentTypeHeader, expectedContentTypeHeader, "content-type Header should be valid in the Response"); Assert.IsTrue(timeDiffTotalSeconds < 3, "date Header should be valid in the Response"); Assert.AreEqual(expectedServerHeader, serverHeader, "server Header should be valid in the Response"); }
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!"); } }
public void UserWasUpdatedAfterPatchRequest() { string collection = "users"; var client = HttpTool.CreateClient(BaseAddressUri, AcceptHeader); AddCleanupAction(() => client.Dispose()); var newUserId = HttpTool.CreateAndPostRandomUser(BaseAddressUri, AcceptHeader); AddCleanupAction(() => HttpTool.DeleteUser(client, newUserId)); var newUserFromServer = HttpTool.GetUserById(client, newUserId); var newUserName = $"Updated {newUserFromServer.Name}"; newUserFromServer.Name = newUserName; var updatedUser = new User { Id = newUserId, Name = newUserName }; string uriRequestPatch = $"api/{collection}/{newUserId}"; HttpContent httpContent = HttpTool.ConvertObjectToHttpContent((UserDTO)updatedUser); var httpResponseMessagePatch = HttpTool.MakeRequestToServer(client, HttpMethod.Patch, uriRequestPatch, httpContent); Task <string> readTask = HttpTool.ReadContentFromMessage(httpResponseMessagePatch); var updatedUserFromServer = HttpTool.GetUserById(client, newUserId); Assert.IsTrue(updatedUserFromServer.Equals(newUserFromServer), $"The User in the Response is not the expected one!"); }
public void NotFoundErrorAfterGetWrongCollectionRequest() { string collection = "WrongCollection"; var client = HttpTool.CreateClient(BaseAddressUri, AcceptHeader); AddCleanupAction(() => client.Dispose()); var uriRequestGet = $"api/{collection}/"; var httpResponseMessageGet = HttpTool.MakeRequestToServer(client, HttpMethod.Get, uriRequestGet, toValidateStatusCode: false); Assert.AreEqual(HttpStatusCode.NotFound, httpResponseMessageGet.StatusCode, "\"Not Found\" message should be in HttpResponseMessage!"); }
// 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 UserWasDeletedAfterDeleteRequest() { string collection = "users"; var client = HttpTool.CreateClient(BaseAddressUri, AcceptHeader); AddCleanupAction(() => client.Dispose()); var newUserId = HttpTool.CreateAndPostRandomUser(BaseAddressUri, AcceptHeader); string uriRequestDelete = $"api/{collection}/{newUserId}"; 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 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); }