-
Notifications
You must be signed in to change notification settings - Fork 0
Improve API Error Handling #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
elzik
wants to merge
14
commits into
main
Choose a base branch
from
improve-api-error-handling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c79a61c
Do not leak exception details in production
elzik 86f0986
Fix unauthorised scenario asserts
elzik a07c208
Fix whitespace issue
elzik d4309f8
Fix whitespace
elzik e765be2
Fix whitespace
elzik 4b34d4b
Ensure tests cover exception handler as well as development excetion …
elzik 9571f3f
Merge branch 'improve-api-error-handling' of https://github.com/elzik…
elzik a0d1d9f
Revert "Ensure tests cover exception handler as well as development e…
elzik 040c833
Use additional class to ensure tests cover exception handler as well …
elzik 12871ac
Use multiple WebApplicationFactories to cover production and deveopme…
elzik 3ef8ba8
Use TheoryData as return type to provide better type safety
elzik 6867e19
Explicit;ly configure exception handler
elzik dcde89c
Ensure WebApplicationFactory and client get disposed
elzik 22ce3e7
Add http sample file to solution
elzik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
tests/Elzik.Breef.Api.Tests.Functional/DevelopmentWebApplicationFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.AspNetCore.Mvc.Testing; | ||
|
|
||
| namespace Elzik.Breef.Api.Tests.Functional; | ||
|
|
||
| public class DevelopmentWebApplicationFactory : WebApplicationFactory<Program> | ||
| { | ||
| protected override void ConfigureWebHost(IWebHostBuilder builder) | ||
| { | ||
| builder.UseEnvironment("Development"); | ||
| } | ||
| } |
44 changes: 37 additions & 7 deletions
44
tests/Elzik.Breef.Api.Tests.Functional/HealthTestsNative.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,47 @@ | ||
| using Microsoft.AspNetCore.Mvc.Testing; | ||
| using Shouldly; | ||
| using System.Net; | ||
| using System.Net.Http.Json; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Elzik.Breef.Api.Tests.Functional; | ||
|
|
||
| public class HealthTestsNative : HealthTestsBase, IClassFixture<WebApplicationFactory<Program>> | ||
| public class HealthTestsNative | ||
| { | ||
| private readonly WebApplicationFactory<Program> _webApplicationFactory; | ||
| private readonly HttpClient _client; | ||
| private readonly ITestOutputHelper _output; | ||
|
|
||
| protected override HttpClient Client => _client; | ||
| public HealthTestsNative(ITestOutputHelper output) | ||
| { | ||
| _output = output; | ||
| } | ||
|
|
||
| public static IEnumerable<object[]> WebApplicationFactories() | ||
| { | ||
| yield return new object[] { new DevelopmentWebApplicationFactory(), "Development" }; | ||
| yield return new object[] { new ProductionWebApplicationFactory(), "Production" }; | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(WebApplicationFactories))] | ||
| public async Task Health_Called_ReturnsOK(WebApplicationFactory<Program> factory, string environmentName) | ||
| { | ||
| // Arrange | ||
| _output.WriteLine($"Testing health endpoint in {environmentName} environment"); | ||
| using var client = factory.CreateClient(); | ||
| const string baseUrl = "http://localhost"; | ||
|
|
||
| // Act | ||
| var response = await client.GetAsync($"{baseUrl}/health"); | ||
|
|
||
| // Assert | ||
| response.StatusCode.ShouldBe(HttpStatusCode.OK); | ||
| var body = await response.Content.ReadFromJsonAsync<HealthResponse>(); | ||
| body.ShouldNotBeNull(); | ||
| body!.Status.ShouldBe("Healthy"); | ||
| } | ||
|
|
||
| public HealthTestsNative(WebApplicationFactory<Program> webAppFactory) | ||
| private class HealthResponse | ||
| { | ||
| _webApplicationFactory = webAppFactory; | ||
| _client = _webApplicationFactory.CreateClient(); | ||
| public string Status { get; set; } = string.Empty; | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
tests/Elzik.Breef.Api.Tests.Functional/ProductionWebApplicationFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.AspNetCore.Mvc.Testing; | ||
|
|
||
| namespace Elzik.Breef.Api.Tests.Functional; | ||
|
|
||
| public class ProductionWebApplicationFactory : WebApplicationFactory<Program> | ||
| { | ||
| protected override void ConfigureWebHost(IWebHostBuilder builder) | ||
| { | ||
| builder.UseEnvironment("Production"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.