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+ }
0 commit comments