Exemple #1
0
        [InlineData("/{a}/foo/{b}", "/6/foo/7")]           // Extra values (c) dropped
        public async Task Set_PathPattern_ReplacesPathWithRouteValues(string transformValue, string expected)
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddOptions();
            serviceCollection.AddRouting();
            using var services = serviceCollection.BuildServiceProvider();

            var routeValues = new Dictionary <string, object>
            {
                { "a", "6" },
                { "b", "7" },
                { "c", "8" },
            };

            var httpContext = new DefaultHttpContext();

            httpContext.Request.RouteValues = new RouteValueDictionary(routeValues);
            var context = new RequestTransformContext()
            {
                Path        = "/",
                HttpContext = httpContext
            };
            var transform = new PathRouteValuesTransform(transformValue, services.GetRequiredService <TemplateBinderFactory>());
            await transform.ApplyAsync(context);

            Assert.Equal(expected, context.Path);

            // The transform should not modify the original request's route values
            Assert.Equal(routeValues, httpContext.Request.RouteValues);
        }
    public async Task RouteValuesWithSlashesNotEncoded()
    {
        var serviceCollection = new ServiceCollection();

        serviceCollection.AddOptions();
        serviceCollection.AddRouting();
        using var services = serviceCollection.BuildServiceProvider();

        var routeValues = new Dictionary <string, object>
        {
            { "a", "abc" },
            { "b", "def" },
            { "remainder", "klm/nop/qrs" },
        };

        var httpContext = new DefaultHttpContext();

        httpContext.Request.RouteValues = new RouteValueDictionary(routeValues);
        var context = new RequestTransformContext()
        {
            Path        = "/",
            HttpContext = httpContext
        };
        var transform = new PathRouteValuesTransform("/{a}/{b}/{**remainder}", services.GetRequiredService <TemplateBinderFactory>());
        await transform.ApplyAsync(context);

        Assert.Equal("/abc/def/klm/nop/qrs", context.Path.Value);

        // The transform should not modify the original request's route values
        Assert.Equal(routeValues, httpContext.Request.RouteValues);
    }