Skip to content

Commit 5569d40

Browse files
Unifying tests.
1 parent eb8ee58 commit 5569d40

File tree

7 files changed

+181
-156
lines changed

7 files changed

+181
-156
lines changed

src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@ public class ClientIpEnricher : ILogEventEnricher
2020
{
2121
private const string IpAddressPropertyName = "ClientIp";
2222
private const string IpAddressItemKey = "Serilog_ClientIp";
23+
private readonly string _forwardHeaderKey;
2324

2425
private readonly IHttpContextAccessor _contextAccessor;
2526

26-
public ClientIpEnricher()
27+
public ClientIpEnricher(string forwardHeaderKey)
28+
: this(forwardHeaderKey, new HttpContextAccessor())
2729
{
28-
_contextAccessor = new HttpContextAccessor();
2930
}
3031

31-
internal ClientIpEnricher(IHttpContextAccessor contextAccessor)
32+
internal ClientIpEnricher(string forwardHeaderKey, IHttpContextAccessor contextAccessor)
3233
{
34+
_forwardHeaderKey = forwardHeaderKey;
3335
_contextAccessor = contextAccessor;
3436
}
3537

@@ -70,7 +72,7 @@ private string GetIpAddress()
7072
#else
7173
private string GetIpAddress()
7274
{
73-
var ipAddress = _contextAccessor.HttpContext?.Request?.Headers[ClinetIpConfiguration.XForwardHeaderName].FirstOrDefault();
75+
var ipAddress = _contextAccessor.HttpContext?.Request?.Headers[_forwardHeaderKey].FirstOrDefault();
7476

7577
if (!string.IsNullOrEmpty(ipAddress))
7678
return GetIpAddressFromProxy(ipAddress);
@@ -85,7 +87,8 @@ private string GetIpAddressFromProxy(string proxifiedIpList)
8587

8688
if (addresses.Length != 0)
8789
{
88-
// If IP contains port, it will be after the last : (IPv6 uses : as delimiter and could have more of them)
90+
// If IP contains port, it will be after the last : (IPv6 uses : as delimiter and
91+
// could have more of them)
8992
return addresses[0].Contains(":")
9093
? addresses[0].Substring(0, addresses[0].LastIndexOf(":", StringComparison.Ordinal))
9194
: addresses[0];

src/Serilog.Enrichers.ClientInfo/Enrichers/ClinetIpConfiguration.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/Serilog.Enrichers.ClientInfo.Tests/ClientHeaderEnricherTests.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public ClientHeaderEnricherTests()
1818
}
1919

2020
[Fact]
21-
public void EnrichLogWithClientHeaderEnricher_WhenHttpRequestContainHeader_ShouldCreateHeaderValueProperty()
21+
public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateHeaderValueProperty()
2222
{
2323
// Arrange
2424
var headerKey = "RequestId";
@@ -48,7 +48,7 @@ public void EnrichLogWithMulitpleClientHeaderEnricher_WhenHttpRequestContainHead
4848
{
4949
// Arrange
5050
var headerKey1 = "Header1";
51-
var headerKey2 = "Header2";
51+
var headerKey2 = "Header-2";
5252
var headerValue1 = Guid.NewGuid().ToString();
5353
var headerValue2 = Guid.NewGuid().ToString();
5454
_contextAccessor.HttpContext.Request.Headers.Add(headerKey1, headerValue1);
@@ -72,12 +72,12 @@ public void EnrichLogWithMulitpleClientHeaderEnricher_WhenHttpRequestContainHead
7272
Assert.NotNull(evt);
7373
Assert.True(evt.Properties.ContainsKey(headerKey1));
7474
Assert.Equal(headerValue1, evt.Properties[headerKey1].LiteralValue().ToString());
75-
Assert.True(evt.Properties.ContainsKey(headerKey2));
76-
Assert.Equal(headerValue2, evt.Properties[headerKey2].LiteralValue().ToString());
75+
Assert.True(evt.Properties.ContainsKey(headerKey2.Replace("-", "")));
76+
Assert.Equal(headerValue2, evt.Properties[headerKey2.Replace("-", "")].LiteralValue().ToString());
7777
}
7878

7979
[Fact]
80-
public void EnrichLogWithClientHeaderEnricher_WhenHttpRequestNotContainHeader_ShouldCreateHeaderValuePropertyWithNoValue()
80+
public void EnrichLogWithClientHeader_WhenHttpRequestNotContainHeader_ShouldCreateHeaderValuePropertyWithNoValue()
8181
{
8282
// Arrange
8383
var headerKey = "RequestId";
@@ -98,4 +98,20 @@ public void EnrichLogWithClientHeaderEnricher_WhenHttpRequestNotContainHeader_Sh
9898
Assert.True(evt.Properties.ContainsKey(headerKey));
9999
Assert.Null(evt.Properties[headerKey].LiteralValue());
100100
}
101+
102+
[Fact]
103+
public void WithRequestHeader_ThenLoggerIsCalled_ShouldNotThrowException()
104+
{
105+
// Arrange
106+
var logger = new LoggerConfiguration()
107+
.Enrich.WithRequestHeader("HeaderName")
108+
.WriteTo.Sink(new DelegatingSink(e => { }))
109+
.CreateLogger();
110+
111+
// Act
112+
var exception = Record.Exception(() => logger.Information("LOG"));
113+
114+
// Assert
115+
Assert.Null(exception);
116+
}
101117
}

test/Serilog.Enrichers.ClientInfo.Tests/ClientIpEnricherTests.cs

Lines changed: 121 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -4,113 +4,128 @@
44
using System.Net;
55
using Xunit;
66

7-
namespace Serilog.Enrichers.ClientInfo.Tests
7+
namespace Serilog.Enrichers.ClientInfo.Tests;
8+
9+
public class ClientIpEnricherTests
810
{
9-
public class ClientIpEnricherTests
11+
private const string ForwardHeaderKey = "x-forwarded-for";
12+
private readonly IHttpContextAccessor _contextAccessor;
13+
14+
public ClientIpEnricherTests()
15+
{
16+
var httpContext = new DefaultHttpContext();
17+
_contextAccessor = Substitute.For<IHttpContextAccessor>();
18+
_contextAccessor.HttpContext.Returns(httpContext);
19+
}
20+
21+
[Fact]
22+
public void EnrichLogWithClientIp_ShouldCreateClientIpPropertyWithValue()
23+
{
24+
// Arrange
25+
_contextAccessor.HttpContext.Connection.RemoteIpAddress = IPAddress.Parse("::1");
26+
27+
var ipEnricher = new ClientIpEnricher(ForwardHeaderKey, _contextAccessor);
28+
29+
LogEvent evt = null;
30+
var log = new LoggerConfiguration()
31+
.Enrich.With(ipEnricher)
32+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
33+
.CreateLogger();
34+
35+
// Act
36+
log.Information(@"Has an IP property");
37+
38+
// Assert
39+
Assert.NotNull(evt);
40+
Assert.True(evt.Properties.ContainsKey("ClientIp"));
41+
Assert.Equal("::1", evt.Properties["ClientIp"].LiteralValue());
42+
}
43+
44+
[Fact]
45+
public void EnrichLogWithClientIp_WhenLogMoreThanOnce_ShouldReadClientIpValueFromHttpContextItems()
1046
{
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(ClinetIpConfiguration.XForwardHeaderName, 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_With_IpEnricher_With_Custom_XForwardHeader_AndRequest_Contain_ForwardHeader_Should_Read_ClientIp_Value_From_Header_Value()
92-
{
93-
//Arrange
94-
const string customForwardHeader = "CustomForwardHeader";
95-
_contextAccessor.HttpContext.Connection.RemoteIpAddress = IPAddress.Loopback;
96-
_contextAccessor.HttpContext.Request.Headers.Add(customForwardHeader, IPAddress.Broadcast.ToString());
97-
98-
var ipEnricher = new ClientIpEnricher(_contextAccessor);
99-
ClinetIpConfiguration.XForwardHeaderName = customForwardHeader;
100-
101-
LogEvent evt = null;
102-
var log = new LoggerConfiguration()
103-
.Enrich.With(ipEnricher)
104-
.WriteTo.Sink(new DelegatingSink(e => evt = e))
105-
.CreateLogger();
106-
107-
// Act
108-
log.Information(@"Has an IP property");
109-
110-
// Assert
111-
Assert.NotNull(evt);
112-
Assert.True(evt.Properties.ContainsKey("ClientIp"));
113-
Assert.Equal(IPAddress.Broadcast.ToString(), evt.Properties["ClientIp"].LiteralValue());
114-
}
47+
//Arrange
48+
_contextAccessor.HttpContext.Connection.RemoteIpAddress = IPAddress.Loopback;
49+
var ipEnricher = new ClientIpEnricher(ForwardHeaderKey, _contextAccessor);
50+
51+
LogEvent evt = null;
52+
var log = new LoggerConfiguration()
53+
.Enrich.With(ipEnricher)
54+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
55+
.CreateLogger();
56+
57+
// Act
58+
log.Information(@"Has an IP property");
59+
log.Information(@"Has an other IP property");
60+
61+
// Assert
62+
Assert.NotNull(evt);
63+
Assert.True(evt.Properties.ContainsKey("ClientIp"));
64+
Assert.Equal(IPAddress.Loopback.ToString(), evt.Properties["ClientIp"].LiteralValue());
65+
}
66+
67+
[Fact]
68+
public void EnrichLogWithClientIp_WhenRequestContainForwardHeader_ShouldCreateClientIpPropertyWithValue()
69+
{
70+
//Arrange
71+
_contextAccessor.HttpContext.Connection.RemoteIpAddress = IPAddress.Loopback;
72+
_contextAccessor.HttpContext.Request.Headers.Add(ForwardHeaderKey, IPAddress.Broadcast.ToString());
73+
74+
var ipEnricher = new ClientIpEnricher(ForwardHeaderKey, _contextAccessor);
75+
76+
LogEvent evt = null;
77+
var log = new LoggerConfiguration()
78+
.Enrich.With(ipEnricher)
79+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
80+
.CreateLogger();
81+
82+
// Act
83+
log.Information(@"Has an IP property");
84+
85+
// Assert
86+
Assert.NotNull(evt);
87+
Assert.True(evt.Properties.ContainsKey("ClientIp"));
88+
Assert.Equal(IPAddress.Broadcast.ToString(), evt.Properties["ClientIp"].LiteralValue());
89+
}
90+
91+
[Fact]
92+
public void EnrichLogWithClientIp_WithCustomForwardHeaderAndRequest_ShouldCreateClientIpPropertyWithValue()
93+
{
94+
//Arrange
95+
const string customForwardHeader = "CustomForwardHeader";
96+
_contextAccessor.HttpContext.Connection.RemoteIpAddress = IPAddress.Loopback;
97+
_contextAccessor.HttpContext.Request.Headers.Add(customForwardHeader, IPAddress.Broadcast.ToString());
98+
99+
var ipEnricher = new ClientIpEnricher(customForwardHeader, _contextAccessor);
100+
101+
LogEvent evt = null;
102+
var log = new LoggerConfiguration()
103+
.Enrich.With(ipEnricher)
104+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
105+
.CreateLogger();
106+
107+
// Act
108+
log.Information(@"Has an IP property");
109+
110+
// Assert
111+
Assert.NotNull(evt);
112+
Assert.True(evt.Properties.ContainsKey("ClientIp"));
113+
Assert.Equal(IPAddress.Broadcast.ToString(), evt.Properties["ClientIp"].LiteralValue());
114+
}
115+
116+
[Fact]
117+
public void WithClientIp_ThenLoggerIsCalled_ShouldNotThrowException()
118+
{
119+
// Arrange
120+
var logger = new LoggerConfiguration()
121+
.Enrich.WithClientIp()
122+
.WriteTo.Sink(new DelegatingSink(e => { }))
123+
.CreateLogger();
124+
125+
// Act
126+
var exception = Record.Exception(() => logger.Information("LOG"));
127+
128+
// Assert
129+
Assert.Null(exception);
115130
}
116131
}

test/Serilog.Enrichers.ClientInfo.Tests/CorrelationIdEnricherTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public CorrelationIdEnricherTests()
1919
}
2020

2121
[Fact]
22-
public void EnrichLogWithCorrelationIdEnricher_WhenHttpRequestContainCorrelationHeader_ShouldCreateCorrelationIdProperty()
22+
public void EnrichLogWithCorrelationId_WhenHttpRequestContainCorrelationHeader_ShouldCreateCorrelationIdProperty()
2323
{
2424
// Arrange
2525
var correlationId = Guid.NewGuid().ToString();
@@ -43,7 +43,7 @@ public void EnrichLogWithCorrelationIdEnricher_WhenHttpRequestContainCorrelation
4343
}
4444

4545
[Fact]
46-
public void EnrichLogWithCorrelationIdEnricher_WhenHttpRequestContainCorrelationHeader_ShouldCreateCorrelationIdPropertyHasValue()
46+
public void EnrichLogWithCorrelationId_WhenHttpRequestContainCorrelationHeader_ShouldCreateCorrelationIdPropertyHasValue()
4747
{
4848
// Arrange
4949
var correlationId = Guid.NewGuid().ToString();
@@ -67,7 +67,7 @@ public void EnrichLogWithCorrelationIdEnricher_WhenHttpRequestContainCorrelation
6767
}
6868

6969
[Fact]
70-
public void EnrichLogWithCorrelationIdEnricher_WhenHttpRequestNotContainCorrelationHeaderAndAddDefaultValueIsFalse_ShouldCreateCorrelationIdPropertyWithNoValue()
70+
public void EnrichLogWithCorrelationId_WhenHttpRequestNotContainCorrelationHeaderAndAddDefaultValueIsFalse_ShouldCreateCorrelationIdPropertyWithNoValue()
7171
{
7272
// Arrange
7373
var correlationIdEnricher = new CorrelationIdEnricher(HeaderKey, false, _contextAccessor);
@@ -88,7 +88,7 @@ public void EnrichLogWithCorrelationIdEnricher_WhenHttpRequestNotContainCorrelat
8888
}
8989

9090
[Fact]
91-
public void EnrichLogWithCorrelationIdEnricher_WhenHttpRequestNotContainCorrelationHeaderAndAddDefaultValueIsTrue_ShouldCreateCorrelationIdPropertyHasValue()
91+
public void EnrichLogWithCorrelationId_WhenHttpRequestNotContainCorrelationHeaderAndAddDefaultValueIsTrue_ShouldCreateCorrelationIdPropertyHasValue()
9292
{
9393
// Arrange
9494
var correlationIdEnricher = new CorrelationIdEnricher(HeaderKey, true, _contextAccessor);
@@ -109,7 +109,7 @@ public void EnrichLogWithCorrelationIdEnricher_WhenHttpRequestNotContainCorrelat
109109
}
110110

111111
[Fact]
112-
public void WithCorrelationId_ThenLoggerIsCalled_ShouldNotThrowException()
112+
public void WithClientIp_ThenLoggerIsCalled_ShouldNotThrowException()
113113
{
114114
// Arrange
115115
var logger = new LoggerConfiguration()

0 commit comments

Comments
 (0)