private async Task LoadRouteDefinitionsAsync() { if (_routeTemplateCollection != null) { return; } using (_asyncLock.StartLockAsync()) { if (_routeTemplateCollection != null) { return; } _routeTemplateCollection = new RouteTemplateCollection <List <RouteDefinition> >(); var routeDefinitionUri = new Uri(_gatewayOptions.RouteDefinitionsUri, UriKind.RelativeOrAbsolute); var routeDefinitions = await _configurationRetriever.RetrieveRouteDefinitionsAsync <List <RouteDefinition> >(routeDefinitionUri); foreach (var routeDefinition in routeDefinitions) { var routeTemplateDefinitions = _routeTemplateCollection.Get(routeDefinition.RouteTemplate); if (routeTemplateDefinitions == null) { routeTemplateDefinitions = new List <RouteDefinition>(); _routeTemplateCollection.Add(routeDefinition.RouteTemplate, routeTemplateDefinitions); } routeTemplateDefinitions.Add(routeDefinition); } } }
public RouteTemplateMatcherTest() { _collection = new RouteTemplateCollection <string>(); _collection.Add("/", "/"); _collection.Add("/param/{path1}", "/param/{path1}"); _collection.Add("/param/{path1}/{path2}", "/param/{path1}/{path2}"); _collection.Add("/param/{path1}/{path2}/{path3}", "/param/{path1}/{path2}/{path3}"); _collection.Add("/a/{path2}", "/a/{path2}"); _collection.Add("/param/{path1}/c", "/param/{path1}/c"); _collection.Add("/a/{*path}", "/a/{*path}"); _collection.Add("/z/a/{*path}", "/z/a/{*path}"); }
public void FoundValues_Match_Test() { var collection = new RouteTemplateCollection <string>(); collection.Add("/rpc/{topicName}/{serviceName}/{*servicePath}", "route"); var match = collection.Match("/rpc/topicName/serviceName/path1/path2"); Assert.AreEqual("route", match.Item); Assert.AreEqual("topicName", match.Values.GetValueOrDefault("topicName")); Assert.AreEqual("serviceName", match.Values.GetValueOrDefault("serviceName")); Assert.AreEqual("path1/path2", match.Values.GetValueOrDefault("servicePath")); Assert.AreEqual(3, match.Values.Count); }
public async Task InvokeAsync(HttpContext httpContext) { if (_routeTemplateCollection == null) { var routeDefinitionUri = new Uri(_mockerOptions.RouteDefinitionsUri, UriKind.RelativeOrAbsolute); var routeDefinitions = await _configurationRetriever.RetrieveRouteDefinitionsAsync <List <RouteDefinition> >(routeDefinitionUri); var routeTemplateCollection = new RouteTemplateCollection <RouteDefinition>(); foreach (var routeDefinition in routeDefinitions) { routeTemplateCollection.Add(routeDefinition.RouteTemplate, routeDefinition); } _routeTemplateCollection = routeTemplateCollection; } var match = _routeTemplateCollection.Match(httpContext.Request.Path); if (match?.Item.RouteMethods == null || !match.Item.RouteMethods.Contains(httpContext.Request.Method)) { await _next(httpContext); return; } await Task.Delay(match.Item.DelayInMilliseconds, httpContext.RequestAborted); httpContext.Response.StatusCode = match.Item.StatusCode; if (match.Item.Headers != null) { foreach (var header in match.Item.Headers) { httpContext.Response.Headers.TryAdd(header.Key, header.Value); } } var body = Encoding.UTF8.GetBytes(match.Item.Body); await httpContext.Response.Body.WriteAsync(body, httpContext.RequestAborted); _historyRepository.Add(new HistoryItem { RequestPath = httpContext.Request.Path, RequestQuery = httpContext.Request.Query.Select(q => new KeyValuePair <string, string>(q.Key, q.Value)).ToList(), RequestMethod = httpContext.Request.Method, RequestHeaders = httpContext.Request.Headers.Select(h => new KeyValuePair <string, string>(h.Key, h.Value.ToString())).ToList() }); }