-
Notifications
You must be signed in to change notification settings - Fork 8
tests: migrate integration tests to TUnit.AspNetCore WebApplicationTest #1100
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
Merged
BenjaminMichaelis
merged 18 commits into
main
from
agents/tunit-aspnetcore-integration-tests
May 17, 2026
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
685c57c
Migrate integration tests to TUnit.AspNetCore WebApplicationTest
BenjaminMichaelis 10bfa05
fix: use Factory.Inner.CreateClient() in FunctionalTests for redirect…
BenjaminMichaelis 756a93d
refactor(tests): restore trace correlation and add async InServiceSco…
BenjaminMichaelis 148b516
fix: scope disposal guard to SQLite connections only, not base disposal
BenjaminMichaelis a264306
Merge branch 'main' into agents/tunit-aspnetcore-integration-tests
BenjaminMichaelis 1a845d8
test: fix integration factory disposal and traced redirects
BenjaminMichaelis 2f9c831
test: fix SqliteConnection disposal analyzer warning
BenjaminMichaelis c47b9be
test: address PR feedback on startup order and redirects
BenjaminMichaelis 93269b3
test: address remaining PR review comments
BenjaminMichaelis 395b9d8
refactor(tests): address PR feedback - rename helper, fix doc, scoped…
BenjaminMichaelis dff58d7
fix(tests): make WebApplicationFactory service overrides idempotent
BenjaminMichaelis c4445d2
fix(tests): serialize SQLite EnsureCreated and disable auto redirect …
BenjaminMichaelis 153572e
fix(tests): harden factory override checks and keep redirect handling…
BenjaminMichaelis 761b9d5
refactor(tests): address latest PR feedback on tracing and helper con…
BenjaminMichaelis 4cc987d
fix(tests): serialize schema creation per test factory
BenjaminMichaelis 794da81
fix(tests): restore non-redirecting clients and harden keep-alive init
BenjaminMichaelis 5026519
fix(tests): dispose response before redirect-limit exception
BenjaminMichaelis 25cb216
chore(tests): clarify hosted-service insertion and harden connection …
BenjaminMichaelis 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
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,84 @@ | ||
| using System.Net; | ||
| using Microsoft.AspNetCore.Mvc.Testing; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using TUnit.AspNetCore; | ||
|
|
||
| namespace EssentialCSharp.Web.Tests; | ||
|
|
||
| public abstract class IntegrationTestBase : WebApplicationTest<WebApplicationFactory, Program> | ||
| { | ||
| /// <summary> | ||
| /// Creates an <see cref="HttpClient"/> with <c>AllowAutoRedirect = false</c> so callers can | ||
| /// assert exact redirect status codes and <c>Location</c> headers without the client | ||
| /// silently following them. | ||
| /// </summary> | ||
| protected HttpClient CreateClientWithoutAutoRedirect() => | ||
| Factory.Inner.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false }); | ||
|
|
||
| /// <summary> | ||
| /// Executes an initial GET and follows redirect responses with subsequent GET requests. | ||
| /// This helper is intentionally GET-only. | ||
| /// </summary> | ||
| protected static async Task<HttpResponseMessage> GetFollowingGetRedirectsAsync( | ||
| HttpClient client, | ||
| string relativeUrl, | ||
| int maxRedirects = 10) | ||
| { | ||
| HttpResponseMessage response = await client.GetAsync(relativeUrl); | ||
|
|
||
| for (int redirectCount = 0; | ||
| redirectCount < maxRedirects && IsRedirectStatusCode(response.StatusCode); | ||
| redirectCount++) | ||
| { | ||
| Uri? location = response.Headers.Location; | ||
| if (location is null) | ||
| { | ||
| return response; | ||
| } | ||
|
|
||
| response.Dispose(); | ||
|
|
||
| response = await client.GetAsync(location); | ||
| } | ||
|
|
||
| if (IsRedirectStatusCode(response.StatusCode)) | ||
| { | ||
| response.Dispose(); | ||
| throw new InvalidOperationException( | ||
| $"Exceeded redirect limit ({maxRedirects}) for '{relativeUrl}'. Last status: {(int)response.StatusCode} {response.StatusCode}."); | ||
|
BenjaminMichaelis marked this conversation as resolved.
Comment on lines
+46
to
+48
|
||
| } | ||
|
|
||
| return response; | ||
| } | ||
|
Comment on lines
+22
to
+52
|
||
|
|
||
| private static bool IsRedirectStatusCode(HttpStatusCode statusCode) => | ||
| statusCode == HttpStatusCode.Moved || | ||
| statusCode == HttpStatusCode.Found || | ||
| statusCode == HttpStatusCode.RedirectMethod || | ||
| statusCode == HttpStatusCode.TemporaryRedirect || | ||
| statusCode == HttpStatusCode.PermanentRedirect; | ||
|
BenjaminMichaelis marked this conversation as resolved.
|
||
|
|
||
| protected T InServiceScope<T>(Func<IServiceProvider, T> action) | ||
| { | ||
| using IServiceScope scope = Factory.Services.GetRequiredService<IServiceScopeFactory>().CreateScope(); | ||
| return action(scope.ServiceProvider); | ||
| } | ||
|
|
||
| protected void InServiceScope(Action<IServiceProvider> action) | ||
| { | ||
| using IServiceScope scope = Factory.Services.GetRequiredService<IServiceScopeFactory>().CreateScope(); | ||
| action(scope.ServiceProvider); | ||
| } | ||
|
|
||
| protected async Task<T> InServiceScopeAsync<T>(Func<IServiceProvider, Task<T>> action) | ||
| { | ||
| using IServiceScope scope = Factory.Services.GetRequiredService<IServiceScopeFactory>().CreateScope(); | ||
| return await action(scope.ServiceProvider); | ||
| } | ||
|
|
||
| protected async Task InServiceScopeAsync(Func<IServiceProvider, Task> action) | ||
| { | ||
| using IServiceScope scope = Factory.Services.GetRequiredService<IServiceScopeFactory>().CreateScope(); | ||
| await action(scope.ServiceProvider); | ||
| } | ||
|
BenjaminMichaelis marked this conversation as resolved.
|
||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.