Exemplo n.º 1
0
        public void Success_Match_Test(string pathString, string routeValue, string[] parameterNames, string[] parameterValues)
        {
            var match = _collection.Match(pathString);

            Assert.AreEqual(routeValue, match.Item);
            Assert.AreEqual(parameterNames.Length, match.Values.Count);
            for (int i = 0; i < parameterNames.Length; i++)
            {
                var parameterName  = parameterNames[i];
                var parameterValue = parameterValues[i];
                Assert.IsTrue(match.Values.ContainsKey(parameterName), $"{parameterName} not found");
                Assert.AreEqual(parameterValue, match.Values[parameterName], $"Parameter [{parameterName}] Expected [{parameterValue}], Actual [{match.Values[parameterName]}]");
            }
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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()
            });
        }
Exemplo n.º 4
0
        public async Task <RouteDefinitionMatch> MatchAsync(string pathString, string method)
        {
            await LoadRouteDefinitionsAsync();

            var routeTemplateMatch = _routeTemplateCollection.Match(pathString);
            var routeDefinition    = routeTemplateMatch?.Item.SingleOrDefault(rd =>
                                                                              rd.RouteMethods.Contains(method));

            if (routeDefinition == null)
            {
                return(null);
            }

            var routeDefinitionMatch = new RouteDefinitionMatch
            {
                RouteDefinition = routeDefinition,
                Values          = routeTemplateMatch.Values
            };

            return(routeDefinitionMatch);
        }