diff --git a/test/ApplicationInsightsRequestLoggingTests/ApplicationInsightsRequestLoggingTests.csproj b/test/ApplicationInsightsRequestLoggingTests/ApplicationInsightsRequestLoggingTests.csproj index 188a071..e3e661c 100644 --- a/test/ApplicationInsightsRequestLoggingTests/ApplicationInsightsRequestLoggingTests.csproj +++ b/test/ApplicationInsightsRequestLoggingTests/ApplicationInsightsRequestLoggingTests.csproj @@ -7,18 +7,19 @@ - + - - - - - - + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/ApplicationInsightsRequestLoggingTests/BodyLoggerMiddlewareTests.cs b/test/ApplicationInsightsRequestLoggingTests/BodyLoggerMiddlewareTests.cs index 627bb32..ac732ba 100644 --- a/test/ApplicationInsightsRequestLoggingTests/BodyLoggerMiddlewareTests.cs +++ b/test/ApplicationInsightsRequestLoggingTests/BodyLoggerMiddlewareTests.cs @@ -9,6 +9,9 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using System.Net.Http; +using System.Threading; +using Microsoft.ApplicationInsights.Channel; +using Microsoft.ApplicationInsights.DataContracts; using Moq; using Microsoft.Extensions.DependencyInjection; @@ -178,5 +181,65 @@ public async void BodyLoggerMiddleware_Should_Properly_Pass() var body = await response.Content.ReadAsStringAsync(); body.Should().Be("Hello from terminating delegate!"); } + + [Fact] + public async void BodyLoggerMiddleware_Should_Disable_Ip_Masking() + { + // Arrange + using var host = await new HostBuilder() + .ConfigureWebHost(webBuilder => + { + webBuilder + .UseTestServer() + .ConfigureTestServices(services => + { + // Set fake client IP + services.AddSingleton(); + + // Register stub telemetry channel for testing + services.AddSingleton(); + + // Register app insights infrastructure + services.AddApplicationInsightsTelemetry(); + + // Add request body logging middleware + services.AddAppInsightsHttpBodyLogging(o => + { + // Ensure middleware kicks in on success status + o.HttpCodes.Add(StatusCodes.Status200OK); + o.DisableIpMasking = true; + }); + }) + .Configure(app => + { + app.UseMiddleware(); + app.UseAppInsightsHttpBodyLogging(); + app.Run(async context => + { + // Send request body back in response body + await context.Request.Body.CopyToAsync(context.Response.Body); + }); + }); + }) + .StartAsync(); + + // Act + _ = await host.GetTestClient().PostAsync("/", new StringContent("Hello from client")); + + // Assert + var channel = host.Services.GetService() as FakeTelemetryChannel; + channel.Should().NotBeNull(); + + // Unfortunately, threads can't be synchronized in a deterministic manner + SpinWait.SpinUntil(() => + { + Thread.Sleep(10); + return channel?.SentTelemtries.Count >= 1; + }, TimeSpan.FromSeconds(3)).Should().BeTrue(); + + var requestItem = channel?.SentTelemtries.First() as RequestTelemetry; + requestItem.Should().NotBeNull(); + requestItem?.Properties["ClientIp"].Should().Be("127.168.1.32"); + } } } diff --git a/test/ApplicationInsightsRequestLoggingTests/FakeTelemetryChannel.cs b/test/ApplicationInsightsRequestLoggingTests/FakeTelemetryChannel.cs new file mode 100644 index 0000000..7e60a0a --- /dev/null +++ b/test/ApplicationInsightsRequestLoggingTests/FakeTelemetryChannel.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using Microsoft.ApplicationInsights.Channel; + +namespace ApplicationInsightsRequestLoggingTests; + +public class FakeTelemetryChannel : ITelemetryChannel +{ + public List SentTelemtries = new(); + public bool IsFlushed { get; private set; } + public bool? DeveloperMode { get; set; } + public string EndpointAddress { get; set; } + + public void Send(ITelemetry item) + { + SentTelemtries.Add(item); + } + + public void Flush() + { + IsFlushed = true; + } + + public void Dispose() + { + } +} \ No newline at end of file diff --git a/test/ManualTests/ManualTests.csproj b/test/ManualTests/ManualTests.csproj index b73d7c2..92ea18d 100644 --- a/test/ManualTests/ManualTests.csproj +++ b/test/ManualTests/ManualTests.csproj @@ -14,5 +14,4 @@ - diff --git a/test/ManualTests/Program.cs b/test/ManualTests/Program.cs index a0c79fc..d4f3959 100644 --- a/test/ManualTests/Program.cs +++ b/test/ManualTests/Program.cs @@ -38,4 +38,6 @@ void ConfigureMiddleware(IApplicationBuilder app, IServiceProvider services) void ConfigureEndpoints(IEndpointRouteBuilder app, IServiceProvider services) { app.MapControllers(); -} \ No newline at end of file +} + +public partial class Program { } \ No newline at end of file diff --git a/test/ManualTests/appsettings.json b/test/ManualTests/appsettings.json index 732b0ef..10f68b8 100644 --- a/test/ManualTests/appsettings.json +++ b/test/ManualTests/appsettings.json @@ -5,8 +5,5 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*", - "ApplicationInsights": { - "ConnectionString": "replace-me" - } + "AllowedHosts": "*" }