Simplify tests
This commit is contained in:
parent
a2a70e8339
commit
08003d1fc3
|
|
@ -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<HttpResponseMessage> 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<TestEndpoints>();
|
||||
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!");
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user