@@ -620,6 +620,73 @@ describe("error handling", () => {
620620 expect ( body . message ) . toBe ( "Resource not found" ) ;
621621 } ) ;
622622
623+ describe ( "invalid JSON body" , ( ) => {
624+ it ( "should return 400 for empty JSON body" , async ( ) => {
625+ const endpoint = createEndpoint (
626+ "/post" ,
627+ {
628+ method : "POST" ,
629+ } ,
630+ async ( c ) => {
631+ return c . body ;
632+ } ,
633+ ) ;
634+ const router = createRouter ( { endpoint } ) ;
635+ const request = new Request ( "http://localhost/post" , {
636+ method : "POST" ,
637+ headers : { "Content-Type" : "application/json" } ,
638+ body : "" ,
639+ } ) ;
640+ const response = await router . handler ( request ) ;
641+ expect ( response . status ) . toBe ( 400 ) ;
642+ const body = await response . json ( ) ;
643+ expect ( body . code ) . toBe ( "BAD_REQUEST" ) ;
644+ expect ( body . message ) . toBe ( "Invalid JSON in request body" ) ;
645+ } ) ;
646+
647+ it ( "should return 400 for malformed JSON body" , async ( ) => {
648+ const endpoint = createEndpoint (
649+ "/post" ,
650+ {
651+ method : "POST" ,
652+ } ,
653+ async ( c ) => {
654+ return c . body ;
655+ } ,
656+ ) ;
657+ const router = createRouter ( { endpoint } ) ;
658+ const request = new Request ( "http://localhost/post" , {
659+ method : "POST" ,
660+ headers : { "Content-Type" : "application/json" } ,
661+ body : "not json" ,
662+ } ) ;
663+ const response = await router . handler ( request ) ;
664+ expect ( response . status ) . toBe ( 400 ) ;
665+ const body = await response . json ( ) ;
666+ expect ( body . code ) . toBe ( "BAD_REQUEST" ) ;
667+ } ) ;
668+
669+ it ( "should return 200 for valid JSON body" , async ( ) => {
670+ const endpoint = createEndpoint (
671+ "/post" ,
672+ {
673+ method : "POST" ,
674+ } ,
675+ async ( c ) => {
676+ return c . body ;
677+ } ,
678+ ) ;
679+ const router = createRouter ( { endpoint } ) ;
680+ const request = new Request ( "http://localhost/post" , {
681+ method : "POST" ,
682+ headers : { "Content-Type" : "application/json" } ,
683+ body : "{}" ,
684+ } ) ;
685+ const response = await router . handler ( request ) ;
686+ expect ( response . status ) . toBe ( 200 ) ;
687+ } ) ;
688+ } ) ;
689+
623690 describe ( "allowedMediaTypes" , ( ) => {
624691 it ( "should allow requests with allowed media type at router level" , async ( ) => {
625692 const endpoint = createEndpoint (
0 commit comments