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)); }
public void GetSegments_For1SegmentsRoute_ShouldReturn1Segment() { RouteUtil.GetSegments("math").ShouldAllBeEquivalentTo(new string[] { "math" }); }
public void GetSegments_For3SegmentsRoute_ShouldReturn3Segments() { RouteUtil.GetSegments("math/addition/2/3").ShouldAllBeEquivalentTo(new string[] { "math", "addition", "2", "3" }); }