Skip to content

Make UseStatusCodePagesWithReExecute clear Endpoints #14531

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

Merged
merged 1 commit into from
Oct 1, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Options;

namespace Microsoft.AspNetCore.Builder
Expand Down Expand Up @@ -188,6 +189,12 @@ public static IApplicationBuilder UseStatusCodePagesWithReExecute(
OriginalQueryString = originalQueryString.HasValue ? originalQueryString.Value : null,
});

// An endpoint may have already been set. Since we're going to re-invoke the middleware pipeline we need to reset
// the endpoint and route values to ensure things are re-calculated.
context.HttpContext.SetEndpoint(endpoint: null);
var routeValuesFeature = context.HttpContext.Features.Get<IRouteValuesFeature>();
routeValuesFeature?.RouteValues?.Clear();

context.HttpContext.Request.Path = newPath;
context.HttpContext.Request.QueryString = newQueryString;
try
Expand Down
106 changes: 0 additions & 106 deletions src/Middleware/Diagnostics/test/UnitTests/ExceptionHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,112 +177,6 @@ public async Task ClearsResponseBuffer_BeforeRequestIsReexecuted()
}
}

[Fact]
public async Task Redirect_StatusPage()
{
var expectedStatusCode = 432;
var destination = "/location";
var builder = new WebHostBuilder()
.Configure(app =>
{
app.UseStatusCodePagesWithRedirects("/errorPage?id={0}");

app.Map(destination, (innerAppBuilder) =>
{
innerAppBuilder.Run((httpContext) =>
{
httpContext.Response.StatusCode = expectedStatusCode;
return Task.FromResult(1);
});
});

app.Map("/errorPage", (innerAppBuilder) =>
{
innerAppBuilder.Run(async (httpContext) =>
{
await httpContext.Response.WriteAsync(httpContext.Request.QueryString.Value);
});
});

app.Run((context) =>
{

throw new InvalidOperationException($"Invalid input provided. {context.Request.Path}");
});
});
var expectedQueryString = $"?id={expectedStatusCode}";
var expectedUri = $"/errorPage{expectedQueryString}";
using (var server = new TestServer(builder))
{

var client = server.CreateClient();
var response = await client.GetAsync(destination);
Assert.Equal(HttpStatusCode.Found, response.StatusCode);
Assert.Equal(expectedUri, response.Headers.First(s => s.Key == "Location").Value.First());

response = await client.GetAsync(expectedUri);
var content = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedQueryString, content);
Assert.Equal(expectedQueryString, response.RequestMessage.RequestUri.Query);
}
}

[Fact]
public async Task Reexecute_CanRetrieveInformationAboutOriginalRequest()
{
var expectedStatusCode = 432;
var destination = "/location";
var builder = new WebHostBuilder()
.Configure(app =>
{
app.Use(async (context, next) =>
{
var beforeNext = context.Request.QueryString;
await next();
var afterNext = context.Request.QueryString;

Assert.Equal(beforeNext, afterNext);
});
app.UseStatusCodePagesWithReExecute(pathFormat: "/errorPage", queryFormat: "?id={0}");

app.Map(destination, (innerAppBuilder) =>
{
innerAppBuilder.Run((httpContext) =>
{
httpContext.Response.StatusCode = expectedStatusCode;
return Task.FromResult(1);
});
});

app.Map("/errorPage", (innerAppBuilder) =>
{
innerAppBuilder.Run(async (httpContext) =>
{
var statusCodeReExecuteFeature = httpContext.Features.Get<IStatusCodeReExecuteFeature>();
await httpContext.Response.WriteAsync(
httpContext.Request.QueryString.Value
+ ", "
+ statusCodeReExecuteFeature.OriginalPath
+ ", "
+ statusCodeReExecuteFeature.OriginalQueryString);
});
});

app.Run((context) =>
{
throw new InvalidOperationException("Invalid input provided.");
});
});

using (var server = new TestServer(builder))
{
var client = server.CreateClient();
var response = await client.GetAsync(destination + "?name=James");
var content = await response.Content.ReadAsStringAsync();
Assert.Equal($"?id={expectedStatusCode}, /location, ?name=James", content);
}
}

[Fact]
public async Task ClearsCacheHeaders_SetByReexecutionPathHandlers()
{
Expand Down
174 changes: 174 additions & 0 deletions src/Middleware/Diagnostics/test/UnitTests/StatusCodeMiddlewareTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// 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.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Microsoft.AspNetCore.Diagnostics
{
public class StatusCodeMiddlewareTest
{
[Fact]
public async Task Redirect_StatusPage()
{
var expectedStatusCode = 432;
var destination = "/location";
var builder = new WebHostBuilder()
.Configure(app =>
{
app.UseStatusCodePagesWithRedirects("/errorPage?id={0}");

app.Map(destination, (innerAppBuilder) =>
{
innerAppBuilder.Run((httpContext) =>
{
httpContext.Response.StatusCode = expectedStatusCode;
return Task.FromResult(1);
});
});

app.Map("/errorPage", (innerAppBuilder) =>
{
innerAppBuilder.Run(async (httpContext) =>
{
await httpContext.Response.WriteAsync(httpContext.Request.QueryString.Value);
});
});

app.Run((context) =>
{
throw new InvalidOperationException($"Invalid input provided. {context.Request.Path}");
});
});
var expectedQueryString = $"?id={expectedStatusCode}";
var expectedUri = $"/errorPage{expectedQueryString}";
using var server = new TestServer(builder);
var client = server.CreateClient();
var response = await client.GetAsync(destination);
Assert.Equal(HttpStatusCode.Found, response.StatusCode);
Assert.Equal(expectedUri, response.Headers.First(s => s.Key == "Location").Value.First());

response = await client.GetAsync(expectedUri);
var content = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedQueryString, content);
Assert.Equal(expectedQueryString, response.RequestMessage.RequestUri.Query);
}

[Fact]
public async Task Reexecute_CanRetrieveInformationAboutOriginalRequest()
{
var expectedStatusCode = 432;
var destination = "/location";
var builder = new WebHostBuilder()
.Configure(app =>
{
app.Use(async (context, next) =>
{
var beforeNext = context.Request.QueryString;
await next();
var afterNext = context.Request.QueryString;

Assert.Equal(beforeNext, afterNext);
});
app.UseStatusCodePagesWithReExecute(pathFormat: "/errorPage", queryFormat: "?id={0}");

app.Map(destination, (innerAppBuilder) =>
{
innerAppBuilder.Run((httpContext) =>
{
httpContext.Response.StatusCode = expectedStatusCode;
return Task.FromResult(1);
});
});

app.Map("/errorPage", (innerAppBuilder) =>
{
innerAppBuilder.Run(async (httpContext) =>
{
var statusCodeReExecuteFeature = httpContext.Features.Get<IStatusCodeReExecuteFeature>();
await httpContext.Response.WriteAsync(
httpContext.Request.QueryString.Value
+ ", "
+ statusCodeReExecuteFeature.OriginalPath
+ ", "
+ statusCodeReExecuteFeature.OriginalQueryString);
});
});

app.Run((context) =>
{
throw new InvalidOperationException("Invalid input provided.");
});
});

using var server = new TestServer(builder);
var client = server.CreateClient();
var response = await client.GetAsync(destination + "?name=James");
var content = await response.Content.ReadAsStringAsync();
Assert.Equal($"?id={expectedStatusCode}, /location, ?name=James", content);
}

[Fact]
public async Task Reexecute_ClearsEndpointAndRouteData()
{
var expectedStatusCode = 432;
var destination = "/location";
var builder = new WebHostBuilder()
.Configure(app =>
{
app.UseStatusCodePagesWithReExecute(pathFormat: "/errorPage", queryFormat: "?id={0}");

app.Use((context, next) =>
{
Assert.Empty(context.Request.RouteValues);
Assert.Null(context.GetEndpoint());
return next();
});

app.Map(destination, (innerAppBuilder) =>
{
innerAppBuilder.Run((httpContext) =>
{
httpContext.SetEndpoint(new Endpoint((_) => Task.CompletedTask, new EndpointMetadataCollection(), "Test"));
httpContext.Request.RouteValues["John"] = "Doe";
httpContext.Response.StatusCode = expectedStatusCode;
return Task.CompletedTask;
});
});

app.Map("/errorPage", (innerAppBuilder) =>
{
innerAppBuilder.Run(async (httpContext) =>
{
var statusCodeReExecuteFeature = httpContext.Features.Get<IStatusCodeReExecuteFeature>();
await httpContext.Response.WriteAsync(
httpContext.Request.QueryString.Value
+ ", "
+ statusCodeReExecuteFeature.OriginalPath
+ ", "
+ statusCodeReExecuteFeature.OriginalQueryString);
});
});

app.Run((context) =>
{
throw new InvalidOperationException("Invalid input provided.");
});
});

using var server = new TestServer(builder);
var client = server.CreateClient();
var response = await client.GetAsync(destination + "?name=James");
var content = await response.Content.ReadAsStringAsync();
Assert.Equal($"?id={expectedStatusCode}, /location, ?name=James", content);
}
}
}