Skip to content

Commit daaf0d5

Browse files
committed
Add unit tests.
1 parent d3c43e7 commit daaf0d5

File tree

6 files changed

+277
-0
lines changed

6 files changed

+277
-0
lines changed

Serilog.Enrichers.ClientInfo.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F5A0A932-D
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Enrichers.ClientInfo", "src\Serilog.Enrichers.ClientInfo\Serilog.Enrichers.ClientInfo.csproj", "{232EFC21-2E8D-44B0-8506-C0DCE122AAA2}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Enrichers.ClientInfo.Tests", "test\Serilog.Enrichers.ClientInfo.Tests\Serilog.Enrichers.ClientInfo.Tests.csproj", "{A6AFC898-47F8-4F6D-9391-C79689885B7A}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -19,12 +21,17 @@ Global
1921
{232EFC21-2E8D-44B0-8506-C0DCE122AAA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
2022
{232EFC21-2E8D-44B0-8506-C0DCE122AAA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
2123
{232EFC21-2E8D-44B0-8506-C0DCE122AAA2}.Release|Any CPU.Build.0 = Release|Any CPU
24+
{A6AFC898-47F8-4F6D-9391-C79689885B7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{A6AFC898-47F8-4F6D-9391-C79689885B7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
26+
{A6AFC898-47F8-4F6D-9391-C79689885B7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
27+
{A6AFC898-47F8-4F6D-9391-C79689885B7A}.Release|Any CPU.Build.0 = Release|Any CPU
2228
EndGlobalSection
2329
GlobalSection(SolutionProperties) = preSolution
2430
HideSolutionNode = FALSE
2531
EndGlobalSection
2632
GlobalSection(NestedProjects) = preSolution
2733
{232EFC21-2E8D-44B0-8506-C0DCE122AAA2} = {84342DE9-9E61-4802-9E77-305CFFD0D2B5}
34+
{A6AFC898-47F8-4F6D-9391-C79689885B7A} = {F5A0A932-DD02-47C1-B81E-3F34F879FEEF}
2835
EndGlobalSection
2936
GlobalSection(ExtensibilityGlobals) = postSolution
3037
SolutionGuid = {785F0E90-8DC5-4003-AD7A-33DE806F3B3A}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using Microsoft.AspNetCore.Http;
2+
using NSubstitute;
3+
using Serilog.Events;
4+
using Xunit;
5+
6+
namespace Serilog.Enrichers.ClientInfo.Tests
7+
{
8+
public class ClientAgentEnricherTests
9+
{
10+
private readonly IHttpContextAccessor _contextAccessor;
11+
12+
public ClientAgentEnricherTests()
13+
{
14+
var httpContext = new DefaultHttpContext();
15+
_contextAccessor = Substitute.For<IHttpContextAccessor>();
16+
_contextAccessor.HttpContext.Returns(httpContext);
17+
}
18+
19+
[Fact]
20+
public void When_Enrich_Log_Event_ClientAgentEnricher_Should_Contain_ClientAgent_Property()
21+
{
22+
// Arrange
23+
var agentEnricher = new ClientAgentEnricher(_contextAccessor);
24+
25+
LogEvent evt = null;
26+
var log = new LoggerConfiguration()
27+
.Enrich.With(agentEnricher)
28+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
29+
.CreateLogger();
30+
31+
// Act
32+
log.Information(@"Has an Agent property");
33+
34+
// Assert
35+
Assert.NotNull(evt);
36+
Assert.True(evt.Properties.ContainsKey("ClientAgent"));
37+
Assert.True(string.IsNullOrEmpty(evt.Properties["ClientAgent"].LiteralValue().ToString()));
38+
}
39+
40+
[Fact]
41+
public void When_Enrich_Log_Event_ClientAgentEnricher_And_Request_Contain_UserAgentHeader_Should_ClientAgentProperty_Have_Value()
42+
{
43+
// Arrange
44+
_contextAccessor.HttpContext.Request.Headers.Add("User-Agent", "Test Agent");
45+
46+
var agentEnricher = new ClientAgentEnricher(_contextAccessor);
47+
48+
LogEvent evt = null;
49+
var log = new LoggerConfiguration()
50+
.Enrich.With(agentEnricher)
51+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
52+
.CreateLogger();
53+
54+
// Act
55+
log.Information(@"Has an Agent property");
56+
57+
// Assert
58+
Assert.NotNull(evt);
59+
Assert.True(evt.Properties.ContainsKey("ClientAgent"));
60+
Assert.Equal("Test Agent", evt.Properties["ClientAgent"].LiteralValue().ToString());
61+
}
62+
}
63+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using Microsoft.AspNetCore.Http;
2+
using NSubstitute;
3+
using Serilog.Events;
4+
using System.Net;
5+
using Xunit;
6+
7+
namespace Serilog.Enrichers.ClientInfo.Tests
8+
{
9+
public class ClientIpEnricherTests
10+
{
11+
private readonly IHttpContextAccessor _contextAccessor;
12+
13+
public ClientIpEnricherTests()
14+
{
15+
var httpContext = new DefaultHttpContext();
16+
_contextAccessor = Substitute.For<IHttpContextAccessor>();
17+
_contextAccessor.HttpContext.Returns(httpContext);
18+
}
19+
20+
[Fact]
21+
public void When_Enrich_Log_Event_With_IpEnricher_Should_Contain_ClientIp_Property()
22+
{
23+
// Arrange
24+
_contextAccessor.HttpContext.Connection.RemoteIpAddress = IPAddress.Parse("::1");
25+
26+
var ipEnricher = new ClientIpEnricher(_contextAccessor);
27+
28+
LogEvent evt = null;
29+
var log = new LoggerConfiguration()
30+
.Enrich.With(ipEnricher)
31+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
32+
.CreateLogger();
33+
34+
// Act
35+
log.Information(@"Has an IP property");
36+
37+
// Assert
38+
Assert.NotNull(evt);
39+
Assert.True(evt.Properties.ContainsKey("ClientIp"));
40+
Assert.Equal("::1", evt.Properties["ClientIp"].LiteralValue());
41+
}
42+
43+
[Fact]
44+
public void When_Enrich_Log_Event_With_IpEnricher_And_Log_More_Than_Once_Should_Read_ClientIp_Value_From_HttpContext_Items()
45+
{
46+
//Arrange
47+
_contextAccessor.HttpContext.Connection.RemoteIpAddress = IPAddress.Loopback;
48+
var ipEnricher = new ClientIpEnricher(_contextAccessor);
49+
50+
LogEvent evt = null;
51+
var log = new LoggerConfiguration()
52+
.Enrich.With(ipEnricher)
53+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
54+
.CreateLogger();
55+
56+
// Act
57+
log.Information(@"Has an IP property");
58+
log.Information(@"Has an other IP property");
59+
60+
// Assert
61+
Assert.NotNull(evt);
62+
Assert.True(evt.Properties.ContainsKey("ClientIp"));
63+
Assert.Equal(IPAddress.Loopback.ToString(), evt.Properties["ClientIp"].LiteralValue());
64+
}
65+
66+
[Fact]
67+
public void When_Enrich_Log_Event_With_IpEnricher_AndRequest_Contain_ForwardHeader_Should_Read_ClientIp_Value_From_Header_Value()
68+
{
69+
//Arrange
70+
_contextAccessor.HttpContext.Connection.RemoteIpAddress = IPAddress.Loopback;
71+
_contextAccessor.HttpContext.Request.Headers.Add("X-forwarded-for", IPAddress.Broadcast.ToString());
72+
73+
var ipEnricher = new ClientIpEnricher(_contextAccessor);
74+
75+
LogEvent evt = null;
76+
var log = new LoggerConfiguration()
77+
.Enrich.With(ipEnricher)
78+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
79+
.CreateLogger();
80+
81+
// Act
82+
log.Information(@"Has an IP property");
83+
84+
// Assert
85+
Assert.NotNull(evt);
86+
Assert.True(evt.Properties.ContainsKey("ClientIp"));
87+
Assert.Equal(IPAddress.Broadcast.ToString(), evt.Properties["ClientIp"].LiteralValue());
88+
}
89+
90+
[Fact]
91+
public void When_Enrich_Log_Event_ClientAgentEnricher_Should_Contain_ClientAgent_Property()
92+
{
93+
var httpContext = new DefaultHttpContext();
94+
var contextAccessor = Substitute.For<IHttpContextAccessor>();
95+
contextAccessor.HttpContext.Returns(httpContext);
96+
97+
var agentEnricher = new ClientAgentEnricher(contextAccessor);
98+
99+
LogEvent evt = null;
100+
var log = new LoggerConfiguration()
101+
.Enrich.With(agentEnricher)
102+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
103+
.CreateLogger();
104+
105+
log.Information(@"Has an Agent property");
106+
107+
Assert.NotNull(evt);
108+
Assert.True(evt.Properties.ContainsKey("ClientAgent"));
109+
Assert.True(string.IsNullOrEmpty(evt.Properties["ClientAgent"].LiteralValue().ToString()));
110+
}
111+
112+
[Fact]
113+
public void When_Enrich_Log_Event_ClientAgentEnricher_And_Request_Contain_UserAgentHeader_Should_ClientAgentProperty_Have_Value()
114+
{
115+
var httpContext = new DefaultHttpContext();
116+
httpContext.Request.Headers.Add("User-Agent", "Test Agent");
117+
var contextAccessor = Substitute.For<IHttpContextAccessor>();
118+
contextAccessor.HttpContext.Returns(httpContext);
119+
120+
var agentEnricher = new ClientAgentEnricher(contextAccessor);
121+
122+
LogEvent evt = null;
123+
var log = new LoggerConfiguration()
124+
.Enrich.With(agentEnricher)
125+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
126+
.CreateLogger();
127+
128+
log.Information(@"Has an Agent property");
129+
130+
Assert.NotNull(evt);
131+
Assert.True(evt.Properties.ContainsKey("ClientAgent"));
132+
Assert.Equal("Test Agent", evt.Properties["ClientAgent"].LiteralValue().ToString());
133+
}
134+
}
135+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Serilog.Core;
2+
using Serilog.Events;
3+
using System;
4+
5+
namespace Serilog.Enrichers.ClientInfo.Tests
6+
{
7+
public class DelegatingSink : ILogEventSink
8+
{
9+
private readonly Action<LogEvent> _write;
10+
11+
public DelegatingSink(Action<LogEvent> write)
12+
{
13+
_write = write ?? throw new ArgumentNullException(nameof(write));
14+
}
15+
16+
public void Emit(LogEvent logEvent)
17+
{
18+
_write(logEvent);
19+
}
20+
21+
public static LogEvent GetLogEvent(Action<ILogger> writeAction)
22+
{
23+
LogEvent result = null;
24+
var l = new LoggerConfiguration()
25+
.MinimumLevel.Verbose()
26+
.WriteTo.Sink(new DelegatingSink(le => result = le))
27+
.CreateLogger();
28+
29+
writeAction(l);
30+
return result;
31+
}
32+
}
33+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Serilog.Events;
2+
3+
namespace Serilog.Enrichers.ClientInfo.Tests
4+
{
5+
internal static class Extensions
6+
{
7+
public static object LiteralValue(this LogEventPropertyValue @this)
8+
{
9+
return ((ScalarValue)@this).Value;
10+
}
11+
}
12+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
11+
<PackageReference Include="nsubstitute" Version="4.2.2" />
12+
<PackageReference Include="xunit" Version="2.4.1" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
<PrivateAssets>all</PrivateAssets>
16+
</PackageReference>
17+
<PackageReference Include="coverlet.collector" Version="3.1.0">
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
<PrivateAssets>all</PrivateAssets>
20+
</PackageReference>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\..\src\Serilog.Enrichers.ClientInfo\Serilog.Enrichers.ClientInfo.csproj" />
25+
</ItemGroup>
26+
27+
</Project>

0 commit comments

Comments
 (0)