public async Task ShouldThrowFriendlyHierarchyOfExceptions() { // Arrange using var fixture = TestApplicationFactory <StartupForIntegration> .Create(); // Act var response = await fixture.CreateClient().GetAsync("api/Errors/throwHierarchy"); var responseString = await response.Content.ReadAsStringAsync(); var content = JsonConvert.DeserializeObject <ErrorResponse>(responseString); // Assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); Assert.Contains("Admin root thrown!", content.Message); Assert.Collection(content.UserErrors, e1 => { Assert.Equal("Admin root thrown!", e1.Message); Assert.Equal("OurApplicationException", e1.ErrorType); Assert.Collection(e1.ChildErrors, t1 => { Assert.Equal("My friendly leaf1!", t1.Message); Assert.Equal("OurApplicationException", t1.ErrorType); Assert.Empty(t1.ChildErrors); }, t2 => { Assert.Equal("Third party plugin has failed!", t2.Message); Assert.Equal("ThirdPartyPluginFailedException", t2.ErrorType); Assert.Empty(t2.ChildErrors); } ); }); Assert.Empty(content.RawErrors); }
public async Task GivenApi_WhenServiceIsOverrided_ThenReturnResultFromFakeService() { // Arrange const string dataToProcess = "my-data"; var myServiceMock = Substitute.For <IMyService>(); myServiceMock.Process(Arg.Any <string>()) .Returns(ci => $"Processed {dataToProcess} using very FAKE service"); using var fixture = TestApplicationFactory <StartupForIntegration> .Create(s => { s.Replace(ServiceDescriptor.Transient(c => myServiceMock)); }); // Act var response = await fixture.CreateClient().GetAsync($"api/Service/{dataToProcess}"); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); // Assert Assert.Equal($"Processed {dataToProcess} using very FAKE service", responseString); }
public async Task ForPostShouldReturnWithModelValidationError() { // Arrange var request = new StoreDocumentsRequest { RequestId = Guid.NewGuid(), Operation = "INVALID", Owner = new Owner(), Documents = new List <Document>() }; using var fixture = TestApplicationFactory <StartupForIntegration> .Create(); // Act var response = await fixture.CreateClient().PostAsync("api/Validation", new JsonContent(request)); // Assert var responseString = await response.Content.ReadAsStringAsync(); Assert.Equal(UnprocessableEntity, (int)response.StatusCode); Assert.NotNull(response); Assert.Contains( $"'{request.Operation}' is not supported. Supported operations are: Validate, Process, Publish.", responseString); }
public async Task GivenAuthenticationWithOAuth2_WhenQueryingTheSecureResourceWithoutAuthorizationHeader_ThenGetInternalServerError() { // Arrange using var fixture = TestApplicationFactory <StartupForNoAuth> .Create(); // Act var response = await fixture.CreateClient().GetAsync("api/Authentication/secured"); // Assert response.StatusCode.Should().Be(HttpStatusCode.BadRequest); }
public async Task GivenAuthenticationWithImpersonation_WhenQueryingTheResourceWithoutAuthorizationHeader_ThenGetUnauthorizedStatus() { // Arrange using var fixture = TestApplicationFactory <StartupForImpersonation> .Create(); // Act var response = await fixture.CreateClient().GetAsync("api/Authentication/secured"); // Assert response.StatusCode.Should().Be(HttpStatusCode.Unauthorized); }
public async Task GivenAFeature_WhenRequestedFeatureStateIsPresentedInConfiguration_ThenReturnTheStatus(string name, HttpStatusCode statusCode, bool expected) { // Arrange using var fixture = TestApplicationFactory <StartupForIntegration> .Create(); // Act var response = await fixture.CreateClient().GetAsync($"api/feature/{name}"); // Assert response.StatusCode.Should().Be(statusCode); var actual = await response.Content.ReadAsStringAsync(); actual.Should().Be(expected.ToString().ToLower()); }
public async Task ForPostShouldReturnErrorWithMissingArguments() { // Arrange using var fixture = TestApplicationFactory <StartupForIntegration> .Create(); // Act var response = await fixture.CreateClient().PostAsync("api/Validation", new StringContent(string.Empty, Encoding.UTF8, "application/json")); // Assert var responseString = await response.Content.ReadAsStringAsync(); Assert.Equal(UnprocessableEntity, (int)response.StatusCode); Assert.Contains("A non-empty request body is required", responseString); }
public async Task GivenAuthenticationWithImpersonation_WhenQueryingTheResourceAuthorizationHeader_ThenGetRelevantStatus(string header, HttpStatusCode statusCode) { // Arrange using var fixture = TestApplicationFactory <StartupForImpersonation> .Create(); var client = fixture.CreateClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(ImpersonationBearerDefaults.AuthenticationScheme, header); // Act var response = await client.GetAsync("api/Authentication/secured"); // Assert response.StatusCode.Should().Be(statusCode); }
public async Task GivenAssetWhenRequestedShouldMapAndReturnAssetWithCorrectRating(string assetId) { // Arrange using var fixture = TestApplicationFactory <StartupForAutomapper> .Create(); // Act var response = await fixture.CreateClient().GetAsync($"api/automapper/asset/{assetId}"); var responseString = await response.Content.ReadAsStringAsync(); var content = JsonConvert.DeserializeObject <AssetDo>(responseString); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(assetId, content.Id); Assert.Equal("AAA", content.Rating); }
public async Task GivenApi_WhenNoServiceReplaced_ThenReturnResultAsExpected() { // Arrange const string dataToProcess = "my-data"; using var fixture = TestApplicationFactory <StartupForIntegration> .Create(); // Act var response = await fixture.CreateClient().GetAsync($"api/Service/{dataToProcess}"); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); // Assert Assert.Equal($"Processed {dataToProcess}", responseString); }
public async Task GivenUserWhenRequestedShouldMapAndReturnCorrectUserResult(string firstName, string lastName) { // Arrange using var fixture = TestApplicationFactory <StartupForAutomapper> .Create(); // Act var response = await fixture.CreateClient().GetAsync($"api/automapper/person/{firstName}/{lastName}"); var responseString = await response.Content.ReadAsStringAsync(); var content = JsonConvert.DeserializeObject <PersonDo>(responseString); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(firstName, content.FirstName); Assert.Equal(lastName, content.LastName); }
public async Task GivenAPIRunning_WhenHealthRequested_ShouldReturnHealthy() { // Arrange using var fixture = TestApplicationFactory <StartupForHealthy> .Create(); // Act var response = await fixture.CreateClient().GetAsync($"health"); var responseString = await response.Content.ReadAsStringAsync(); var actual = JsonConvert.DeserializeObject <HealthData>(responseString); // Assert response.StatusCode.Should().Be(HttpStatusCode.OK); actual.Status.Should().Be(HealthStatus.Healthy); actual.Entries["microsoft"].Status.Should().Be(HealthStatus.Healthy); actual.Entries["microsoft"].Exception.Should().BeNull(); }
public async Task GivenEnvironmentVariableWhenIsNotAvailableItShouldReturnRelevantHttpResponse( string key, HttpStatusCode expectedStatus, string expectedValue) { // Arrange using var fixture = TestApplicationFactory <StartupForConfiguration> .Create(); // Act var response = await fixture.CreateClient().GetAsync($"api/configuration/environment/{key}"); var responseString = await response.Content.ReadAsStringAsync(); // Assert Assert.Equal(expectedStatus, response.StatusCode); if (expectedStatus == HttpStatusCode.OK) { Assert.Equal(expectedValue, responseString); } }
public async Task GivenAPIRunning_WhenHealthRequestedAndOneOfTheDependenciesIsUnhealthy_ShouldReturnUnhealthy() { // Arrange using var fixture = TestApplicationFactory <StartupForUnhealthy> .Create(); // Act var response = await fixture.CreateClient().GetAsync($"health"); var responseString = await response.Content.ReadAsStringAsync(); var actual = JsonConvert.DeserializeObject <HealthData>(responseString); // Assert response.StatusCode.Should().Be(HttpStatusCode.ServiceUnavailable); actual.Status.Should().Be(HealthStatus.Unhealthy); actual.Entries["microsoft"].Status.Should().Be(HealthStatus.Healthy); actual.Entries["microsoft"].Exception.Should().BeNull(); actual.Entries["microsoftwithwrongurl"].Status.Should().Be(HealthStatus.Unhealthy); actual.Entries["microsoftwithwrongurl"].Exception.Should().NotBeNull(); actual.Entries["microsoftwithwrongurl"].Description.Should().NotBeNull(); }
public async Task ForPostShouldReturn200Ok(string operation) { // Arrange var request = new StoreDocumentsRequest { RequestId = Guid.NewGuid(), Operation = operation, Owner = new Owner(), Documents = new List <Document> { new Document() } }; using var fixture = TestApplicationFactory <StartupForIntegration> .Create(); // Act var response = await fixture.CreateClient().PostAsync("api/Validation", new JsonContent(request)); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
public async Task ShouldThrowFriendlyException() { // Arrange using var fixture = TestApplicationFactory <StartupForIntegration> .Create(); // Act var response = await fixture.CreateClient().GetAsync("api/Errors/throwFriendly"); var responseString = await response.Content.ReadAsStringAsync(); var content = JsonConvert.DeserializeObject <ErrorResponse>(responseString); // Assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); Assert.Equal("I am a user friendly exception message", content.Message); Assert.Collection(content.UserErrors, e1 => { Assert.Equal("I am a user friendly exception message", e1.Message); Assert.Equal("OurApplicationException", e1.ErrorType); Assert.Empty(e1.ChildErrors); }); Assert.Empty(content.RawErrors); }
public async Task ForPostShouldReturnWithMultipleModelValidationErrors() { // Arrange var request = new StoreDocumentsRequest { Documents = new List <Document> { new Document() } }; using var fixture = TestApplicationFactory <StartupForIntegration> .Create(); // Act var response = await fixture.CreateClient().PostAsync("api/Validation", new JsonContent(request)); // Assert var responseString = await response.Content.ReadAsStringAsync(); Assert.Equal(UnprocessableEntity, (int)response.StatusCode); Assert.Contains("Operation must be supplied! Supported operations are: Validate, Process, Publish.", responseString); Assert.Contains("'Request Id' must not be empty.", responseString); }