public void OnProvidersExecuting_DoesNotAddsModelsForAreaPages_IfFeatureIsDisabled() { // Arrange var descriptors = new[] { GetDescriptor("/Pages/About.cshtml"), GetDescriptor("/Areas/Accounts/Pages/Home.cshtml"), }; var options = new RazorPagesOptions { EnableAreas = false }; var provider = new TestCompiledPageRouteModelProvider(descriptors, options); var context = new PageRouteModelProviderContext(); // Act provider.OnProvidersExecuting(context); // Assert Assert.Collection(context.RouteModels, result => { Assert.Equal("/Pages/About.cshtml", result.RelativePath); Assert.Equal("/About", result.ViewEnginePath); Assert.Collection(result.Selectors, selector => Assert.Equal("About", selector.AttributeRouteModel.Template)); Assert.Collection(result.RouteValues.OrderBy(k => k.Key), kvp => { Assert.Equal("page", kvp.Key); Assert.Equal("/About", kvp.Value); }); }); }
public void OnProvidersExecuting_AddsMultipleSelectorsForIndexPage() { // Arrange var descriptors = new[] { GetDescriptor("/Pages/Index.cshtml"), GetDescriptor("/Pages/Admin/Index.cshtml", "some-template"), }; var provider = new TestCompiledPageRouteModelProvider(descriptors, new RazorPagesOptions()); var context = new PageRouteModelProviderContext(); // Act provider.OnProvidersExecuting(context); // Assert Assert.Collection(context.RouteModels, result => { Assert.Equal("/Pages/Index.cshtml", result.RelativePath); Assert.Equal("/Index", result.ViewEnginePath); Assert.Collection(result.Selectors, selector => Assert.Equal("Index", selector.AttributeRouteModel.Template), selector => Assert.Equal("", selector.AttributeRouteModel.Template)); }, result => { Assert.Equal("/Pages/Admin/Index.cshtml", result.RelativePath); Assert.Equal("/Admin/Index", result.ViewEnginePath); Assert.Collection(result.Selectors, selector => Assert.Equal("Admin/Index/some-template", selector.AttributeRouteModel.Template), selector => Assert.Equal("Admin/some-template", selector.AttributeRouteModel.Template)); }); }
public void OnProvidersExecuting_AddsModelsForCompiledViews() { // Arrange var descriptors = new[] { GetDescriptor("/Pages/About.cshtml"), GetDescriptor("/Pages/Home.cshtml", "some-prefix"), }; var provider = new TestCompiledPageRouteModelProvider(descriptors, new RazorPagesOptions()); var context = new PageRouteModelProviderContext(); // Act provider.OnProvidersExecuting(context); // Assert Assert.Collection(context.RouteModels, result => { Assert.Equal("/Pages/About.cshtml", result.RelativePath); Assert.Equal("/About", result.ViewEnginePath); Assert.Collection(result.Selectors, selector => Assert.Equal("About", selector.AttributeRouteModel.Template)); }, result => { Assert.Equal("/Pages/Home.cshtml", result.RelativePath); Assert.Equal("/Home", result.ViewEnginePath); Assert.Collection(result.Selectors, selector => Assert.Equal("Home/some-prefix", selector.AttributeRouteModel.Template)); }); }
public void OnProvidersExecuting_DoesNotAddAreaAndNonAreaRoutesForAPage() { // Arrange var descriptors = new[] { GetDescriptor("/Areas/Accounts/Manage/Home.cshtml"), GetDescriptor("/Areas/About.cshtml"), GetDescriptor("/Contact.cshtml"), }; var options = new RazorPagesOptions { AllowAreas = true, AreaRootDirectory = "/Areas", RootDirectory = "/", }; var provider = new TestCompiledPageRouteModelProvider(descriptors, options); var context = new PageRouteModelProviderContext(); // Act provider.OnProvidersExecuting(context); // Assert Assert.Collection(context.RouteModels, result => { Assert.Equal("/Areas/Accounts/Manage/Home.cshtml", result.RelativePath); Assert.Equal("/Manage/Home", result.ViewEnginePath); Assert.Collection(result.Selectors, selector => Assert.Equal("Accounts/Manage/Home", selector.AttributeRouteModel.Template)); Assert.Collection(result.RouteValues.OrderBy(k => k.Key), kvp => { Assert.Equal("area", kvp.Key); Assert.Equal("Accounts", kvp.Value); }, kvp => { Assert.Equal("page", kvp.Key); Assert.Equal("/Manage/Home", kvp.Value); }); }, result => { Assert.Equal("/Contact.cshtml", result.RelativePath); Assert.Equal("/Contact", result.ViewEnginePath); Assert.Collection(result.Selectors, selector => Assert.Equal("Contact", selector.AttributeRouteModel.Template)); Assert.Collection(result.RouteValues.OrderBy(k => k.Key), kvp => { Assert.Equal("page", kvp.Key); Assert.Equal("/Contact", kvp.Value); }); }); }
public CompiledPageRouteModelProviderTest() { FileProvider = new TestFileProvider(); Project = new FileProviderRazorProject( Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == FileProvider), Mock.Of <IHostingEnvironment>(e => e.ContentRootPath == "BasePath")); TemplateEngine = new RazorTemplateEngine(RazorEngine.Create(), Project); PagesOptions = new RazorPagesOptions(); Provider = new TestCompiledPageRouteModelProvider(new ApplicationPartManager(), Options.Create(PagesOptions), TemplateEngine, NullLoggerFactory.Instance); }
private TestCompiledPageRouteModelProvider CreateProvider( RazorPagesOptions options = null, IList <CompiledViewDescriptor> descriptors = null) { options = options ?? new RazorPagesOptions(); var provider = new TestCompiledPageRouteModelProvider( new ApplicationPartManager(), Options.Create(options), NullLogger <CompiledPageRouteModelProvider> .Instance); provider.Descriptors.AddRange(descriptors ?? Array.Empty <CompiledViewDescriptor>()); return(provider); }
private CompiledPageRouteModelProvider CreateProvider(IList <RazorCompiledItem> items, RazorPagesOptions options = null) { options = options ?? new RazorPagesOptions(); var provider = new TestCompiledPageRouteModelProvider( new ApplicationPartManager(), Options.Create(options), NullLogger <CompiledPageRouteModelProvider> .Instance); for (var i = 0; i < items.Count; i++) { provider.Descriptors.Add(new CompiledViewDescriptor(items[i])); } return(provider); }
public void OnProvidersExecuting_ThrowsIfRouteTemplateHasOverridePattern() { // Arrange var descriptors = new[] { GetDescriptor("/Pages/Index.cshtml"), GetDescriptor("/Pages/Home.cshtml", "/some-prefix"), }; var provider = new TestCompiledPageRouteModelProvider(descriptors, new RazorPagesOptions()); var context = new PageRouteModelProviderContext(); // Act & Assert var ex = Assert.Throws <InvalidOperationException>(() => provider.OnProvidersExecuting(context)); Assert.Equal("The route for the page at '/Pages/Home.cshtml' cannot start with / or ~/. Pages do not support overriding the file path of the page.", ex.Message); }
private TestCompiledPageRouteModelProvider CreateProvider( RazorPagesOptions options = null, IList <CompiledViewDescriptor> descriptors = null, VirtualRazorProjectFileSystem fileSystem = null) { options = options ?? new RazorPagesOptions(); fileSystem = fileSystem ?? new VirtualRazorProjectFileSystem(); var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem); var provider = new TestCompiledPageRouteModelProvider( new ApplicationPartManager(), Options.Create(options), projectEngine, NullLogger <CompiledPageRouteModelProvider> .Instance); provider.Descriptors.AddRange(descriptors ?? Array.Empty <CompiledViewDescriptor>()); return(provider); }
private TestCompiledPageRouteModelProvider CreateProvider( RazorPagesOptions options = null, IList <CompiledViewDescriptor> descriptors = null, TestFileProvider fileProvider = null) { options = options ?? new RazorPagesOptions(); fileProvider = fileProvider ?? new TestFileProvider(); var fileSystem = new FileProviderRazorProjectFileSystem( Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider), Mock.Of <IHostingEnvironment>(e => e.ContentRootPath == "BasePath")); var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem); var provider = new TestCompiledPageRouteModelProvider( new ApplicationPartManager(), Options.Create(options), projectEngine, NullLogger <CompiledPageRouteModelProvider> .Instance); provider.Descriptors.AddRange(descriptors ?? Array.Empty <CompiledViewDescriptor>()); return(provider); }
public void OnProvidersExecuting_AddsModelsForCompiledAreaPages() { // Arrange var descriptors = new[] { GetDescriptor("/Features/Products/Files/About.cshtml"), GetDescriptor("/Features/Products/Files/Manage/Index.cshtml"), GetDescriptor("/Features/Products/Files/Manage/Edit.cshtml", "{id}"), }; var options = new RazorPagesOptions { EnableAreas = true, AreaRootDirectory = "/Features", RootDirectory = "/Files", }; var provider = new TestCompiledPageRouteModelProvider(descriptors, options); var context = new PageRouteModelProviderContext(); // Act provider.OnProvidersExecuting(context); // Assert Assert.Collection(context.RouteModels, result => { Assert.Equal("/Features/Products/Files/About.cshtml", result.RelativePath); Assert.Equal("/About", result.ViewEnginePath); Assert.Collection(result.Selectors, selector => Assert.Equal("Products/About", selector.AttributeRouteModel.Template)); Assert.Collection(result.RouteValues.OrderBy(k => k.Key), kvp => { Assert.Equal("area", kvp.Key); Assert.Equal("Products", kvp.Value); }, kvp => { Assert.Equal("page", kvp.Key); Assert.Equal("/About", kvp.Value); }); }, result => { Assert.Equal("/Features/Products/Files/Manage/Index.cshtml", result.RelativePath); Assert.Equal("/Manage/Index", result.ViewEnginePath); Assert.Collection(result.Selectors, selector => Assert.Equal("Products/Manage/Index", selector.AttributeRouteModel.Template), selector => Assert.Equal("Products/Manage", selector.AttributeRouteModel.Template)); Assert.Collection(result.RouteValues.OrderBy(k => k.Key), kvp => { Assert.Equal("area", kvp.Key); Assert.Equal("Products", kvp.Value); }, kvp => { Assert.Equal("page", kvp.Key); Assert.Equal("/Manage/Index", kvp.Value); }); }, result => { Assert.Equal("/Features/Products/Files/Manage/Edit.cshtml", result.RelativePath); Assert.Equal("/Manage/Edit", result.ViewEnginePath); Assert.Collection(result.Selectors, selector => Assert.Equal("Products/Manage/Edit/{id}", selector.AttributeRouteModel.Template)); Assert.Collection(result.RouteValues.OrderBy(k => k.Key), kvp => { Assert.Equal("area", kvp.Key); Assert.Equal("Products", kvp.Value); }, kvp => { Assert.Equal("page", kvp.Key); Assert.Equal("/Manage/Edit", kvp.Value); }); }); }