Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented test for ClientIP enrichement #15

Merged
merged 2 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.2.0" />
<PackageReference Include="FluentAssertions" Version="6.8.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.21" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.10" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.10" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<FakeRemoteIpAddressMiddleware>();

// Register stub telemetry channel for testing
services.AddSingleton<ITelemetryChannel, FakeTelemetryChannel>();

// 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<FakeRemoteIpAddressMiddleware>();
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<ITelemetryChannel>() 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");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using Microsoft.ApplicationInsights.Channel;

namespace ApplicationInsightsRequestLoggingTests;

public class FakeTelemetryChannel : ITelemetryChannel
{
public List<ITelemetry> 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()
{
}
}
1 change: 0 additions & 1 deletion test/ManualTests/ManualTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@
<ItemGroup>
<ProjectReference Include="..\..\src\ApplicationInsightsRequestLogging\ApplicationInsightsRequestLogging.csproj" />
</ItemGroup>

</Project>
4 changes: 3 additions & 1 deletion test/ManualTests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ void ConfigureMiddleware(IApplicationBuilder app, IServiceProvider services)
void ConfigureEndpoints(IEndpointRouteBuilder app, IServiceProvider services)
{
app.MapControllers();
}
}

public partial class Program { }
5 changes: 1 addition & 4 deletions test/ManualTests/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ApplicationInsights": {
"ConnectionString": "replace-me"
}
"AllowedHosts": "*"
}