From 3b21a0fbf16439c3c00c8917a95655e61cadc69f Mon Sep 17 00:00:00 2001 From: Joseph Woodward Date: Sat, 12 Jan 2019 00:42:15 +0000 Subject: [PATCH] More tests --- docker-compose.yaml | 31 ++++++++++++++++ src/Serilog.Sinks.Loki/LokiHttpClient.cs | 22 +++++++----- src/Serilog.Sinks.Loki/LokiSinkExtensions.cs | 2 +- ...ontent.ContentMatchesApproved.approved.txt | 1 + .../HttpClientTests/PostContent.cs | 36 +++++++++++++++++++ test/Serilog.Sinks.Loki.Tests/PostBody.cs | 7 ++++ .../Serilog.Sinks.Loki.Tests.csproj | 2 +- 7 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 docker-compose.yaml create mode 100644 test/Serilog.Sinks.Loki.Tests/HttpClientTests/PostContent.ContentMatchesApproved.approved.txt create mode 100644 test/Serilog.Sinks.Loki.Tests/HttpClientTests/PostContent.cs create mode 100644 test/Serilog.Sinks.Loki.Tests/PostBody.cs diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..8ebb4cd --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,31 @@ +version: "3" + +networks: + loki: + +services: + loki: + image: grafana/loki:master + ports: + - "3100:3100" + command: -config.file=/etc/loki/local-config.yaml + networks: + - loki + + promtail: + image: grafana/promtail:master + volumes: + - /var/log:/var/log + command: + -config.file=/etc/promtail/docker-config.yaml + networks: + - loki + + grafana: + image: grafana/grafana:master + ports: + - "3000:3000" + environment: + GF_EXPLORE_ENABLED: "true" + networks: + - loki diff --git a/src/Serilog.Sinks.Loki/LokiHttpClient.cs b/src/Serilog.Sinks.Loki/LokiHttpClient.cs index abbcd19..0700d25 100644 --- a/src/Serilog.Sinks.Loki/LokiHttpClient.cs +++ b/src/Serilog.Sinks.Loki/LokiHttpClient.cs @@ -1,3 +1,4 @@ +using System; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; @@ -13,9 +14,6 @@ public class LokiHttpClient : IHttpClient public LokiHttpClient(HttpClient httpClient = null) { HttpClient = httpClient ?? new HttpClient(); - HttpClient.DefaultRequestHeaders - .Accept - .Add(new MediaTypeWithQualityHeaderValue("application/json")); } public void SetAuthCredentials(LokiCredentials credentials) @@ -24,16 +22,22 @@ public void SetAuthCredentials(LokiCredentials credentials) return; var headers = HttpClient.DefaultRequestHeaders; - if (headers.All(x => x.Key != "Authorization")) - { - var token = Base64Encode($"{c.Username}:{c.Password}"); - headers.Add("Authorization", $"Basic {token}"); - } + if (headers.Any(x => x.Key == "Authorization")) + return; + + var token = Base64Encode($"{c.Username}:{c.Password}"); + headers.Add("Authorization", $"Basic {token}"); } public virtual Task PostAsync(string requestUri, HttpContent content) { + content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); return HttpClient.PostAsync(requestUri, content); + +/* var r = content.ReadAsStringAsync().Result; + var result = await HttpClient.PostAsync(requestUri, content); + var body = result.Content.ReadAsStringAsync().Result; //right! + return result;*/ } public virtual void Dispose() @@ -42,7 +46,7 @@ public virtual void Dispose() private static string Base64Encode(string plainText) { var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); - return System.Convert.ToBase64String(plainTextBytes); + return Convert.ToBase64String(plainTextBytes); } } } \ No newline at end of file diff --git a/src/Serilog.Sinks.Loki/LokiSinkExtensions.cs b/src/Serilog.Sinks.Loki/LokiSinkExtensions.cs index a7f2280..b72e837 100644 --- a/src/Serilog.Sinks.Loki/LokiSinkExtensions.cs +++ b/src/Serilog.Sinks.Loki/LokiSinkExtensions.cs @@ -18,7 +18,7 @@ private static LoggerConfiguration LokiHttpImpl(this LoggerSinkConfiguration sin { c.SetAuthCredentials(credentials); } - + return sinkConfiguration.Http(LokiRouteBuilder.BuildPostUri(credentials.Url), batchFormatter: formatter, httpClient: client); } } diff --git a/test/Serilog.Sinks.Loki.Tests/HttpClientTests/PostContent.ContentMatchesApproved.approved.txt b/test/Serilog.Sinks.Loki.Tests/HttpClientTests/PostContent.ContentMatchesApproved.approved.txt new file mode 100644 index 0000000..43cc694 --- /dev/null +++ b/test/Serilog.Sinks.Loki.Tests/HttpClientTests/PostContent.ContentMatchesApproved.approved.txt @@ -0,0 +1 @@ +{"streams":[{"labels":"{level=\"error\"}","entries":[{"ts":"","line":"Something's wrong"}]}]} diff --git a/test/Serilog.Sinks.Loki.Tests/HttpClientTests/PostContent.cs b/test/Serilog.Sinks.Loki.Tests/HttpClientTests/PostContent.cs new file mode 100644 index 0000000..189b57d --- /dev/null +++ b/test/Serilog.Sinks.Loki.Tests/HttpClientTests/PostContent.cs @@ -0,0 +1,36 @@ +using System.Text.RegularExpressions; +using Serilog.Sinks.Loki.Tests.Infrastructure; +using Shouldly; +using Xunit; + +namespace Serilog.Sinks.Loki.Tests.HttpClientTests +{ + public class PostContent : IClassFixture + { + private readonly TestHttpClient _client; + + public PostContent() + { + _client = new TestHttpClient(); + } + + [Fact] + public void ContentMatchesApproved() + { + // Arrange + var credentials = new NoAuthCredentials("http://test:80"); + var log = new LoggerConfiguration() + .MinimumLevel.Information() + .WriteTo.LokiHttp(credentials, httpClient: _client) + .CreateLogger(); + + // Act + log.Error("Something's wrong"); + log.Dispose(); + + // Assert + _client.Content.ShouldMatchApproved(x => x.WithScrubber(s => Regex.Replace(s, + @"\d{1,2}\d{1,2}\d{2,4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{1,2}:\d{1,2}.\d{1,7}\+\d{2}:\d{2}", ""))); + } + } +} \ No newline at end of file diff --git a/test/Serilog.Sinks.Loki.Tests/PostBody.cs b/test/Serilog.Sinks.Loki.Tests/PostBody.cs new file mode 100644 index 0000000..8ada184 --- /dev/null +++ b/test/Serilog.Sinks.Loki.Tests/PostBody.cs @@ -0,0 +1,7 @@ +namespace Serilog.Sinks.Loki.Tests +{ + public class Response + { + + } +} \ No newline at end of file diff --git a/test/Serilog.Sinks.Loki.Tests/Serilog.Sinks.Loki.Tests.csproj b/test/Serilog.Sinks.Loki.Tests/Serilog.Sinks.Loki.Tests.csproj index 5daec25..c299983 100644 --- a/test/Serilog.Sinks.Loki.Tests/Serilog.Sinks.Loki.Tests.csproj +++ b/test/Serilog.Sinks.Loki.Tests/Serilog.Sinks.Loki.Tests.csproj @@ -11,7 +11,7 @@ - +