Skip to content

Added coverage for developer exception page #35778

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 2 commits into from
Aug 26, 2021
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
1 change: 0 additions & 1 deletion src/DefaultBuilder/src/WebApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui

if (context.HostingEnvironment.IsDevelopment())
{
// TODO: add test for this
app.UseDeveloperExceptionPage();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.Net;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.HostFiltering;
Expand Down Expand Up @@ -883,7 +884,8 @@ public async Task StartupFilter_WithUseRoutingWorks()
await using var app = builder.Build();

var chosenEndpoint = string.Empty;
app.MapGet("/", async c => {
app.MapGet("/", async c =>
{
chosenEndpoint = c.GetEndpoint().DisplayName;
await c.Response.WriteAsync("Hello World");
}).WithDisplayName("One");
Expand Down Expand Up @@ -917,7 +919,8 @@ public async Task CanAddMiddlewareBeforeUseRouting()

app.UseRouting();

app.MapGet("/1", async c => {
app.MapGet("/1", async c =>
{
chosenEndpoint = c.GetEndpoint().DisplayName;
await c.Response.WriteAsync("Hello World");
}).WithDisplayName("One");
Expand Down Expand Up @@ -1158,6 +1161,78 @@ public async Task PropertiesPreservedFromInnerApplication()
app.Start();
}

[Fact]
public async Task DeveloperExceptionPageIsOnByDefaltInDevelopment()
{
var builder = WebApplication.CreateBuilder(new WebApplicationOptions() { EnvironmentName = Environments.Development });
builder.WebHost.UseTestServer();
await using var app = builder.Build();

app.MapGet("/", void () => throw new InvalidOperationException("BOOM"));

await app.StartAsync();

var client = app.GetTestClient();

var response = await client.GetAsync("/");

Assert.False(response.IsSuccessStatusCode);
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
Assert.Contains("BOOM", await response.Content.ReadAsStringAsync());
Assert.Contains("text/plain", response.Content.Headers.ContentType.MediaType);
}

[Fact]
public async Task DeveloperExceptionPageDoesNotGetCaughtByStartupFilters()
{
var builder = WebApplication.CreateBuilder(new WebApplicationOptions() { EnvironmentName = Environments.Development });
builder.WebHost.UseTestServer();
builder.Services.AddSingleton<IStartupFilter, ThrowingStartupFilter>();
await using var app = builder.Build();

await app.StartAsync();

var client = app.GetTestClient();

var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => client.GetAsync("/"));

Assert.Equal("BOOM Filter", ex.Message);
}

[Fact]
public async Task DeveloperExceptionPageIsNotOnInProduction()
{
var builder = WebApplication.CreateBuilder(new WebApplicationOptions() { EnvironmentName = Environments.Production });
builder.WebHost.UseTestServer();
await using var app = builder.Build();

app.MapGet("/", void () => throw new InvalidOperationException("BOOM"));

await app.StartAsync();

var client = app.GetTestClient();

var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => client.GetAsync("/"));

Assert.Equal("BOOM", ex.Message);
}

class ThrowingStartupFilter : IStartupFilter
{
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return app =>
{
app.Use((HttpContext context, RequestDelegate next) =>
{
throw new InvalidOperationException("BOOM Filter");
});

next(app);
};
}
}

class PropertyFilter : IStartupFilter
{
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
Expand Down