CSharpHttpServer/SimpleHttpServerTest/SimpleServerTest.cs

52 lines
1.7 KiB
C#

using SimpleHttpServer;
namespace SimpleHttpServerTest;
[TestClass]
public class SimpleServerTest {
const int PORT = 8833;
private HttpServer? activeServer = null;
private static string GetRequestPath(string url) => $"http://localhost:{PORT}/{url.TrimStart('/')}";
[TestInitialize]
public void Init() {
var conf = new SimpleHttpServerConfiguration();
if (activeServer != null)
throw new InvalidOperationException("Tried to create another httpserver instance when an existing one was already running.");
Console.WriteLine("Starting server...");
activeServer = new HttpServer(PORT, conf);
activeServer.RegisterEndpointsFromType<TestEndpoints>();
activeServer.Start();
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);
await Console.Out.WriteLineAsync("Shutdown finished.");
}
[TestMethod]
public async Task CheckSimpleServe() {
using var hc = new HttpClient();
await hc.GetStringAsync(GetRequestPath("/"));
}
public class TestEndpoints {
[HttpEndpoint(HttpRequestType.GET, "/", "index.html", "amogus.html")]
public static async Task Index(RequestContext req) {
await req.RespWriter.WriteLineAsync("It works!");
}
}
}