diff --git a/SimpleHttpServerTest/SimpleServerTest.cs b/SimpleHttpServerTest/SimpleServerTest.cs index 20835ad..9c38237 100644 --- a/SimpleHttpServerTest/SimpleServerTest.cs +++ b/SimpleHttpServerTest/SimpleServerTest.cs @@ -1,4 +1,6 @@ using SimpleHttpServer; +using System.Net; +using System.Runtime.CompilerServices; namespace SimpleHttpServerTest; @@ -8,7 +10,14 @@ public class SimpleServerTest { const int PORT = 8833; private HttpServer? activeServer = null; + private HttpClient? activeHttpClient = null; 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(resp.StatusCode, statusCode); + return resp; + } [TestInitialize] public void Init() { @@ -21,6 +30,8 @@ public class SimpleServerTest { activeServer.RegisterEndpointsFromType(); activeServer.Start(); + activeHttpClient = new HttpClient(); + Console.WriteLine("Server started."); } @@ -33,17 +44,17 @@ public class SimpleServerTest { } await Console.Out.WriteLineAsync("Shutting down server..."); await activeServer.StopAsync(ctokSrc.Token); + activeHttpClient?.Dispose(); + activeHttpClient = null; await Console.Out.WriteLineAsync("Shutdown finished."); } [TestMethod] public async Task CheckSimpleServe() { - using var hc = new HttpClient(); - await hc.GetStringAsync(GetRequestPath("/")); + await AssertGetStatusCodeAsync("/", HttpStatusCode.OK); } public class TestEndpoints { - [HttpEndpoint(HttpRequestType.GET, "/", "index.html", "amogus.html")] public static async Task Index(RequestContext req) { await req.RespWriter.WriteLineAsync("It works!");