Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauricio Dominguez committed Jan 10, 2020
1 parent 3cbe13c commit 3d8482b
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
{
RequestMessage = request;
RequestContent = await request.Content.ReadAsStringAsync();
return new HttpResponseMessage(_statusCodeToReturn);
return new HttpResponseMessage(_statusCodeToReturn)
{
Content = new StringContent("")
};
}

public void ReturnsStatusCode(HttpStatusCode statusCode)
Expand Down
70 changes: 70 additions & 0 deletions test/Honeycomb.Serilog.Sink.Tests/Helpers/Some.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;

using Serilog;
using Serilog.Events;

using Xunit.Sdk;

namespace Honeycomb.Serilog.Sink.Tests.Helpers
{
static class Some
{
public static LogEvent LogEvent(string messageTemplate, params object[] propertyValues)
{
return LogEvent(null, messageTemplate, propertyValues);
}

public static LogEvent LogEvent(Exception exception, string messageTemplate, params object[] propertyValues)
{
return LogEvent(LogEventLevel.Information, exception, messageTemplate, propertyValues);
}

public static LogEvent LogEvent(LogEventLevel level, Exception exception, string messageTemplate, params object[] propertyValues)
{
var log = new LoggerConfiguration().CreateLogger();
MessageTemplate template;
IEnumerable<LogEventProperty> properties;
#pragma warning disable Serilog004 // Constant MessageTemplate verifier
if (!log.BindMessageTemplate(messageTemplate, propertyValues, out template, out properties))
#pragma warning restore Serilog004 // Constant MessageTemplate verifier
{
throw new XunitException("Template could not be bound.");
}
return new LogEvent(DateTimeOffset.Now, level, exception, template, properties);
}

public static LogEvent LogEvent(LogEventLevel level, string messageTemplate, params object[] propertyValues)
{
var log = new LoggerConfiguration().CreateLogger();

#pragma warning disable Serilog004 // Constant MessageTemplate verifier
if (!log.BindMessageTemplate(messageTemplate, propertyValues, out var template, out var properties))
#pragma warning restore Serilog004 // Constant MessageTemplate verifier
{
throw new XunitException("Template could not be bound.");
}
return new LogEvent(DateTimeOffset.Now, level, null, template, properties);
}

public static LogEvent DebugEvent()
{
return LogEvent(LogEventLevel.Debug, null, "Debug event");
}

public static LogEvent InformationEvent()
{
return LogEvent(LogEventLevel.Information, null, "Information event");
}

public static LogEvent ErrorEvent()
{
return LogEvent(LogEventLevel.Error, null, "Error event");
}

public static string String()
{
return Guid.NewGuid().ToString("n");
}
}
}
13 changes: 11 additions & 2 deletions test/Honeycomb.Serilog.Sink.Tests/HoneycombSerilogSinkStub.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

using Serilog.Events;

namespace Honeycomb.Serilog.Sink.Tests
{
internal class HoneycombSerilogSinkStub : HoneycombSerilogSink
{
private readonly HttpClient _client;

public HoneycombSerilogSinkStub(HttpClient client, string teamId, string apiKey)
: base(teamId, apiKey, 1, TimeSpan.FromMilliseconds(1))
public HoneycombSerilogSinkStub(HttpClient client, string teamId, string apiKey, int batchSizeLimit, TimeSpan period)
: base(teamId, apiKey, batchSizeLimit, period)
{
_client = client;
}

protected override HttpClient Client => _client;

public Task EmitTestable(params LogEvent[] events)
{
return EmitBatchAsync(events);
}
}
}
90 changes: 54 additions & 36 deletions test/Honeycomb.Serilog.Sink.Tests/HoneycombSerilogSinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;

using FluentAssertions;
using FluentAssertions.Execution;

using Honeycomb.Serilog.Sink.Tests.Builders;
using Honeycomb.Serilog.Sink.Tests.Helpers;

using Serilog.Events;
using Serilog.Parsing;
Expand Down Expand Up @@ -42,7 +44,7 @@ public void Create_WhenInvalidApiKeyIsProvided_ThrowsArgumentException(string ap
}

[Fact]
public void Emit_AlwaysSendsApiKey()
public async Task Emit_AlwaysSendsApiKeyAsync()
{
const string teamId = nameof(teamId);
const string apiKey = nameof(apiKey);
Expand All @@ -51,14 +53,14 @@ public void Emit_AlwaysSendsApiKey()

var sut = CreateSut(teamId, apiKey, clientStub);

sut.Emit(new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, new MessageTemplate("", Enumerable.Empty<MessageTemplateToken>()), Enumerable.Empty<LogEventProperty>()));
await sut.EmitTestable(new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, new MessageTemplate("", Enumerable.Empty<MessageTemplateToken>()), Enumerable.Empty<LogEventProperty>()));

clientStub.RequestSubmitted.Headers.Should().ContainSingle(h => h.Key == "X-Honeycomb-Team");
clientStub.RequestSubmitted.Headers.GetValues("X-Honeycomb-Team").Should().ContainSingle().Which.Should().Be(apiKey);
}

[Fact]
public void Emit_CallsEndpointUsingTeamId()
public async Task Emit_CallsEndpointUsingTeamId()
{
const string teamId = nameof(teamId);
const string apiKey = nameof(apiKey);
Expand All @@ -67,13 +69,13 @@ public void Emit_CallsEndpointUsingTeamId()

var sut = CreateSut(teamId, apiKey, clientStub);

sut.Emit(new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, new MessageTemplate("", Enumerable.Empty<MessageTemplateToken>()), Enumerable.Empty<LogEventProperty>()));
await sut.EmitTestable(new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, new MessageTemplate("", Enumerable.Empty<MessageTemplateToken>()), Enumerable.Empty<LogEventProperty>()));

clientStub.RequestSubmitted.RequestUri.ToString().Should().EndWith(teamId);
}

[Fact]
public void Emit_GivenNoExceptionIsLogged_SerializesLogMessageAsJson_HasNoExceptionInMessage()
public async Task Emit_GivenNoExceptionIsLogged_SerializesLogMessageAsJson_HasNoExceptionInMessageAsync()
{
const string teamId = nameof(teamId);
const string apiKey = nameof(apiKey);
Expand All @@ -83,29 +85,34 @@ public void Emit_GivenNoExceptionIsLogged_SerializesLogMessageAsJson_HasNoExcept
var sut = CreateSut(teamId, apiKey, clientStub);

var level = LogEventLevel.Fatal;
var eventTime = DateTimeOffset.Now;

var messageTemplateParser = new MessageTemplateParser();
var messageTempalteString = "Testing message {message}";
var messageTemplate = messageTemplateParser.Parse(messageTempalteString);

sut.Emit(new LogEvent(eventTime, level, null, messageTemplate, Enumerable.Empty<LogEventProperty>()));
//sut.Emit(new LogEvent(eventTime, level, null, messageTemplate, new[] { new LogEventProperty("message", new ScalarValue("hello")), new LogEventProperty("message2", new ScalarValue("hello2")) }));
var eventToSend = Some.LogEvent(level, messageTempalteString);

await sut.EmitTestable(eventToSend);

var requestContent = clientStub.RequestContent;
using (var document = JsonDocument.Parse(requestContent))
using (new AssertionScope())
{
document.RootElement.GetProperty("level").GetString().Should().Be(level.ToString());
document.RootElement.GetProperty("timestamp").GetDateTimeOffset().Should().Be(eventTime);
document.RootElement.GetProperty("messageTemplate").GetString().Should().Be(messageTempalteString);
document.RootElement.TryGetProperty("exception", out var ex);
document.RootElement.ValueKind.Should().Be(JsonValueKind.Array);
document.RootElement.GetArrayLength().Should().Be(1);
JsonElement sentEvent = document.RootElement.EnumerateArray().Single();

sentEvent.GetProperty("time").GetDateTimeOffset().Should().Be(eventToSend.Timestamp);
sentEvent.GetProperty("data").ValueKind.Should().Be(JsonValueKind.Object);

JsonElement data = sentEvent.GetProperty("data");
data.GetProperty("level").GetString().Should().Be(level.ToString());
data.GetProperty("messageTemplate").GetString().Should().Be(messageTempalteString);
data.TryGetProperty("exception", out var ex);
ex.ValueKind.Should().Be(JsonValueKind.Undefined);
}
}

[Fact]
public void Emit_GivenAnExceptionToLog_SerializesLogMessageAsJson_IncludesExceptionInMessage()
public async Task Emit_GivenAnExceptionToLog_SerializesLogMessageAsJson_IncludesExceptionInMessageAsync()
{
const string teamId = nameof(teamId);
const string apiKey = nameof(apiKey);
Expand All @@ -115,28 +122,34 @@ public void Emit_GivenAnExceptionToLog_SerializesLogMessageAsJson_IncludesExcept
var sut = CreateSut(teamId, apiKey, clientStub);

var level = LogEventLevel.Fatal;
var eventTime = DateTimeOffset.Now;

var messageTemplateParser = new MessageTemplateParser();
var messageTempalteString = "Testing message {message}";
var messageTemplate = messageTemplateParser.Parse(messageTempalteString);
var ex = new Exception("TestException");

sut.Emit(new LogEvent(eventTime, level, ex, messageTemplate, Enumerable.Empty<LogEventProperty>()));
var eventToSend = Some.LogEvent(level, ex, messageTempalteString);

await sut.EmitTestable(eventToSend);

var requestContent = clientStub.RequestContent;
using (var document = JsonDocument.Parse(requestContent))
using (new AssertionScope())
{
document.RootElement.GetProperty("level").GetString().Should().Be(level.ToString());
document.RootElement.GetProperty("timestamp").GetDateTimeOffset().Should().Be(eventTime);
document.RootElement.GetProperty("messageTemplate").GetString().Should().Be(messageTempalteString);
document.RootElement.GetProperty("exception").GetString().Should().Be(ex.ToString());
document.RootElement.ValueKind.Should().Be(JsonValueKind.Array);
document.RootElement.GetArrayLength().Should().Be(1);
JsonElement sentEvent = document.RootElement.EnumerateArray().Single();

sentEvent.GetProperty("time").GetDateTimeOffset().Should().Be(eventToSend.Timestamp);
sentEvent.GetProperty("data").ValueKind.Should().Be(JsonValueKind.Object);
JsonElement data = sentEvent.GetProperty("data");

data.GetProperty("level").GetString().Should().Be(level.ToString());
data.GetProperty("messageTemplate").GetString().Should().Be(messageTempalteString);
data.GetProperty("exception").GetString().Should().Be(ex.ToString());
}
}

[Fact]
public void Emit_GivenAMessageWithProperties_SendsThemAll()
public async Task Emit_GivenAMessageWithProperties_SendsThemAllAsync()
{
const string teamId = nameof(teamId);
const string apiKey = nameof(apiKey);
Expand All @@ -146,30 +159,35 @@ public void Emit_GivenAMessageWithProperties_SendsThemAll()
var sut = CreateSut(teamId, apiKey, clientStub);

var level = LogEventLevel.Fatal;
var eventTime = DateTimeOffset.Now;

var messageTemplateParser = new MessageTemplateParser();
var messageTempalteString = "Testing message {message}";
var messageTemplate = messageTemplateParser.Parse(messageTempalteString);
var ex = new Exception("TestException");
const string property = nameof(property);

var messageTempalteString = $"Testing message property {{{nameof(property)}}}";

const string propertyName = nameof(propertyName);
const string propertyValue = nameof(propertyValue);
var properties = new LogEventProperty(propertyName, new ScalarValue(propertyValue));
var eventToSend = Some.LogEvent(level, messageTempalteString, property);

sut.Emit(new LogEvent(eventTime, level, ex, messageTemplate, new[] { properties }));
await sut.EmitTestable(eventToSend);

var requestContent = clientStub.RequestContent;
using (var document = JsonDocument.Parse(requestContent))
using (new AssertionScope())
{
document.RootElement.GetProperty(propertyName).GetString().Should().Be(propertyValue);
document.RootElement.ValueKind.Should().Be(JsonValueKind.Array);
document.RootElement.GetArrayLength().Should().Be(1);
JsonElement sentEvent = document.RootElement.EnumerateArray().Single();

sentEvent.GetProperty("time").GetDateTimeOffset().Should().Be(eventToSend.Timestamp);
sentEvent.GetProperty("data").ValueKind.Should().Be(JsonValueKind.Object);

JsonElement data = sentEvent.GetProperty("data");

data.GetProperty(nameof(property)).GetString().Should().Be(property);
}
}

private HoneycombSerilogSink CreateSut(string teamId, string apiKey, HttpClient client = null)
private HoneycombSerilogSinkStub CreateSut(string teamId, string apiKey, HttpClient client = null)
{
return new HoneycombSerilogSinkStub(client, teamId, apiKey);
return new HoneycombSerilogSinkStub(client, teamId, apiKey, 1, TimeSpan.FromMilliseconds(1));
}
}
}

0 comments on commit 3d8482b

Please sign in to comment.