public async Task CreatesNewPost() { // Arrange. var newPost = new CreatePostRequest() { Title = "new title", Content = "some content", }; // Act. var restResponse = await _apiClient.PostsClient.Create(newPost); // Assert. Assert.NotNull(restResponse); Assert.Equal(HttpStatusCode.Created, restResponse.StatusCode); var location = restResponse.HttpResponse.Headers.Location; Assert.NotNull(location); // Location: scheme://host:port/api/posts/[post-id]?possibleQuery var path = location.GetComponents(UriComponents.Path, UriFormat.Unescaped); var postIdString = path.Substring(path.LastIndexOf('/') + 1); var postId = Guid.Parse(postIdString); var dbContext2 = _testServer.GetBlogDbContext(); Assert.True(await dbContext2.Posts.AnyAsync(p => p.Id == postId)); }
public async Task DeletesExistingPost() { // Arrange. var dbContext = _testServer.GetBlogDbContext(); var post = new Post(); dbContext.Posts.Add(post); await dbContext.SaveChangesAsync(); // Act. var restResponse = await _apiClient.PostsClient.Delete(post.Id); // Assert. Assert.NotNull(restResponse); Assert.Equal(HttpStatusCode.NoContent, restResponse.StatusCode); var dbContext2 = _testServer.GetBlogDbContext(); Assert.False(await dbContext2.Posts.AnyAsync(p => p.Id == post.Id)); }
public async Task UpdatesExistingPost() { // Arrange. var dbContext = _testServer.GetBlogDbContext(); var post = new Post(); dbContext.Posts.Add(post); await dbContext.SaveChangesAsync(); var postUpdate = new UpdatePostRequest() { Title = "new title", Content = "some new content", }; // Act. var restResponse = await _apiClient.PostsClient.Update(post.Id, postUpdate); // Assert. Assert.NotNull(restResponse); Assert.Equal(HttpStatusCode.NoContent, restResponse.StatusCode); var dbContext2 = _testServer.GetBlogDbContext(); var postInDb = await dbContext2.Posts.SingleAsync(p => p.Id == post.Id); Assert.Equal(postUpdate.Content, postInDb.Content); Assert.Equal(postUpdate.Title, postInDb.Title); var location = restResponse.HttpResponse.Headers.Location; Assert.NotNull(location); // Location: scheme://host:port/api/posts/[post-id]?possibleQuery var path = location.GetComponents(UriComponents.Path, UriFormat.Unescaped); var postIdString = path.Substring(path.LastIndexOf('/') + 1); var locationPostId = Guid.Parse(postIdString); Assert.Equal(post.Id, locationPostId); }
public async Task ReturnsAllPosts() { // Arrange. var dbContext = _testServer.GetBlogDbContext(); dbContext.Posts.Add(new Post()); await dbContext.SaveChangesAsync(); // Act. var restResponse = await _apiClient.PostsClient.GetAllPosts(); // Assert. Assert.NotNull(restResponse); Assert.Equal(HttpStatusCode.OK, restResponse.StatusCode); var getAllPostsResponse = restResponse.Response; Assert.NotNull(getAllPostsResponse); var posts = getAllPostsResponse.Posts; Assert.NotNull(posts); Assert.NotEmpty(posts); }