Skip to content
Prev Previous commit
Next Next commit
move and remove tests
  • Loading branch information
rainsxng committed Feb 4, 2026
commit e669fef6dc4dfbac3caa6bb991a5248c7df480bf
Original file line number Diff line number Diff line change
Expand Up @@ -109,124 +109,6 @@ public void RequestLatencyExtensions_Add_BindsToConfigSection()
Assert.Equal(expectedOptions.EnableDetailedLatencyBreakdown, options.EnableDetailedLatencyBreakdown);
}

[Fact]
public async Task LatencyInfo_IsPopulated_WhenLoggerWrapsHandlersPipeline()
{
using var sp = new ServiceCollection()
.AddLatencyContext()
.AddRedaction()
.AddHttpClientLatencyTelemetry()
.AddHttpClient("test")
.ConfigurePrimaryHttpMessageHandler(() => new ServerNameStubHandler("TestServer"))
.AddExtendedHttpClientLogging(wrapHandlersPipeline: true)
.Services
.AddFakeLogging()
.BuildServiceProvider();

var client = sp.GetRequiredService<IHttpClientFactory>().CreateClient("test");

using var response = await client.GetAsync("http://localhost/api");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

var collector = sp.GetFakeLogCollector();
var record = collector.LatestRecord;
Assert.NotNull(record);

var latencyInfo = record.GetStructuredStateValue("LatencyInfo");
Assert.False(string.IsNullOrEmpty(latencyInfo));
Assert.StartsWith("v1.0,", latencyInfo);
Assert.Contains("TestServer", latencyInfo);
}

[Fact]
public async Task LatencyInfo_IsPopulated_WithConfigurationSection_AndWrapHandlersPipeline()
{
var configSection = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
{ "Logging:LogBody", "true" }
})
.Build()
.GetSection("Logging");

using var sp = new ServiceCollection()
.AddLatencyContext()
.AddRedaction()
.AddHttpClientLatencyTelemetry()
.AddHttpClient("test")
.ConfigurePrimaryHttpMessageHandler(() => new ServerNameStubHandler("TestServer"))
.AddExtendedHttpClientLogging(configSection, wrapHandlersPipeline: true)
.Services
.AddFakeLogging()
.BuildServiceProvider();

var client = sp.GetRequiredService<IHttpClientFactory>().CreateClient("test");

using var response = await client.GetAsync("http://localhost/api");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

var collector = sp.GetFakeLogCollector();
var record = collector.LatestRecord;
Assert.NotNull(record);

var latencyInfo = record.GetStructuredStateValue("LatencyInfo");
Assert.False(string.IsNullOrEmpty(latencyInfo));
Assert.StartsWith("v1.0,", latencyInfo);
}

[Fact]
public async Task LatencyInfo_IsPopulated_WithActionConfiguration_WhenLoggerDoesNotWrapHandlersPipeline()
{
using var sp = new ServiceCollection()
.AddLatencyContext()
.AddRedaction()
.AddHttpClientLatencyTelemetry()
.AddHttpClient("test")
.ConfigurePrimaryHttpMessageHandler(() => new ServerNameStubHandler("TestServer"))
.AddExtendedHttpClientLogging(o => o.LogBody = true, wrapHandlersPipeline: false)
.Services
.AddFakeLogging()
.BuildServiceProvider();

var client = sp.GetRequiredService<IHttpClientFactory>().CreateClient("test");

using var response = await client.GetAsync("http://localhost/api");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

var collector = sp.GetFakeLogCollector();
var record = collector.LatestRecord;
Assert.NotNull(record);

var latencyInfo = record.GetStructuredStateValue("LatencyInfo");
Assert.False(string.IsNullOrEmpty(latencyInfo));
Assert.StartsWith("v1.0,", latencyInfo);
}

[Fact]
public async Task LatencyInfo_IsNotPresent_WhenLatencyTelemetryNotAdded()
{
using var sp = new ServiceCollection()
.AddRedaction()
.AddExtendedHttpClientLogging()
.AddHttpClient("test")
.ConfigurePrimaryHttpMessageHandler(() => new ServerNameStubHandler("TestServer"))
.Services
.AddFakeLogging()
.BuildServiceProvider();

var client = sp.GetRequiredService<IHttpClientFactory>().CreateClient("test");

using var response = await client.GetAsync("http://localhost/api");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

var collector = sp.GetFakeLogCollector();
var record = collector.LatestRecord;
Assert.NotNull(record);

var latencyInfo = record.GetStructuredStateValue("LatencyInfo");
Assert.True(string.IsNullOrEmpty(latencyInfo));
}

private static IConfigurationSection GetConfigSection(HttpClientLatencyTelemetryOptions options)
{
return new ConfigurationBuilder()
Expand All @@ -237,14 +119,4 @@ private static IConfigurationSection GetConfigSection(HttpClientLatencyTelemetry
.Build()
.GetSection($"{nameof(HttpClientLatencyTelemetryOptions)}");
}

private sealed class ServerNameStubHandler(string serverName) : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Headers.TryAddWithoutValidation(TelemetryConstants.ServerApplicationNameHeader, serverName);
return Task.FromResult(response);
}
}
}
Comment thread
evgenyfedorov2 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -404,6 +405,45 @@ public void AddHttpClientLogging_ServiceCollection_CreatesClientSuccessfully()
Assert.NotNull(httpClient);
}

[Fact]
public async Task LatencyInfo_IsPopulated_WhenLoggerWrapsHandlersPipeline()
{
await using var sp = new ServiceCollection()
.AddLatencyContext()
.AddRedaction()
.AddHttpClientLatencyTelemetry()
.AddHttpClient("test")
.ConfigurePrimaryHttpMessageHandler(() => new ServerNameStubHandler("TestServer"))
Comment thread
rainsxng marked this conversation as resolved.
Outdated
.AddExtendedHttpClientLogging(wrapHandlersPipeline: true)
.Services
.AddFakeLogging()
.BuildServiceProvider();

var client = sp.GetRequiredService<IHttpClientFactory>().CreateClient("test");

using var response = await client.GetAsync("http://localhost/api");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

var collector = sp.GetFakeLogCollector();
var record = collector.LatestRecord;
Assert.NotNull(record);

var latencyInfo = record.GetStructuredStateValue("LatencyInfo");
Assert.False(string.IsNullOrEmpty(latencyInfo));
Assert.StartsWith("v1.0,", latencyInfo);
Assert.Contains("TestServer", latencyInfo);
}

private sealed class ServerNameStubHandler(string serverName) : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Headers.TryAddWithoutValidation(TelemetryConstants.ServerApplicationNameHeader, serverName);
return Task.FromResult(response);
}
}

private static void EnsureSingleLogger<T>(IServiceProvider serviceProvider, string serviceKey)
where T : IHttpClientLogger
{
Expand Down
Loading