|
| 1 | +using System.Net; |
| 2 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using TUnit.AspNetCore; |
| 5 | + |
| 6 | +namespace EssentialCSharp.Web.Tests; |
| 7 | + |
| 8 | +public abstract class IntegrationTestBase : WebApplicationTest<WebApplicationFactory, Program> |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Creates an <see cref="HttpClient"/> with <c>AllowAutoRedirect = false</c> so callers can |
| 12 | + /// assert exact redirect status codes and <c>Location</c> headers without the client |
| 13 | + /// silently following them. |
| 14 | + /// </summary> |
| 15 | + protected HttpClient CreateClientWithoutAutoRedirect() => |
| 16 | + Factory.Inner.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false }); |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Executes an initial GET and follows redirect responses with subsequent GET requests. |
| 20 | + /// This helper is intentionally GET-only. |
| 21 | + /// </summary> |
| 22 | + protected static async Task<HttpResponseMessage> GetFollowingGetRedirectsAsync( |
| 23 | + HttpClient client, |
| 24 | + string relativeUrl, |
| 25 | + int maxRedirects = 10) |
| 26 | + { |
| 27 | + HttpResponseMessage response = await client.GetAsync(relativeUrl); |
| 28 | + |
| 29 | + for (int redirectCount = 0; |
| 30 | + redirectCount < maxRedirects && IsRedirectStatusCode(response.StatusCode); |
| 31 | + redirectCount++) |
| 32 | + { |
| 33 | + Uri? location = response.Headers.Location; |
| 34 | + if (location is null) |
| 35 | + { |
| 36 | + return response; |
| 37 | + } |
| 38 | + |
| 39 | + response.Dispose(); |
| 40 | + |
| 41 | + response = await client.GetAsync(location); |
| 42 | + } |
| 43 | + |
| 44 | + if (IsRedirectStatusCode(response.StatusCode)) |
| 45 | + { |
| 46 | + response.Dispose(); |
| 47 | + throw new InvalidOperationException( |
| 48 | + $"Exceeded redirect limit ({maxRedirects}) for '{relativeUrl}'. Last status: {(int)response.StatusCode} {response.StatusCode}."); |
| 49 | + } |
| 50 | + |
| 51 | + return response; |
| 52 | + } |
| 53 | + |
| 54 | + private static bool IsRedirectStatusCode(HttpStatusCode statusCode) => |
| 55 | + statusCode == HttpStatusCode.Moved || |
| 56 | + statusCode == HttpStatusCode.Found || |
| 57 | + statusCode == HttpStatusCode.RedirectMethod || |
| 58 | + statusCode == HttpStatusCode.TemporaryRedirect || |
| 59 | + statusCode == HttpStatusCode.PermanentRedirect; |
| 60 | + |
| 61 | + protected T InServiceScope<T>(Func<IServiceProvider, T> action) |
| 62 | + { |
| 63 | + using IServiceScope scope = Factory.Services.GetRequiredService<IServiceScopeFactory>().CreateScope(); |
| 64 | + return action(scope.ServiceProvider); |
| 65 | + } |
| 66 | + |
| 67 | + protected void InServiceScope(Action<IServiceProvider> action) |
| 68 | + { |
| 69 | + using IServiceScope scope = Factory.Services.GetRequiredService<IServiceScopeFactory>().CreateScope(); |
| 70 | + action(scope.ServiceProvider); |
| 71 | + } |
| 72 | + |
| 73 | + protected async Task<T> InServiceScopeAsync<T>(Func<IServiceProvider, Task<T>> action) |
| 74 | + { |
| 75 | + using IServiceScope scope = Factory.Services.GetRequiredService<IServiceScopeFactory>().CreateScope(); |
| 76 | + return await action(scope.ServiceProvider); |
| 77 | + } |
| 78 | + |
| 79 | + protected async Task InServiceScopeAsync(Func<IServiceProvider, Task> action) |
| 80 | + { |
| 81 | + using IServiceScope scope = Factory.Services.GetRequiredService<IServiceScopeFactory>().CreateScope(); |
| 82 | + await action(scope.ServiceProvider); |
| 83 | + } |
| 84 | +} |
0 commit comments