示例#1
0
        public void MatchTemplate_WithMatching2TempateSegment_ShouldReturn2KeyValuePairs()
        {
            var routeMatch = RouteUtil.MatchTemplate("math/addition/2/3", "math/addition/{num1}/{num2}");

            routeMatch["num1"].Should().Be("2");
            routeMatch["num2"].Should().Be("3");
        }
示例#2
0
        protected override async Task <HttpResponseMessage> ExecuteInternal(HttpRequestMessage req)
        {
            // get and validate collection name
            string collectionName = GetRouteValue(req, "collectionName");

            if (string.IsNullOrWhiteSpace(collectionName))
            {
                throw new ResourceNotFoundException();
            }

            this.Logger.Info($"Executing api for collection {collectionName}");
            // retrieve collection
            var collectionModel = await this.MockApiRepo.GetCollectionAsync(collectionName);

            // find matching api definition, throw 404 if no match is found. Skip the collectionName in the route to match the template
            var absolutePath = req.RequestUri.AbsolutePath.Substring(req.RequestUri.AbsolutePath.ToLower().IndexOf(collectionName.ToLower()) + collectionName.Length);

            this.Logger.Info($"Absolute path:{absolutePath}");
            var actualRoutePath = RouteUtil.BuildRoute(RouteUtil.GetSegments(absolutePath));

            this.Logger.Info($"Actual Route Path:{actualRoutePath}");
            IDictionary <string, string> routeTemplateValuesMap = null;
            var apiModel = collectionModel.MockApis.FirstOrDefault(model =>
            {
                routeTemplateValuesMap = RouteUtil.MatchTemplate(actualRoutePath, model.RouteTemplate);
                return(routeTemplateValuesMap != null);
            });

            if (apiModel == null)
            {
                throw new ResourceNotFoundException();
            }

            if (!HttpMethodMatch(req.Method, apiModel.Verb))
            {
                throw new MethodNotAllowedException();
            }

            // update route data for the API
            var routeData = req.GetRouteData();

            foreach (var templateKeyValue in routeTemplateValuesMap)
            {
                routeData.Values.Add(templateKeyValue.Key, templateKeyValue.Value);
            }

            // if a matching api definition is found, then validate the script
            this.languageBindingFactory.CreateLanguageValidator(apiModel.Language).Validate(apiModel.Body);

            // run the script now that all the validation has passed
            var apiLanguageBinding = this.languageBindingFactory.CreateLanguageBinding(apiModel.Language);

            return(apiLanguageBinding.Run(apiModel, req));
        }
示例#3
0
        public void MatchTemplate_WithNonMatching2TempateSegment_ShouldReturnNull()
        {
            var routeMatch = RouteUtil.MatchTemplate("math/substraction/2/3", "math/addition/{num1}/{num2}");

            routeMatch.Should().BeNull();
        }