public async Task TestGet_ReturnsStatusCode500InternalServerError() { // Arrange var httpClientFactoryMock = Substitute.For <IHttpClientFactory>(); var url = "http://localhost:8090/api/customers"; var fakeHttpMessageHandler = new FakeHttpMessageHandler(new HttpResponseMessage() { StatusCode = HttpStatusCode.InternalServerError, }); var fakeHttpClient = new HttpClient(fakeHttpMessageHandler); httpClientFactoryMock.CreateClient().Returns(fakeHttpClient); // Act HttpResponseMessage response = await fakeHttpClient.GetAsync(url); // Assert response.StatusCode.Should().Be(HttpStatusCode.InternalServerError); }
public async Task TestDeleteCustomerById_Returns404NotFound() { // Arrange var httpClientFactoryMock = Substitute.For <IHttpClientFactory>(); var url = "http://localhost:8090/api/customers/3"; var fakeHttpMessageHandler = new FakeHttpMessageHandler(new HttpResponseMessage() { StatusCode = HttpStatusCode.NotFound, }); var fakeHttpClient = new HttpClient(fakeHttpMessageHandler); httpClientFactoryMock.CreateClient().Returns(fakeHttpClient); // Act HttpResponseMessage response = await fakeHttpClient.DeleteAsync(url); // Assert response.StatusCode.Should().Be(HttpStatusCode.NotFound); }
public async Task TestPost_ReturnsStatusCode201Created() { // Arrange var customerToAdd = new Customer { name = "Michael", age = 50, active = false }; var expectedCustomer = new Customer { Id = "1", name = "Michael", age = 50, active = false }; var httpClientFactoryMock = Substitute.For <IHttpClientFactory>(); var url = "http://localhost:8090/api/customers"; var fakeHttpMessageHandler = new FakeHttpMessageHandler(new HttpResponseMessage() { StatusCode = HttpStatusCode.Created, Content = new StringContent(JsonConvert.SerializeObject(expectedCustomer), Encoding.UTF8, "application/json") }); var fakeHttpClient = new HttpClient(fakeHttpMessageHandler); httpClientFactoryMock.CreateClient().Returns(fakeHttpClient); // Act HttpResponseMessage response = await fakeHttpClient.PostAsync(url, new StringContent(JsonConvert.SerializeObject(customerToAdd), Encoding.UTF8, "application/json")); string actualJsonCustomer = await response.Content.ReadAsStringAsync(); Customer actualCustomer = new Customer(); JsonConvert.PopulateObject(actualJsonCustomer, actualCustomer); // Assert response.StatusCode.Should().Be(HttpStatusCode.Created); actualCustomer .Should() .BeOfType <Customer>() .And .Equals(expectedCustomer); }
public async Task TestGetById_Returns500InternalServerError() { // Arrange Customer expectedCustomer = new Customer { Id = "2", name = "Alice", age = 23, active = false }; var httpClientFactoryMock = Substitute.For <IHttpClientFactory>(); var url = "http://localhost:8090/api/customers/2"; var fakeHttpMessageHandler = new FakeHttpMessageHandler(new HttpResponseMessage() { StatusCode = HttpStatusCode.InternalServerError, }); var fakeHttpClient = new HttpClient(fakeHttpMessageHandler); httpClientFactoryMock.CreateClient().Returns(fakeHttpClient); // Act HttpResponseMessage response = await fakeHttpClient.GetAsync(url); // Assert response.StatusCode.Should().Be(HttpStatusCode.InternalServerError); }
public async Task TestPost_ReturnsStatusCode401NotFound() { // Arrange var customerToAdd = new Customer { name = "Michael", age = 50, active = false }; var httpClientFactoryMock = Substitute.For <IHttpClientFactory>(); var url = "http://localhost:8090/api/customers"; var fakeHttpMessageHandler = new FakeHttpMessageHandler(new HttpResponseMessage() { StatusCode = HttpStatusCode.NotFound }); var fakeHttpClient = new HttpClient(fakeHttpMessageHandler); httpClientFactoryMock.CreateClient().Returns(fakeHttpClient); // Act HttpResponseMessage response = await fakeHttpClient.PostAsync(url, new StringContent(JsonConvert.SerializeObject(customerToAdd), Encoding.UTF8, "application/json")); // Assert response.StatusCode.Should().Be(HttpStatusCode.NotFound); }
public async Task TestDeleteAllCustomers_ReturnsOK() { // Arrange string expectedContent = "Deleted All Customers"; var httpClientFactoryMock = Substitute.For <IHttpClientFactory>(); var url = "http://localhost:8090/api/customers/delete"; var fakeHttpMessageHandler = new FakeHttpMessageHandler(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent("Deleted All Customers", Encoding.UTF8, "application/json") }); var fakeHttpClient = new HttpClient(fakeHttpMessageHandler); httpClientFactoryMock.CreateClient().Returns(fakeHttpClient); // Act HttpResponseMessage response = await fakeHttpClient.DeleteAsync(url); var result = await response.Content.ReadAsStringAsync(); // Assert response.StatusCode.Should().Be(HttpStatusCode.OK); result.Should().Equals(expectedContent); }
public async Task TestPutCustomer_ReturnsStatusCode500InternalServerError() { // Arrange var idOfCustomerToUpdate = 3; var customerUpdateDetails = new Customer { Id = "3", name = "Michael", age = 70, active = true }; var httpClientFactoryMock = Substitute.For <IHttpClientFactory>(); var url = "http://localhost:8090/api/customers/3"; var fakeHttpMessageHandler = new FakeHttpMessageHandler(new HttpResponseMessage() { StatusCode = HttpStatusCode.InternalServerError, }); var fakeHttpClient = new HttpClient(fakeHttpMessageHandler); httpClientFactoryMock.CreateClient().Returns(fakeHttpClient); // Act HttpResponseMessage response = await fakeHttpClient.PutAsync(url, new StringContent(JsonConvert.SerializeObject(customerUpdateDetails), Encoding.UTF8, "application/json")); // Assert response.StatusCode.Should().Be(HttpStatusCode.InternalServerError); }