From 770da3addd54072e8eeedca8cfa540a8b885b6fc Mon Sep 17 00:00:00 2001 From: "Per Christian B. Viken" Date: Sun, 1 Mar 2026 15:57:14 +0100 Subject: [PATCH] Return No Content if reading request body is canceled --- src/Http/Wolverine.Http.Tests/posting_json.cs | 18 ++++++++++++++++++ src/Http/Wolverine.Http/HttpHandler.cs | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/src/Http/Wolverine.Http.Tests/posting_json.cs b/src/Http/Wolverine.Http.Tests/posting_json.cs index 69eb82014..a487d3cdb 100644 --- a/src/Http/Wolverine.Http.Tests/posting_json.cs +++ b/src/Http/Wolverine.Http.Tests/posting_json.cs @@ -112,4 +112,22 @@ public async Task post_json_but_accept_text_get_406() x.StatusCodeShouldBe(406); }); } + + [Fact] + public async Task reading_json_from_canceled_request_gets_204() + { + var cts = new CancellationTokenSource(); + cts.Cancel(); + + var response = await Scenario(x => + { + x.ConfigureHttpContext(ctx => + { + ctx.Request.HttpContext.RequestAborted = cts.Token; + }); + x.Post.Json(new Question { One = 3, Two = 4 }).ToUrl("/question"); + x.WithRequestHeader("accept", "application/json"); + x.StatusCodeShouldBe(204); + }); + } } \ No newline at end of file diff --git a/src/Http/Wolverine.Http/HttpHandler.cs b/src/Http/Wolverine.Http/HttpHandler.cs index 1cc96f2ba..257bf85e7 100644 --- a/src/Http/Wolverine.Http/HttpHandler.cs +++ b/src/Http/Wolverine.Http/HttpHandler.cs @@ -156,6 +156,11 @@ private static bool isRequestJson(HttpContext context) return (body, HandlerContinuation.Continue); } + catch (OperationCanceledException) + { + context.Response.StatusCode = 204; + return (default, HandlerContinuation.Stop); + } catch (Exception e) { var logger = context.RequestServices.GetService>();