|
| 1 | +using Serilog.Events; |
| 2 | +using Serilog.Parsing; |
| 3 | +using Serilog.Sinks.Splunk; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Net; |
| 7 | +using System.Net.Http; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Serilog.Sinks.Splunk.Tests |
| 13 | +{ |
| 14 | + public class EventCollectorSinkTests |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// A fake HttpMessageHandler that records disposal and captures outgoing requests. |
| 18 | + /// </summary> |
| 19 | + private class FakeHandler : HttpMessageHandler |
| 20 | + { |
| 21 | + public bool Disposed { get; private set; } |
| 22 | + public HttpRequestMessage LastRequest { get; private set; } |
| 23 | + public HttpStatusCode ResponseStatusCode { get; set; } = HttpStatusCode.OK; |
| 24 | + |
| 25 | + protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 26 | + { |
| 27 | + LastRequest = request; |
| 28 | + return Task.FromResult(new HttpResponseMessage(ResponseStatusCode)); |
| 29 | + } |
| 30 | + |
| 31 | + protected override void Dispose(bool disposing) |
| 32 | + { |
| 33 | + Disposed = true; |
| 34 | + base.Dispose(disposing); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void DisposingSinkDisposesOwnedHttpResources() |
| 40 | + { |
| 41 | + var handler = new FakeHandler(); |
| 42 | + var sink = new EventCollectorSink( |
| 43 | + "http://splunk.example.com:8088", |
| 44 | + "test-token", |
| 45 | + null, |
| 46 | + new SplunkJsonFormatter(false, true, null), |
| 47 | + handler); |
| 48 | + |
| 49 | + sink.Dispose(); |
| 50 | + |
| 51 | + Assert.True(handler.Disposed, "HttpMessageHandler should be disposed when sink is disposed"); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public void DisposingSinkTwiceDoesNotThrow() |
| 56 | + { |
| 57 | + var handler = new FakeHandler(); |
| 58 | + var sink = new EventCollectorSink( |
| 59 | + "http://splunk.example.com:8088", |
| 60 | + "test-token", |
| 61 | + null, |
| 62 | + new SplunkJsonFormatter(false, true, null), |
| 63 | + handler); |
| 64 | + |
| 65 | + sink.Dispose(); |
| 66 | + sink.Dispose(); // Should not throw |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public async Task EmitBatchSendsRequestWithCorrectAuthHeaders() |
| 71 | + { |
| 72 | + var handler = new FakeHandler(); |
| 73 | + var sink = new EventCollectorSink( |
| 74 | + "http://splunk.example.com:8088", |
| 75 | + "my-secret-token", |
| 76 | + "services/collector/event", |
| 77 | + new SplunkJsonFormatter(false, true, null), |
| 78 | + handler); |
| 79 | + |
| 80 | + var logEvent = new LogEvent( |
| 81 | + DateTimeOffset.UtcNow, |
| 82 | + LogEventLevel.Information, |
| 83 | + null, |
| 84 | + new MessageTemplate("Test", new List<MessageTemplateToken>()), |
| 85 | + new LogEventProperty[] { }); |
| 86 | + |
| 87 | + await sink.EmitBatchAsync(new[] { logEvent }); |
| 88 | + |
| 89 | + Assert.NotNull(handler.LastRequest); |
| 90 | + Assert.Equal("Splunk", handler.LastRequest.Headers.Authorization?.Scheme); |
| 91 | + Assert.Equal("my-secret-token", handler.LastRequest.Headers.Authorization?.Parameter); |
| 92 | + Assert.True(handler.LastRequest.Headers.Contains("X-Splunk-Request-Channel")); |
| 93 | + |
| 94 | + sink.Dispose(); |
| 95 | + } |
| 96 | + |
| 97 | + [Fact] |
| 98 | + public void DisposingLoggerDisposesHttpResources() |
| 99 | + { |
| 100 | + var handler = new FakeHandler(); |
| 101 | + |
| 102 | + var logger = new LoggerConfiguration() |
| 103 | + .WriteTo.EventCollector( |
| 104 | + "http://splunk.example.com:8088", |
| 105 | + "test-token", |
| 106 | + new SplunkJsonFormatter(false, true, null), |
| 107 | + messageHandler: handler) |
| 108 | + .CreateLogger(); |
| 109 | + |
| 110 | + logger.Information("Test event"); |
| 111 | + |
| 112 | + // Logger disposal should cascade to sink disposal |
| 113 | + logger.Dispose(); |
| 114 | + |
| 115 | + Assert.True(handler.Disposed, "HttpMessageHandler should be disposed when logger is disposed"); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments