Simplify tests

This commit is contained in:
GHXX 2024-01-13 03:14:22 +01:00
parent a2a70e8339
commit 08003d1fc3

View File

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