@@ -243,12 +243,13 @@ impl MultipartError {
243
243
244
244
/// Get the response body text used for this rejection.
245
245
pub fn body_text ( & self ) -> String {
246
+ let body = self . source . to_string ( ) ;
246
247
axum_core:: __log_rejection!(
247
248
rejection_type = Self ,
248
- body_text = self . body_text ( ) ,
249
+ body_text = body ,
249
250
status = self . status( ) ,
250
251
) ;
251
- self . source . to_string ( )
252
+ body
252
253
}
253
254
254
255
/// Get the status code used for this rejection.
@@ -398,4 +399,43 @@ mod tests {
398
399
let res = client. post ( "/" ) . multipart ( form) . await ;
399
400
assert_eq ! ( res. status( ) , StatusCode :: PAYLOAD_TOO_LARGE ) ;
400
401
}
402
+
403
+ #[ tokio:: test]
404
+ #[ cfg( feature = "tracing" ) ]
405
+ async fn body_too_large_with_tracing ( ) {
406
+ const BYTES : & [ u8 ] = "<!doctype html><title>🦀</title>" . as_bytes ( ) ;
407
+
408
+ async fn handle ( mut multipart : Multipart ) -> impl IntoResponse {
409
+ let result: Result < ( ) , MultipartError > = async {
410
+ while let Some ( field) = multipart. next_field ( ) . await ? {
411
+ field. bytes ( ) . await ?;
412
+ }
413
+ Ok ( ( ) )
414
+ }
415
+ . await ;
416
+
417
+ let subscriber = tracing_subscriber:: FmtSubscriber :: builder ( )
418
+ . with_max_level ( tracing:: level_filters:: LevelFilter :: TRACE )
419
+ . with_writer ( std:: io:: sink)
420
+ . finish ( ) ;
421
+
422
+ let guard = tracing:: subscriber:: set_default ( subscriber) ;
423
+ let response = result. into_response ( ) ;
424
+ drop ( guard) ;
425
+
426
+ response
427
+ }
428
+
429
+ let app = Router :: new ( )
430
+ . route ( "/" , post ( handle) )
431
+ . layer ( DefaultBodyLimit :: max ( BYTES . len ( ) - 1 ) ) ;
432
+
433
+ let client = TestClient :: new ( app) ;
434
+
435
+ let form =
436
+ reqwest:: multipart:: Form :: new ( ) . part ( "file" , reqwest:: multipart:: Part :: bytes ( BYTES ) ) ;
437
+
438
+ let res = client. post ( "/" ) . multipart ( form) . await ;
439
+ assert_eq ! ( res. status( ) , StatusCode :: PAYLOAD_TOO_LARGE ) ;
440
+ }
401
441
}
0 commit comments