From 7ad2b5185b34406b0cded01b32399875596201e6 Mon Sep 17 00:00:00 2001 From: GHXX Date: Sat, 13 Jan 2024 19:07:00 +0100 Subject: [PATCH] add log output checking in tests --- SimpleHttpServerTest/SimpleServerTest.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/SimpleHttpServerTest/SimpleServerTest.cs b/SimpleHttpServerTest/SimpleServerTest.cs index 9c38237..866080d 100644 --- a/SimpleHttpServerTest/SimpleServerTest.cs +++ b/SimpleHttpServerTest/SimpleServerTest.cs @@ -1,6 +1,5 @@ using SimpleHttpServer; using System.Net; -using System.Runtime.CompilerServices; namespace SimpleHttpServerTest; @@ -11,6 +10,7 @@ public class SimpleServerTest { private HttpServer? activeServer = null; private HttpClient? activeHttpClient = null; + private bool failOnLogError = true; private static string GetRequestPath(string url) => $"http://localhost:{PORT}/{url.TrimStart('/')}"; private async Task RequestGetStringAsync(string path) => await activeHttpClient!.GetStringAsync(GetRequestPath(path)); private async Task AssertGetStatusCodeAsync(string path, HttpStatusCode statusCode) { @@ -21,11 +21,18 @@ public class SimpleServerTest { [TestInitialize] public void Init() { - var conf = new SimpleHttpServerConfiguration(); + var conf = new SimpleHttpServerConfiguration() { + DisableLogMessagePrinting = false, + LogMessageHandler = (LogOutputTopic topic, string message, LogOutputLevel logLevel) => { + if (failOnLogError && logLevel is LogOutputLevel.Error or LogOutputLevel.Fatal) + Assert.Fail($"An error was thrown in the log output:\n{topic} {message}"); + } + }; if (activeServer != null) throw new InvalidOperationException("Tried to create another httpserver instance when an existing one was already running."); Console.WriteLine("Starting server..."); + failOnLogError = true; activeServer = new HttpServer(PORT, conf); activeServer.RegisterEndpointsFromType(); activeServer.Start();