Skip to content

Commit

Permalink
Extend unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashein committed Jun 8, 2024
1 parent 23dae1f commit 25961b7
Showing 1 changed file with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,54 @@ public void EnrichLogWithCorrelationId_WhenHttpRequestNotContainCorrelationHeade
Assert.NotNull(evt.Properties[LogPropertyName].LiteralValue().ToString());
}

[Fact]
public void EnrichLogWithCorrelationId_WhenHttpResponseContainsCorrelationIdHeader_ShouldCreateCorrelationIdProperty()
{
// Arrange
var correlationId = Guid.NewGuid().ToString();
_contextAccessor.HttpContext.Response.Headers.Add(HeaderKey, correlationId);
var correlationIdEnricher = new CorrelationIdEnricher(HeaderKey, false, _contextAccessor);

LogEvent evt = null;
var log = new LoggerConfiguration()
.Enrich.With(correlationIdEnricher)
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

// Act
log.Information(@"Has a correlation id.");

// Assert
Assert.NotNull(evt);
Assert.True(evt.Properties.ContainsKey(LogPropertyName));
Assert.Equal(correlationId, evt.Properties[LogPropertyName].LiteralValue().ToString());
}

[Fact]
public void EnrichLogWithCorrelationId_WhenHttpRequestAndResponseContainCorrelationIdHeader_ShouldCreateCorrelationIdPropertyFromHttpRequest()
{
// Arrange
var requestCorrelationId = Guid.NewGuid().ToString();
var responseCorrelationId = Guid.NewGuid().ToString();
_contextAccessor.HttpContext.Request.Headers.Add(HeaderKey, requestCorrelationId);
_contextAccessor.HttpContext.Response.Headers.Add(HeaderKey, responseCorrelationId);
var correlationIdEnricher = new CorrelationIdEnricher(HeaderKey, false, _contextAccessor);

LogEvent evt = null;
var log = new LoggerConfiguration()
.Enrich.With(correlationIdEnricher)
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

// Act
log.Information(@"Has a correlation id.");

// Assert
Assert.NotNull(evt);
Assert.True(evt.Properties.ContainsKey(LogPropertyName));
Assert.Equal(requestCorrelationId, evt.Properties[LogPropertyName].LiteralValue().ToString());
}

[Fact]
public void WithClientIp_ThenLoggerIsCalled_ShouldNotThrowException()
{
Expand Down

0 comments on commit 25961b7

Please sign in to comment.