How to add middleware to WebApplicationFactory<TEntryPoint> ? #53999
Replies: 4 comments 1 reply
-
HI :) Yes, it is possible to add middleware to the application being tested. However, the issue you’re encountering is likely due to the fact that the middleware you added does not call the next middleware in the pipeline. In ConfigureWebHost :
|
Beta Was this translation helpful? Give feedback.
-
This code returns a 404 - Not Found. public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.Configure(app =>
{
app.Use(async (ctx, next) => { await next(); });
});
base.ConfigureWebHost(builder);
}
} This code returns a 200 - OK public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
}
} It is definitely -NOT- the middleware. |
Beta Was this translation helpful? Give feedback.
-
And for full disclosure - doing nothing in that action also causes a 404 - Not Found
|
Beta Was this translation helpful? Give feedback.
-
Rejoice, my friends! I have found the solution: add an IStartupFilter to the services. class CustomWebApplicationFactory : WebApplicationFactory<Program> {
readonly ITestOutputHelper _output;
readonly DateTimeOffset _logStart;
public CustomWebApplicationFactory(ITestOutputHelper output, DateTimeOffset logStart) {
_output = output;
_logStart = logStart;
}
protected override void ConfigureWebHost(IWebHostBuilder builder) {
builder.ConfigureTestServices(services => {
services.AddSingleton<IStartupFilter>(new LoggingStartupFilter(_output, _logStart));
});
}
}
class LoggingStartupFilter : IStartupFilter {
private readonly ITestOutputHelper _output;
private readonly DateTimeOffset _logStart;
public LoggingStartupFilter(ITestOutputHelper output, DateTimeOffset logStart) {
_output = output;
_logStart = logStart;
}
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) {
return app => {
// This middleware runs at the beginning of the request pipeline
app.Use(async (context, next) => {
_output.WriteLine($"[{(DateTimeOffset.Now - _logStart).TotalSeconds:N3}s] Inbound HTTP request: {context.Request.Method} {context.Request.Path}{context.Request.QueryString}");
await next(context);
_output.WriteLine($"[{(DateTimeOffset.Now - _logStart).TotalSeconds:N3}s] Outbound HTTP response: {context.Response.StatusCode}");
});
next(app);
// Middleware registered here will be after everything defined in Program/Startup
};
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Create a simple web API application project and unit tests:
Change
Program.cs
to make theProgram
class andWeatherForecast
record public:Change
UnitTest1.cs
to be:Run the
WebApiTests.UnitTest1.Test1Async
test and the test succeeds.Change
UnitTest1.cs
to be:Now the test fails with:
Is it not possible to add middleware to the application being tested?
Beta Was this translation helpful? Give feedback.
All reactions