using SimpleHttpServer; using System.Net; namespace SimpleHttpServerTest; [TestClass] public class SimpleServerTest { const int PORT = 8833; 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) { var resp = await activeHttpClient!.GetAsync(GetRequestPath(path)); Assert.AreEqual(statusCode, resp.StatusCode); return resp; } [TestInitialize] public void Init() { 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(); activeHttpClient = new HttpClient(); Console.WriteLine("Server started."); } [TestCleanup] public async Task Cleanup() { var ctokSrc = new CancellationTokenSource(TimeSpan.FromMinutes(2)); if (activeServer == null) { throw new InvalidOperationException("Tried to shut down server when an existing one wasnt runnign yet"); } await Console.Out.WriteLineAsync("Shutting down server..."); await activeServer.StopAsync(ctokSrc.Token); activeHttpClient?.Dispose(); activeHttpClient = null; await Console.Out.WriteLineAsync("Shutdown finished."); } static string GetHttpPageContentFromPrefix(string page) => $"It works!!!!!!56sg5sdf46a4sd65a412f31sdfgdf89h74g9f8h4as56d4f56as2as1f3d24f87g9d87{page}"; [TestMethod] public async Task CheckSimpleServe() { var resp = await AssertGetStatusCodeAsync("/", HttpStatusCode.OK); var str = await resp.Content.ReadAsStringAsync(); Assert.AreEqual("It works!", str); } [TestMethod] public async Task CheckMultiServe() { foreach (var item in "index2.html;testpage;testpage2;testpage3".Split(';')) { await Console.Out.WriteLineAsync($"Checking page: /{item}"); var resp = await AssertGetStatusCodeAsync(item, HttpStatusCode.OK); var str = await resp.Content.ReadAsStringAsync(); Assert.AreEqual(GetHttpPageContentFromPrefix(item), str); } } [TestMethod] public async Task CheckQueryArgs() { foreach (var a1 in "test1;longstring2;something else with a space".Split(';')) { foreach (var a2 in new[] { -10, 2, -2, 5, 0, 4 }) { foreach (var a3 in new[] { -1, 9, 2, -20, 0 }) { var resp = await AssertGetStatusCodeAsync($"returnqueries?arg1={a1}&arg2={a2}&arg3={a3}", HttpStatusCode.OK); var str = await resp.Content.ReadAsStringAsync(); Assert.AreEqual(TestEndpoints.GetReturnQueryPageResult(a1, a2, a3), str); } } } } public class TestEndpoints { [HttpEndpoint(HttpRequestType.GET, "/", "index.html")] public static async Task Index(RequestContext req) { await req.RespWriter.WriteAsync("It works!"); } [HttpEndpoint(HttpRequestType.GET, "index2.html")] public static async Task Index2(RequestContext req) { await req.RespWriter.WriteAsync(GetHttpPageContentFromPrefix("index2.html")); } [HttpEndpoint(HttpRequestType.GET, "/testpage")] public static async Task TestPage(RequestContext req) { await req.RespWriter.WriteAsync(GetHttpPageContentFromPrefix("testpage")); } [HttpEndpoint(HttpRequestType.GET, "testpage2")] public static async Task TestPage2(RequestContext req) { await req.RespWriter.WriteAsync(GetHttpPageContentFromPrefix("testpage2")); } [HttpEndpoint(HttpRequestType.GET, "/testpage3")] public static async Task TestPage3(RequestContext req) { await req.RespWriter.WriteAsync(GetHttpPageContentFromPrefix("testpage3")); } public static string GetReturnQueryPageResult(string arg1, int arg2, int arg3) => $"{arg1};{arg2 * 2 - arg3 * 5}"; [HttpEndpoint(HttpRequestType.GET, "/returnqueries")] public static async Task TestPage3(RequestContext req, string arg1, int arg2, int arg3) { await req.RespWriter.WriteAsync(GetReturnQueryPageResult(arg1, arg2, arg3)); } } }