Support more types of redirection during prerendering. Fixes #11591 (#12418)

* E2E test to show current behavior

* Actually support base-relative, root-relative, and absolute redirections during prerendering

* Fix MVC functional test
This commit is contained in:
Steve Sanderson 2019-07-22 08:14:10 -07:00 committed by GitHub
parent 178374d228
commit 077df0e3ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 4 deletions

View File

@ -88,8 +88,10 @@ namespace Microsoft.AspNetCore.Components.Server.Circuits
if (_jsRuntime == null)
{
throw new NavigationException(uri);
var absoluteUriString = ToAbsoluteUri(uri).ToString();
throw new NavigationException(absoluteUriString);
}
_jsRuntime.InvokeAsync<object>(Interop.NavigateTo, uri, forceLoad);
}

View File

@ -1,6 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
using Microsoft.AspNetCore.E2ETesting;
@ -75,6 +79,24 @@ namespace Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests
() => Browser.FindElement(By.TagName("strong")).Text);
}
[Theory]
[InlineData("base/relative", "prerendered/base/relative")]
[InlineData("/root/relative", "/root/relative")]
[InlineData("http://absolute/url", "http://absolute/url")]
public async Task CanRedirectDuringPrerendering(string destinationParam, string expectedRedirectionLocation)
{
var requestUri = new Uri(
_serverFixture.RootUri,
"prerendered/prerendered-redirection?destination=" + destinationParam);
var httpClient = new HttpClient(new HttpClientHandler { AllowAutoRedirect = false });
var response = await httpClient.GetAsync(requestUri);
var expectedUri = new Uri(_serverFixture.RootUri, expectedRedirectionLocation);
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
Assert.Equal(expectedUri, response.Headers.Location);
}
private void BeginInteractivity()
{
Browser.FindElement(By.Id("load-boot-script")).Click();

View File

@ -0,0 +1,16 @@
@page "/prerendered-redirection"
@inject IUriHelper UriHelper
@{
throw new InvalidOperationException("The rendering logic should never be executed");
}
@code {
protected override Task OnInitializedAsync()
{
var uri = UriHelper.GetAbsoluteUri();
var destination = uri.Substring(uri.IndexOf("?destination=") + 13);
UriHelper.NavigateTo(destination);
return Task.CompletedTask;
}
}

View File

@ -86,7 +86,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.Redirect);
Assert.Equal("/navigation-redirect", response.Headers.Location.ToString());
Assert.Equal("http://localhost/navigation-redirect", response.Headers.Location.ToString());
}
[Fact]
@ -99,10 +99,9 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
var response = await client.GetAsync("http://localhost/components/Navigation/false");
// Assert
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.Redirect);
Assert.Equal("/navigation-redirect", response.Headers.Location.ToString());
Assert.Equal("http://localhost/navigation-redirect", response.Headers.Location.ToString());
}
[Fact]