我正在尝试使用ASP.NET类型为我的WebApplicationFactory
核心中间件创建一个集成测试。
我的目标是模仿一个ASP.NET核心主机,集成应用程序洞察力,并将数据实际发送到一个真正的Application实例中,以确保一切正常工作。
不幸的是,我的测试服务器没有发送任何数据到应用程序洞察力。
这就是我的测试设置。我从WebApplicationFactory
中子类如下:
public class WebTestFixture : WebApplicationFactory<Startup>
{
protected override IHostBuilder CreateHostBuilder()
{
var builder = Host.CreateDefaultBuilder();
builder.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseEnvironment("Testing");
});
builder.ConfigureAppConfiguration((context, config) =>
{
config.AddUserSecrets<WebTestFixture>();
});
return builder;
}
}
然后,我的测试程序集的测试Startup
类如下所示:
public class Startup
{
public Startup(IConfiguration configuration) => Configuration = configuration;
public IConfiguration Configuration { get; set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry();
services.AddAppInsightsHttpBodyLogging();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseAppInsightsHttpBodyLogging();
app.UseEndpoints(endpoints =>
{
endpoints.MapPost("/", async ctx =>
{
ctx.Response.StatusCode = StatusCodes.Status400BadRequest;
await ctx.Response.WriteAsync("Hello from integration test");
});
});
}
}
检测密钥存储在secrets.json中。
{
"ApplicationInsights": {
"InstrumentationKey": "***HIDDEN***"
}
}
最后我的测试
public class BodyLoggerMiddlewareIntegrationTests : IClassFixture<WebTestFixture>
{
private readonly WebTestFixture _factory;
public BodyLoggerMiddlewareIntegrationTests(WebTestFixture factory) => _factory = factory;
[Fact]
public async void BodyLoggerMiddleware_Should_Send_And_Mask_Data()
{
// Arrange
var client = _factory.CreateClient();
// Act
var requestBody = new
{
Name = "Bommelmaier",
Password = "Abracadabra!",
AccessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dnZWRJbkFzIjoiYWRtaW4iLCJpYXQiOjE0MjI3Nzk2Mzh9.gzSraSYS8EXBxLN_oWnFSRgCzcmJmMjLiuyu5CSpyHI"
};
var response = await client.PostAsync("/", new JsonContent(requestBody));
// Assert
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
}
}
测试通过后,我可以在调试日志中看到下面一行(为了更好的可读性而格式化),它告诉我应用程序洞察力发送我的数据。
Application Insights Telemetry:
{
"name": "AppRequests",
"time": "2021-12-22T19:13:32.8336126Z",
"iKey": "***HIDDEN***",
"tags": {
"ai.application.ver": "15.0.0.0",
"ai.cloud.roleInstance": "MY_HOST",
"ai.operation.id": "3fe600bbe13fcf41b1e52a0df7f7465e",
"ai.operation.name": "POST /",
"ai.internal.sdkVersion": "aspnet5c:2.18.0+a9fc6af7538cc287d263d9bc216c7910bfc34566",
"ai.internal.nodeName": "MY_HOST"
},
"data": {
"baseType": "RequestData",
"baseData": {
"ver": 2,
"id": "3857ad615d298e4b",
"name": "POST /",
"duration": "00:00:00.2006790",
"success": false,
"responseCode": "400",
"url": "http://localhost/",
"properties": {
"_MS.ProcessedByMetricExtractors": "(Name:'Requests', Ver:'1.1')",
"RequestBody": "{\"Name\":\"Bommelmaier\",\"Password\":\"Abracadabra!\",\"AccessToken\":\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dnZWRJbkFzIjoiYWRtaW4iLCJpYXQiOjE0MjI3Nzk2Mzh9.gzSraSYS8EXBxLN_oWnFSRgCzcmJmMjLiuyu5CSpyHI\"}",
"AspNetCoreEnvironment": "Production",
"ResponseBody": "Hello from integration test",
"DeveloperMode": "true"
}
}
}
}
然而,即使在等待了超过30分钟之后,也没有任何东西到达实例。有什么想法吗?我在这里错过了什么?
发布于 2022-01-03 04:33:29
谢谢你安德烈亚斯·温德尔。张贴你的建议作为一个答案,以帮助其他社区成员。
您可以打开费德勒,查看这里提到的主机是否有传出流量,Azure监视器使用的IP地址也要确保允许这些主机/ in进入您的传出网络/防火墙/代理/等规则。
https://stackoverflow.com/questions/70454306
复制相似问题