@@ -252,7 +252,7 @@ void TestAction([FromBody(AllowEmpty = true)] BodyStruct bodyStruct)
252
252
}
253
253
254
254
[ Fact ]
255
- public async Task RequestDelegateLogsFromBodyIOExceptionsAsDebug ( )
255
+ public async Task RequestDelegateLogsFromBodyIOExceptionsAsDebugAndAborts ( )
256
256
{
257
257
var invoked = false ;
258
258
@@ -287,6 +287,43 @@ void TestAction([FromBody] Todo todo)
287
287
Assert . Same ( ioException , logMessage . Exception ) ;
288
288
}
289
289
290
+ [ Fact ]
291
+ public async Task RequestDelegateLogsFromBodyInvalidDataExceptionsAsDebugAndSets400Response ( )
292
+ {
293
+ var invoked = false ;
294
+
295
+ var sink = new TestSink ( context => context . LoggerName == "Microsoft.AspNetCore.Routing.MapAction" ) ;
296
+ var testLoggerFactory = new TestLoggerFactory ( sink , enabled : true ) ;
297
+
298
+ void TestAction ( [ FromBody ] Todo todo )
299
+ {
300
+ invoked = true ;
301
+ }
302
+
303
+ var invalidDataException = new InvalidDataException ( ) ;
304
+ var serviceCollection = new ServiceCollection ( ) ;
305
+ serviceCollection . AddSingleton < ILoggerFactory > ( testLoggerFactory ) ;
306
+
307
+ var httpContext = new DefaultHttpContext ( ) ;
308
+ httpContext . Request . Headers [ "Content-Type" ] = "application/json" ;
309
+ httpContext . Request . Body = new IOExceptionThrowingRequestBodyStream ( invalidDataException ) ;
310
+ httpContext . Features . Set < IHttpRequestLifetimeFeature > ( new TestHttpRequestLifetimeFeature ( ) ) ;
311
+ httpContext . RequestServices = serviceCollection . BuildServiceProvider ( ) ;
312
+
313
+ var requestDelegate = MapActionExpressionTreeBuilder . BuildRequestDelegate ( ( Action < Todo > ) TestAction ) ;
314
+
315
+ await requestDelegate ( httpContext ) ;
316
+
317
+ Assert . False ( invoked ) ;
318
+ Assert . False ( httpContext . RequestAborted . IsCancellationRequested ) ;
319
+ Assert . Equal ( 400 , httpContext . Response . StatusCode ) ;
320
+
321
+ var logMessage = Assert . Single ( sink . Writes ) ;
322
+ Assert . Equal ( new EventId ( 2 , "RequestBodyInvalidDataException" ) , logMessage . EventId ) ;
323
+ Assert . Equal ( LogLevel . Debug , logMessage . LogLevel ) ;
324
+ Assert . Same ( invalidDataException , logMessage . Exception ) ;
325
+ }
326
+
290
327
[ Fact ]
291
328
public async Task RequestDelegatePopulatesFromFormParameterBasedOnParameterName ( )
292
329
{
@@ -316,7 +353,7 @@ void TestAction([FromForm] int value)
316
353
}
317
354
318
355
[ Fact ]
319
- public async Task RequestDelegateLogsFromFormIOExceptionsAsDebug ( )
356
+ public async Task RequestDelegateLogsFromFormIOExceptionsAsDebugAndAborts ( )
320
357
{
321
358
var invoked = false ;
322
359
@@ -351,6 +388,43 @@ void TestAction([FromForm] int value)
351
388
Assert . Same ( ioException , logMessage . Exception ) ;
352
389
}
353
390
391
+ [ Fact ]
392
+ public async Task RequestDelegateLogsFromFormInvalidDataExceptionsAsDebugAndSets400Response ( )
393
+ {
394
+ var invoked = false ;
395
+
396
+ var sink = new TestSink ( context => context . LoggerName == "Microsoft.AspNetCore.Routing.MapAction" ) ;
397
+ var testLoggerFactory = new TestLoggerFactory ( sink , enabled : true ) ;
398
+
399
+ void TestAction ( [ FromForm ] int value )
400
+ {
401
+ invoked = true ;
402
+ }
403
+
404
+ var invalidDataException = new InvalidDataException ( ) ;
405
+ var serviceCollection = new ServiceCollection ( ) ;
406
+ serviceCollection . AddSingleton < ILoggerFactory > ( testLoggerFactory ) ;
407
+
408
+ var httpContext = new DefaultHttpContext ( ) ;
409
+ httpContext . Request . Headers [ "Content-Type" ] = "application/x-www-form-urlencoded" ;
410
+ httpContext . Request . Body = new IOExceptionThrowingRequestBodyStream ( invalidDataException ) ;
411
+ httpContext . Features . Set < IHttpRequestLifetimeFeature > ( new TestHttpRequestLifetimeFeature ( ) ) ;
412
+ httpContext . RequestServices = serviceCollection . BuildServiceProvider ( ) ;
413
+
414
+ var requestDelegate = MapActionExpressionTreeBuilder . BuildRequestDelegate ( ( Action < int > ) TestAction ) ;
415
+
416
+ await requestDelegate ( httpContext ) ;
417
+
418
+ Assert . False ( invoked ) ;
419
+ Assert . False ( httpContext . RequestAborted . IsCancellationRequested ) ;
420
+ Assert . Equal ( 400 , httpContext . Response . StatusCode ) ;
421
+
422
+ var logMessage = Assert . Single ( sink . Writes ) ;
423
+ Assert . Equal ( new EventId ( 2 , "RequestBodyInvalidDataException" ) , logMessage . EventId ) ;
424
+ Assert . Equal ( LogLevel . Debug , logMessage . LogLevel ) ;
425
+ Assert . Same ( invalidDataException , logMessage . Exception ) ;
426
+ }
427
+
354
428
[ Fact ]
355
429
public void BuildRequestDelegateThrowsInvalidOperationExceptionGivenBothFromBodyAndFromFormOnDifferentParameters ( )
356
430
{
0 commit comments