Skip to content

Commit

Permalink
Added coverage for developer exception page (dotnet#35778)
Browse files Browse the repository at this point in the history
* Added coverage for developer exception page
- Added tests to verify that the developer exception page is on in development.
  • Loading branch information
davidfowl authored Aug 26, 2021
1 parent cb32a87 commit 85a0679
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
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

0 comments on commit 85a0679

Please sign in to comment.