Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure enhanced nav requests have the correct headers #50263

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Components/Web.JS/dist/Release/blazor.web.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ export async function performEnhancedPageLoad(internalDestinationHref: string, f
const abortSignal = currentEnhancedNavigationAbortController.signal;
const responsePromise = fetch(internalDestinationHref, Object.assign({
signal: abortSignal,
headers: { 'blazor-enhanced-nav': 'on' },
headers: {
'blazor-enhanced-nav': 'on',
'accept': 'text/html',
},
}, fetchOptions));
await getResponsePartsWithFraming(responsePromise, abortSignal,
(response, initialContent) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ public void CanNavigateToNonHtmlResponse()
Browser.Equal("Hello, this is plain text", () => Browser.Exists(By.TagName("html")).Text);
}

[Fact]
public void EnhancedNavRequestsIncludeExpectedHeaders()
{
Navigate($"{ServerPathBase}/nav");
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("List headers")).Click();

var ul = Browser.Exists(By.Id("all-headers"));
var allHeaders = ul.FindElements(By.TagName("li")).Select(x => x.Text.ToLowerInvariant()).ToList();

// The server can trigger arbitrary behavior based on this
Assert.Contains("blazor-enhanced-nav: on", allHeaders);

// This is to make the enhanced nav outcomes more similar to non-enhanced nav.
// For example, the default error middleware will only serve the error page if
// this header is included.
Assert.Contains("accept: text/html", allHeaders);
}

[Fact]
public void ScrollsToHashWithContentAddedAsynchronously()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Globalization;
using System.Reflection;
using System.Security.Claims;
using System.Web;
using Components.TestServer.RazorComponents;
using Components.TestServer.RazorComponents.Pages.Forms;
using Components.TestServer.Services;
Expand Down Expand Up @@ -123,5 +124,17 @@ private static void MapEnhancedNavigationEndpoints(IEndpointRouteBuilder endpoin
response.ContentType = "text/html";
await response.WriteAsync("<h1>404</h1><p>Sorry, there's nothing here! This is a custom server-generated 404 message.</p>");
});

// Used when testing that enhanced nav includes "Accept: text/html"
endpoints.Map("/nav/list-headers", async (HttpRequest request, HttpResponse response) =>
{
response.ContentType = "text/html";
await response.WriteAsync("<ul id='all-headers'>");
foreach (var header in request.Headers)
{
await response.WriteAsync($"<li>{HttpUtility.HtmlEncode(header.Key)}: {HttpUtility.HtmlEncode(header.Value)}</li>");
}
await response.WriteAsync("</ul>");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
<NavLink href="nav/do-redirection?destination=https://microsoft.com">Redirect external</NavLink> |
<NavLink href="nav/do-redirection-while-streaming">Redirect while streaming</NavLink> |
<NavLink href="nav/do-redirection-while-streaming?destination=https://microsoft.com">Redirect external while streaming</NavLink> |
<NavLink href="nav/throw-while-streaming">Error while streaming</NavLink>
<NavLink href="nav/interactive-component-navigation/server">Interactive component navigation (server)</NavLink>
<NavLink href="nav/interactive-component-navigation/webassembly">Interactive component navigation (webassembly)</NavLink>
<NavLink href="nav/throw-while-streaming">Error while streaming</NavLink> |
<NavLink href="nav/interactive-component-navigation/server">Interactive component navigation (server)</NavLink> |
<NavLink href="nav/interactive-component-navigation/webassembly">Interactive component navigation (webassembly)</NavLink> |
<NavLink href="nav/list-headers">List headers</NavLink> |
</nav>
<hr/>
@Body
Loading