add log output checking in tests

This commit is contained in:
GHXX 2024-01-13 19:07:00 +01:00
parent 08003d1fc3
commit 7ad2b5185b

View File

@ -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<HttpResponseMessage> 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<TestEndpoints>();
activeServer.Start();