public RequestHandlerTest() { var headers = new Dictionary <string, string> { { "Content-Type", "application/json" } }; _route = new RouteTableItem("GET", "/test/123/", headers); _requestHandler = new RequestHandler(_route); }
public void IsMatchTest_NotContains_NullHeaders() { var headers = new Dictionary <string, string> { { "Content-Type", "application/json" }, { "testkey", "testvalue" } }; var route = new RouteTableItem("GET", "/test?q=123", headers); Assert.False(route.IsMatch(_httpRequestMock.Object)); }
private static RouteTableItem matchRoute(HttpContext ctx, RouteTable routeTable) { var routeTableItem = new RouteTableItem(); foreach (var routeItem in routeTable) { var mapKey = routeItem.Key.Split('_')[0]; var mapMethod = routeItem.Key.Split('_')[1]; var pathBlock = ctx.Request.Path.Value.Split('/').Where(x => x.IsTrue()).ToArray(); if (ctx.Request.Path.Value == mapKey && mapKey == "/") { routeTableItem = routeItem.Value; //匹配模板路由 var routeValueDictionary = Match(mapKey, ctx.Request.Path.Value); if (routeValueDictionary != null) { routeTableItem.RouteValue = routeValueDictionary; } break; ; } var tmpBlock = mapKey.Split('/').Where(x => x.IsTrue()).ToArray(); //检查路由Method if ((ctx.Request.Method.ToLower() != mapMethod) || ctx.WebSockets.IsWebSocketRequest && mapMethod == "ws") { continue; } //检查路由块数量 if (pathBlock.Length == 0 || pathBlock.Length != tmpBlock.Length) { continue; } //检查是否匹配了模板块{xxxx} var matched = true; for (int pathIndex = 0; pathIndex < pathBlock.Length; pathIndex++) { var pathItem = pathBlock[pathIndex]; var tmpItem = tmpBlock[pathIndex].ToLower(); if (!tmpItem.StartsWith("{") && pathItem != tmpItem) { matched = false; } } if (matched) { //匹配模板路由 var routeValueDictionary = Match(mapKey.ToLower(), ctx.Request.Path.Value.ToLower()); if (routeValueDictionary != null) { routeTableItem = routeItem.Value; routeTableItem.RouteValue = routeValueDictionary; break; } } else { //匹配无模板路由 var diff = pathBlock.Except(tmpBlock); if (diff.Count() == 0) { routeTableItem = routeItem.Value; break; } } } return routeTableItem; }
public void IsMatchTest() { var route = new RouteTableItem("GET", "/test?q=123", _headers); Assert.True(route.IsMatch(_httpRequestMock.Object)); }
public void Constructor_Test() { var route = new RouteTableItem(); Assert.NotNull(route.Request); }