Skip to content

Commit 25961b7

Browse files
committed
Extend unit tests
1 parent 23dae1f commit 25961b7

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,54 @@ public void EnrichLogWithCorrelationId_WhenHttpRequestNotContainCorrelationHeade
108108
Assert.NotNull(evt.Properties[LogPropertyName].LiteralValue().ToString());
109109
}
110110

111+
[Fact]
112+
public void EnrichLogWithCorrelationId_WhenHttpResponseContainsCorrelationIdHeader_ShouldCreateCorrelationIdProperty()
113+
{
114+
// Arrange
115+
var correlationId = Guid.NewGuid().ToString();
116+
_contextAccessor.HttpContext.Response.Headers.Add(HeaderKey, correlationId);
117+
var correlationIdEnricher = new CorrelationIdEnricher(HeaderKey, false, _contextAccessor);
118+
119+
LogEvent evt = null;
120+
var log = new LoggerConfiguration()
121+
.Enrich.With(correlationIdEnricher)
122+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
123+
.CreateLogger();
124+
125+
// Act
126+
log.Information(@"Has a correlation id.");
127+
128+
// Assert
129+
Assert.NotNull(evt);
130+
Assert.True(evt.Properties.ContainsKey(LogPropertyName));
131+
Assert.Equal(correlationId, evt.Properties[LogPropertyName].LiteralValue().ToString());
132+
}
133+
134+
[Fact]
135+
public void EnrichLogWithCorrelationId_WhenHttpRequestAndResponseContainCorrelationIdHeader_ShouldCreateCorrelationIdPropertyFromHttpRequest()
136+
{
137+
// Arrange
138+
var requestCorrelationId = Guid.NewGuid().ToString();
139+
var responseCorrelationId = Guid.NewGuid().ToString();
140+
_contextAccessor.HttpContext.Request.Headers.Add(HeaderKey, requestCorrelationId);
141+
_contextAccessor.HttpContext.Response.Headers.Add(HeaderKey, responseCorrelationId);
142+
var correlationIdEnricher = new CorrelationIdEnricher(HeaderKey, false, _contextAccessor);
143+
144+
LogEvent evt = null;
145+
var log = new LoggerConfiguration()
146+
.Enrich.With(correlationIdEnricher)
147+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
148+
.CreateLogger();
149+
150+
// Act
151+
log.Information(@"Has a correlation id.");
152+
153+
// Assert
154+
Assert.NotNull(evt);
155+
Assert.True(evt.Properties.ContainsKey(LogPropertyName));
156+
Assert.Equal(requestCorrelationId, evt.Properties[LogPropertyName].LiteralValue().ToString());
157+
}
158+
111159
[Fact]
112160
public void WithClientIp_ThenLoggerIsCalled_ShouldNotThrowException()
113161
{

0 commit comments

Comments
 (0)