using Sharp.NetServer.Routing; using Sharp.NetServer.Attributes; using Sharp.NetServer.Controllers; using Sharp.NetServer.Models; namespace NetServer.Test.UnitTests; /// /// 路由系统测试 /// [TestClass] public class RouteTableTests { [TestMethod] public void RegisterController_ShouldDiscoverRoutes() { // Arrange var routeTable = new RouteTable(); // Act routeTable.RegisterController(typeof(TestController)); var routes = routeTable.GetAllRoutes(); // Assert Assert.IsNotNull(routes); Assert.IsTrue(routes.Count > 0); Assert.IsTrue(routes.Any(r => r.Template == "/api/test/hello")); } [TestMethod] public void FindRoute_ShouldMatchCorrectRoute() { // Arrange var routeTable = new RouteTable(); routeTable.RegisterController(typeof(TestController)); // Act var route = routeTable.FindRoute("GET", "/api/test/hello"); // Assert Assert.IsNotNull(route); Assert.AreEqual("GET", route.HttpMethod); Assert.AreEqual("/api/test/hello", route.Template); } [TestMethod] public void FindRoute_WithParameter_ShouldMatch() { // Arrange var routeTable = new RouteTable(); routeTable.RegisterController(typeof(TestController)); // Act var route = routeTable.FindRoute("GET", "/api/test/user/123"); // Assert Assert.IsNotNull(route); Assert.AreEqual("GET", route.HttpMethod); } [TestMethod] public void FindRoute_InvalidPath_ShouldReturnNull() { // Arrange var routeTable = new RouteTable(); routeTable.RegisterController(typeof(TestController)); // Act var route = routeTable.FindRoute("GET", "/invalid/path"); // Assert Assert.IsNull(route); } [TestMethod] public void RegisterController_ShouldCaptureAuthorization() { // Arrange var routeTable = new RouteTable(); // Act routeTable.RegisterController(typeof(TestController)); var route = routeTable.FindRoute("GET", "/api/test/protected"); // Assert Assert.IsNotNull(route); Assert.IsTrue(route.RequireAuthorization); } [TestMethod] public void RegisterController_ShouldCaptureAllowAnonymous() { // Arrange var routeTable = new RouteTable(); // Act routeTable.RegisterController(typeof(TestController)); var route = routeTable.FindRoute("GET", "/api/test/hello"); // Assert Assert.IsNotNull(route); Assert.IsTrue(route.AllowAnonymous); } [TestMethod] public void RegisterController_ShouldCaptureRateLimit() { // Arrange var routeTable = new RouteTable(); // Act routeTable.RegisterController(typeof(TestController)); var route = routeTable.FindRoute("GET", "/api/test/limited"); // Assert Assert.IsNotNull(route); Assert.IsNotNull(route.RateLimitConfig); Assert.AreEqual(100, route.RateLimitConfig.MaxRequests); } } /// /// 测试用控制器 /// [ApiController] [Route("api/test")] internal class TestController : BaseController { [AllowAnonymous] [HttpGet("hello")] public async Task HelloAsync(HttpContext context) { await WriteSuccessResponseAsync(context, new { message = "Hello" }); } [HttpGet("user/{id}")] public async Task GetUserAsync(HttpContext context) { await WriteSuccessResponseAsync(context, new { id = "123" }); } [Authorize] [HttpGet("protected")] public async Task ProtectedAsync(HttpContext context) { await WriteSuccessResponseAsync(context, new { message = "Protected" }); } [RateLimit(100, RateLimitScope.Ip)] [HttpGet("limited")] public async Task LimitedAsync(HttpContext context) { await WriteSuccessResponseAsync(context, new { message = "Limited" }); } }