public void GetVirtualPath_CanLowerCaseUrls_And_AppendTrailingSlash_BasedOnOptions( string returnUrl, string expectedUrl, bool lowercaseUrls, bool appendTrailingSlash) { // Arrange var target = new Mock<IRouter>(MockBehavior.Strict); target .Setup(e => e.GetVirtualPath(It.IsAny<VirtualPathContext>())) .Returns(new VirtualPathData(target.Object, returnUrl)); var routeCollection = new RouteCollection(); routeCollection.Add(target.Object); var virtualPathContext = CreateVirtualPathContext( options: GetRouteOptions( lowerCaseUrls: lowercaseUrls, appendTrailingSlash: appendTrailingSlash)); // Act var pathData = routeCollection.GetVirtualPath(virtualPathContext); // Assert Assert.Equal(expectedUrl, pathData.VirtualPath); Assert.Same(target.Object, pathData.Router); Assert.Empty(pathData.DataTokens); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(minLevel: LogLevel.Trace); var defaultHandler = new RouteHandler((c) => c.Response.WriteAsync($"Hello world! Route values: " + $"{string.Join(", ", c.GetRouteData().Values)}") ); var routeBuilder = new RouteBuilder(app, defaultHandler); routeBuilder.AddHelloRoute(app); routeBuilder.MapRoute( "Track Package Route", "package/{operation:regex(track|create|detonate)}/{id:int}"); app.UseRouter(routeBuilder.Build()); // demonstrate link generation var trackingRouteCollection = new RouteCollection(); trackingRouteCollection.Add(routeBuilder.Routes[1]); // "Track Package Route" app.Run(async (context) => { var dictionary = new RouteValueDictionary { {"operation","create" }, {"id",123} }; var vpc = new VirtualPathContext(context, null, dictionary, "Track Package Route"); context.Response.ContentType = "text/html"; await context.Response.WriteAsync("Menu<hr/>"); await context.Response.WriteAsync(@"<a href='" + trackingRouteCollection.GetVirtualPath(vpc).VirtualPath + "'>Create Package 123</a><br/>"); }); }
public void GetVirtualPath_DoesntLowerCaseUrls_Invariant( string returnUrl, string lowercaseUrl, bool lowercaseUrls) { // Arrange var target = new Mock<IRouter>(MockBehavior.Strict); target .Setup(e => e.GetVirtualPath(It.IsAny<VirtualPathContext>())) .Returns(new VirtualPathData(target.Object, returnUrl)); var routeCollection = new RouteCollection(); routeCollection.Add(target.Object); var virtualPathContext = CreateVirtualPathContext(options: GetRouteOptions(lowercaseUrls)); // Act var pathData = routeCollection.GetVirtualPath(virtualPathContext); // Assert Assert.Equal(lowercaseUrl, pathData.VirtualPath); Assert.Same(target.Object, pathData.Router); Assert.Empty(pathData.DataTokens); }
public void GetVirtualPath_ReturnsDataTokens(RouteValueDictionary dataTokens, string routerName) { // Arrange var virtualPath = "/TestVirtualPath"; var pathContextValues = new RouteValueDictionary { { "controller", virtualPath } }; var pathContext = CreateVirtualPathContext( pathContextValues, GetRouteOptions(), routerName); var route = CreateTemplateRoute("{controller}", routerName, dataTokens); var routeCollection = new RouteCollection(); routeCollection.Add(route); var expectedDataTokens = dataTokens ?? new RouteValueDictionary(); // Act var pathData = routeCollection.GetVirtualPath(pathContext); // Assert Assert.NotNull(pathData); Assert.Same(route, pathData.Router); Assert.Equal(virtualPath, pathData.VirtualPath); Assert.Equal(expectedDataTokens.Count, pathData.DataTokens.Count); foreach (var dataToken in expectedDataTokens) { Assert.True(pathData.DataTokens.ContainsKey(dataToken.Key)); Assert.Equal(dataToken.Value, pathData.DataTokens[dataToken.Key]); } }
public void GetVirtualPath_NoBestEffort_NoMatch() { // Arrange var route1 = CreateRoute(accept: false, match: false, matchValue: "bad"); var route2 = CreateRoute(accept: false, match: false, matchValue: "bad"); var route3 = CreateRoute(accept: false, match: false, matchValue: "bad"); var routeCollection = new RouteCollection(); routeCollection.Add(route1.Object); routeCollection.Add(route2.Object); routeCollection.Add(route3.Object); var virtualPathContext = CreateVirtualPathContext(); // Act var path = routeCollection.GetVirtualPath(virtualPathContext); Assert.Null(path); // All of these should be called route1.Verify(r => r.GetVirtualPath(It.IsAny<VirtualPathContext>()), Times.Once()); route2.Verify(r => r.GetVirtualPath(It.IsAny<VirtualPathContext>()), Times.Once()); route3.Verify(r => r.GetVirtualPath(It.IsAny<VirtualPathContext>()), Times.Once()); }
public void GetVirtualPath_Success( string template, RouteValueDictionary values, string expectedUrl, bool lowercaseUrls) { // Arrange var routeCollection = new RouteCollection(); var route = CreateTemplateRoute(template); routeCollection.Add(route); var context = CreateVirtualPathContext(values, options: GetRouteOptions(lowercaseUrls)); // Act var pathData = routeCollection.GetVirtualPath(context); // Assert Assert.Equal(expectedUrl, pathData.VirtualPath); Assert.Same(route, pathData.Router); Assert.Empty(pathData.DataTokens); }
public void GetVirtualPath_AmbiguousRoutes_RequiresRouteValueValidation_Error() { // Arrange var namedRoute = CreateNamedRoute("Ambiguous", accept: false); var routeCollection = new RouteCollection(); routeCollection.Add(namedRoute); var innerRouteCollection = new RouteCollection(); innerRouteCollection.Add(namedRoute); routeCollection.Add(innerRouteCollection); var virtualPathContext = CreateVirtualPathContext("Ambiguous"); // Act & Assert var ex = Assert.Throws<InvalidOperationException>(() => routeCollection.GetVirtualPath(virtualPathContext)); Assert.Equal("The supplied route name 'Ambiguous' is ambiguous and matched more than one route.", ex.Message); }