public async Task WithAdditionalOutput()
        {
            // Given
            TestDocument redirected1 = new TestDocument(
                new FilePath("foo2.html"),
                new MetadataItems
            {
                { Keys.RedirectFrom, new List <FilePath> {
                      new FilePath("foo.html")
                  } }
            });
            TestDocument redirected2 = new TestDocument(new MetadataItems
            {
                { Keys.RedirectFrom, new List <FilePath> {
                      new FilePath("bar/baz.html")
                  } }
            });
            GenerateRedirects redirect = new GenerateRedirects().WithAdditionalOutput(new FilePath("a/b"), x => string.Join("|", x.Select(y => $"{y.Key} {y.Value}")));

            // When
            IReadOnlyList <TestDocument> results = await ExecuteAsync(new[] { redirected1, redirected2 }, redirect);

            // Then
            results.Select(x => x.Destination.FullPath).ShouldBe(new[] { "foo.html", "bar/baz.html", "a/b" });
            results.Single(x => x.Destination.FullPath == "a/b").Content.ShouldContain("foo.html /foo2.html");
        }
        public async Task WithAdditionalOutputWithoutMetaRefresh()
        {
            // Given
            TestDocument redirected1 = new TestDocument(
                new FilePath("foo2.html"),
                new MetadataItems
            {
                { Keys.RedirectFrom, new List <FilePath> {
                      new FilePath("foo.html")
                  } }
            });
            TestDocument redirected2 = new TestDocument(new MetadataItems
            {
                { Keys.RedirectFrom, new List <FilePath> {
                      new FilePath("bar/baz.html")
                  } }
            });
            GenerateRedirects redirect = new GenerateRedirects()
                                         .WithAdditionalOutput(new FilePath("a/b"), x => string.Join("|", x.Select(y => $"{y.Key} {y.Value}")))
                                         .WithMetaRefreshPages(false);

            // When
            IReadOnlyList <TestDocument> results = await ExecuteAsync(new[] { redirected1, redirected2 }, redirect);

            // Then
            CollectionAssert.AreEquivalent(new[] { "a/b" }, results.Select(x => x.Destination.FullPath));
        }
Пример #3
0
        public Redirects()
        {
            Dependencies.Add(nameof(Content));

            ProcessModules = new ModuleList
            {
                new ReplaceDocuments(nameof(Content)),
                new FlattenTree(),
                new ExecuteConfig(Config.FromSettings(settings =>
                {
                    GenerateRedirects generateRedirects = new GenerateRedirects()
                                                          .WithMetaRefreshPages(settings.GetBool(WebKeys.MetaRefreshRedirects, true));
                    if (settings.GetBool(WebKeys.NetlifyRedirects, false))
                    {
                        generateRedirects = generateRedirects.WithAdditionalOutput(
                            "_redirects",
                            redirects => string.Join(Environment.NewLine, redirects.Select(r => $"/{r.Key} {r.Value}")));
                    }
                    return(generateRedirects);
                }))
            };

            OutputModules = new ModuleList
            {
                new WriteFiles()
            };
        }
        public async Task AddsExtension(string input, string expected)
        {
            // Given
            TestDocument redirected = new TestDocument(new MetadataItems
            {
                { Keys.RedirectFrom, new List <FilePath> {
                      new FilePath(input)
                  } }
            });
            TestDocument      notRedirected = new TestDocument();
            GenerateRedirects redirect      = new GenerateRedirects();

            // When
            IReadOnlyList <TestDocument> results = await ExecuteAsync(new[] { redirected, notRedirected }, redirect);

            // Then
            CollectionAssert.AreEqual(new[] { expected }, results.Select(x => x.Destination.FullPath));
        }
        public async Task SetsHtmlMediaType()
        {
            // Given
            TestDocument redirected = new TestDocument(new MetadataItems
            {
                { Keys.RedirectFrom, new List <FilePath> {
                      new FilePath("foo.html")
                  } }
            });
            TestDocument      notRedirected = new TestDocument();
            GenerateRedirects redirect      = new GenerateRedirects();

            // When
            IReadOnlyList <TestDocument> results = await ExecuteAsync(new[] { redirected, notRedirected }, redirect);

            // Then
            results.Single().ContentProvider.MediaType.ShouldBe(MediaTypes.Html);
        }
        public async Task SingleRedirect()
        {
            // Given
            TestDocument redirected = new TestDocument(new MetadataItems
            {
                { Keys.RedirectFrom, new List <FilePath> {
                      new FilePath("foo.html")
                  } }
            });
            TestDocument      notRedirected = new TestDocument();
            GenerateRedirects redirect      = new GenerateRedirects();

            // When
            IReadOnlyList <TestDocument> results = await ExecuteAsync(new[] { redirected, notRedirected }, redirect);

            // Then
            results.Select(x => x.Destination.FullPath).ShouldBe(new[] { "foo.html" });
        }
        public async Task WarnsForAbsoluteRedirectFromPath()
        {
            // Given
            TestDocument redirected = new TestDocument(new FilePath("/"), null, new MetadataItems
            {
                { Keys.RedirectFrom, new List <FilePath> {
                      new FilePath("/foo/bar")
                  } }
            });
            TestDocument         notRedirected = new TestDocument();
            GenerateRedirects    redirect      = new GenerateRedirects();
            TestExecutionContext context       = new TestExecutionContext(redirected, notRedirected);

            context.TestLoggerProvider.ThrowLogLevel = LogLevel.None;

            // When
            IReadOnlyList <TestDocument> results = await ExecuteAsync(context, redirect);

            // Then
            context.LogMessages.ShouldContain(x => x.LogLevel == LogLevel.Warning && x.FormattedMessage.StartsWith("The redirect path must be relative"));
            results.ShouldBeEmpty();
        }
        public async Task MultipleRedirects()
        {
            // Given
            TestDocument redirected1 = new TestDocument(new MetadataItems
            {
                { Keys.RedirectFrom, new List <FilePath> {
                      new FilePath("foo.html")
                  } }
            });
            TestDocument redirected2 = new TestDocument(new MetadataItems
            {
                { Keys.RedirectFrom, new List <FilePath> {
                      new FilePath("bar/baz.html")
                  } }
            });
            GenerateRedirects redirect = new GenerateRedirects();

            // When
            IReadOnlyList <TestDocument> results = await ExecuteAsync(new[] { redirected1, redirected2 }, redirect);

            // Then
            CollectionAssert.AreEquivalent(new[] { "foo.html", "bar/baz.html" }, results.Select(x => x.Destination.FullPath));
        }
Пример #9
0
        public async Task MultipleRedirects()
        {
            // Given
            TestDocument redirected1 = new TestDocument(new MetadataItems
            {
                { Keys.RedirectFrom, new List <NormalizedPath> {
                      new NormalizedPath("foo.html")
                  } }
            });
            TestDocument redirected2 = new TestDocument(new MetadataItems
            {
                { Keys.RedirectFrom, new List <NormalizedPath> {
                      new NormalizedPath("bar/baz.html")
                  } }
            });
            GenerateRedirects redirect = new GenerateRedirects();

            // When
            IReadOnlyList <TestDocument> results = await ExecuteAsync(new[] { redirected1, redirected2 }, redirect);

            // Then
            results.Select(x => x.Destination.FullPath).ShouldBe(new[] { "foo.html", "bar/baz.html" });
        }