Skip to content

Commit dca15d7

Browse files
committed
extend the problem details sample with a status code selector and add a functional test
1 parent d525944 commit dca15d7

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

src/Middleware/Diagnostics/test/FunctionalTests/ProblemDetailsExceptionHandlerSampleTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Mvc;
77
using System.Net.Http.Json;
88
using System.Net.Http.Headers;
9+
using Microsoft.AspNetCore.Http;
910

1011
namespace Microsoft.AspNetCore.Diagnostics.FunctionalTests;
1112

@@ -34,4 +35,21 @@ public async Task ExceptionHandlerPage_ProducesProblemDetails()
3435
Assert.NotNull(body);
3536
Assert.Equal(500, body.Status);
3637
}
38+
39+
[Fact]
40+
public async Task StatusCodeSelector_ProducesProblemDetailsWith403()
41+
{
42+
// Arrange
43+
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/throw/conflict");
44+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
45+
46+
// Act
47+
var response = await Client.SendAsync(request);
48+
49+
// Assert
50+
var body = await response.Content.ReadFromJsonAsync<ProblemDetails>();
51+
Assert.Equal(HttpStatusCode.Conflict, response.StatusCode);
52+
Assert.NotNull(body);
53+
Assert.Equal(StatusCodes.Status409Conflict, body.Status);
54+
}
3755
}

src/Middleware/Diagnostics/test/UnitTests/ExceptionHandlerTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ public async Task ExceptionHandler_SelectsStatusCode()
692692
}
693693

694694
[Fact]
695-
public async Task ExceptionHandler_SelectsStatusCode404_When404ThrowedAndNotNotAllowed()
695+
public async Task StatusCodeSelector_CanSelect404()
696696
{
697697
using var host = new HostBuilder()
698698
.ConfigureWebHost(webHostBuilder =>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace ExceptionHandlerSample;
5+
6+
public class ConflictException(string message) : Exception(message);

src/Middleware/Diagnostics/test/testassets/ExceptionHandlerSample/StartupWithProblemDetails.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,22 @@ public void ConfigureServices(IServiceCollection services)
1717
public void Configure(IApplicationBuilder app)
1818
{
1919
// Configure the error handler to produces a ProblemDetails.
20-
app.UseExceptionHandler();
20+
app.UseExceptionHandler(new ExceptionHandlerOptions
21+
{
22+
StatusCodeSelector = ex => ex is ConflictException
23+
? StatusCodes.Status409Conflict
24+
: StatusCodes.Status500InternalServerError,
25+
});
2126

2227
// The broken section of our application.
2328
app.Map("/throw", throwApp =>
2429
{
25-
throwApp.Run(context => { throw new Exception("Application Exception"); });
30+
throwApp.Map("/conflict", throwConflictApp =>
31+
{
32+
throwConflictApp.Run(_ => throw new ConflictException("Conflict Exception"));
33+
});
34+
35+
throwApp.Run(_ => throw new Exception("Application Exception"));
2636
});
2737

2838
app.UseStaticFiles();
@@ -32,7 +42,8 @@ public void Configure(IApplicationBuilder app)
3242
{
3343
context.Response.ContentType = "text/html";
3444
await context.Response.WriteAsync("<html><body>Welcome to the sample<br><br>\r\n");
35-
await context.Response.WriteAsync("Click here to throw an exception: <a href=\"/throw\">throw</a>\r\n");
45+
await context.Response.WriteAsync("Click here to throw an exception: <a href=\"/throw\">throw</a><br>\r\n");
46+
await context.Response.WriteAsync("Click here to throw a conflict exception: <a href=\"/throw/conflict\">throw conflict</a>\r\n");
3647
await context.Response.WriteAsync("</body></html>\r\n");
3748
});
3849
}

0 commit comments

Comments
 (0)