Solve 404 error in unit test (#5145)
This commit is contained in:
parent
f5c116001f
commit
f075128b12
|
|
@ -947,6 +947,7 @@ public sealed class BasicTests
|
|||
int numberOfUnSubscribedEvents = 0;
|
||||
int numberOfSubscribedEvents = 0;
|
||||
int numberOfExceptionCallbacks = 0;
|
||||
bool exceptionHandled = false;
|
||||
|
||||
// configure SDK
|
||||
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
|
||||
|
|
@ -991,18 +992,18 @@ public sealed class BasicTests
|
|||
})
|
||||
.Build();
|
||||
|
||||
TestMiddleware.Create(builder => builder
|
||||
.UseExceptionHandler(handler =>
|
||||
handler.Run(async (ctx) =>
|
||||
{
|
||||
exceptionHandled = true;
|
||||
await ctx.Response.WriteAsync("handled");
|
||||
})));
|
||||
|
||||
using (var client = this.factory
|
||||
.WithWebHostBuilder(builder =>
|
||||
{
|
||||
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
|
||||
builder.Configure(app => app
|
||||
.UseExceptionHandler(handler =>
|
||||
{
|
||||
handler.Run(async (ctx) =>
|
||||
{
|
||||
await ctx.Response.WriteAsync("handled");
|
||||
});
|
||||
}));
|
||||
})
|
||||
.CreateClient())
|
||||
{
|
||||
|
|
@ -1020,6 +1021,7 @@ public sealed class BasicTests
|
|||
Assert.Equal(0, numberOfExceptionCallbacks);
|
||||
Assert.Equal(0, numberOfUnSubscribedEvents);
|
||||
Assert.Equal(2, numberOfSubscribedEvents);
|
||||
Assert.True(exceptionHandled);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
|
|
|||
|
|
@ -47,6 +47,8 @@ public class Program
|
|||
|
||||
app.UseMiddleware<ActivityMiddleware>();
|
||||
|
||||
app.AddTestMiddleware();
|
||||
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
namespace TestApp.AspNetCore;
|
||||
|
||||
public static class TestMiddleware
|
||||
{
|
||||
private static readonly AsyncLocal<Action<IApplicationBuilder>?> Current = new();
|
||||
|
||||
public static IApplicationBuilder AddTestMiddleware(this IApplicationBuilder builder)
|
||||
{
|
||||
if (Current.Value is { } configure)
|
||||
{
|
||||
configure(builder);
|
||||
}
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static void Create(Action<IApplicationBuilder> action)
|
||||
{
|
||||
Current.Value = action;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue