@@ -502,26 +502,23 @@ describe('PDPServer', () => {
502502 const mockUuid = '12345678-90ab-cdef-1234-567890abcdef'
503503
504504 server . use (
505- http . post < Record < string , never > , { pieceCid : string } > ( 'http://pdp.local/pdp/piece' , async ( { request } ) => {
506- try {
507- const body = await request . json ( )
508- assert . exists ( body . pieceCid )
509- return HttpResponse . text ( 'Created' , {
510- status : 201 ,
511- headers : {
512- Location : `/pdp/piece/upload/${ mockUuid } ` ,
513- } ,
514- } )
515- } catch ( error ) {
516- return HttpResponse . text ( ( error as Error ) . message , {
517- status : 400 ,
518- } )
519- }
505+ http . post ( 'http://pdp.local/pdp/piece/uploads' , async ( ) => {
506+ return HttpResponse . text ( 'Created' , {
507+ status : 201 ,
508+ headers : {
509+ Location : `/pdp/piece/uploads/${ mockUuid } ` ,
510+ } ,
511+ } )
520512 } ) ,
521- http . put ( 'http://pdp.local/pdp/piece/upload /:uuid' , async ( ) => {
513+ http . put ( 'http://pdp.local/pdp/piece/uploads /:uuid' , async ( ) => {
522514 return HttpResponse . text ( 'No Content' , {
523515 status : 204 ,
524516 } )
517+ } ) ,
518+ http . post ( 'http://pdp.local/pdp/piece/uploads/:uuid' , async ( ) => {
519+ return HttpResponse . text ( 'OK' , {
520+ status : 200 ,
521+ } )
525522 } )
526523 )
527524
@@ -537,26 +534,23 @@ describe('PDPServer', () => {
537534 const mockUuid = 'fedcba09-8765-4321-fedc-ba0987654321'
538535
539536 server . use (
540- http . post < Record < string , never > , { pieceCid : string } > ( 'http://pdp.local/pdp/piece' , async ( { request } ) => {
541- try {
542- const body = await request . json ( )
543- assert . exists ( body . pieceCid )
544- return HttpResponse . text ( 'Created' , {
545- status : 201 ,
546- headers : {
547- Location : `/pdp/piece/upload/${ mockUuid } ` ,
548- } ,
549- } )
550- } catch ( error ) {
551- return HttpResponse . text ( ( error as Error ) . message , {
552- status : 400 ,
553- } )
554- }
537+ http . post ( 'http://pdp.local/pdp/piece/uploads' , async ( ) => {
538+ return HttpResponse . text ( 'Created' , {
539+ status : 201 ,
540+ headers : {
541+ Location : `/pdp/piece/uploads/${ mockUuid } ` ,
542+ } ,
543+ } )
555544 } ) ,
556- http . put ( 'http://pdp.local/pdp/piece/upload /:uuid' , async ( ) => {
545+ http . put ( 'http://pdp.local/pdp/piece/uploads /:uuid' , async ( ) => {
557546 return HttpResponse . text ( 'No Content' , {
558547 status : 204 ,
559548 } )
549+ } ) ,
550+ http . post ( 'http://pdp.local/pdp/piece/uploads/:uuid' , async ( ) => {
551+ return HttpResponse . text ( 'OK' , {
552+ status : 200 ,
553+ } )
560554 } )
561555 )
562556
@@ -565,33 +559,42 @@ describe('PDPServer', () => {
565559 assert . equal ( result . size , 5 )
566560 } )
567561
568- it ( 'should handle existing piece (200 response) ' , async ( ) => {
562+ it ( 'should throw on create upload session error ' , async ( ) => {
569563 const testData = new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 ] )
570- const mockPieceCid = 'bafkzcibcd4bdomn3tgwgrh3g532zopskstnbrd2n3sxfqbze7rxt7vqn7veigmy'
571564
572565 server . use (
573- http . post < Record < string , never > , { pieceCid : string } > ( 'http://pdp.local/pdp/piece' , async ( ) => {
574- return HttpResponse . json (
575- { pieceCid : mockPieceCid } ,
576- {
577- status : 200 ,
578- }
579- )
566+ http . post ( 'http://pdp.local/pdp/piece/uploads' , async ( ) => {
567+ return HttpResponse . text ( 'Database error' , {
568+ status : 500 ,
569+ } )
580570 } )
581571 )
582572
583- // Should not throw - existing piece is OK
584- const result = await pdpServer . uploadPiece ( testData )
585- assert . exists ( result . pieceCid )
586- assert . equal ( result . size , 5 )
573+ try {
574+ await pdpServer . uploadPiece ( testData )
575+ assert . fail ( 'Should have thrown error' )
576+ } catch ( error : any ) {
577+ assert . include ( error . message , 'Failed to create upload session' )
578+ assert . include ( error . message , '500' )
579+ assert . include ( error . message , 'Database error' )
580+ }
587581 } )
588582
589- it ( 'should throw on create upload session error' , async ( ) => {
583+ it ( 'should throw on upload error' , async ( ) => {
590584 const testData = new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 ] )
585+ const mockUuid = '12345678-90ab-cdef-1234-567890abcdef'
591586
592587 server . use (
593- http . post < Record < string , never > , { pieceCid : string } > ( 'http://pdp.local/pdp/piece' , async ( ) => {
594- return HttpResponse . text ( 'Database error' , {
588+ http . post ( 'http://pdp.local/pdp/piece/uploads' , async ( ) => {
589+ return HttpResponse . text ( 'Created' , {
590+ status : 201 ,
591+ headers : {
592+ Location : `/pdp/piece/uploads/${ mockUuid } ` ,
593+ } ,
594+ } )
595+ } ) ,
596+ http . put ( 'http://pdp.local/pdp/piece/uploads/:uuid' , async ( ) => {
597+ return HttpResponse . text ( 'Upload failed' , {
595598 status : 500 ,
596599 } )
597600 } )
@@ -601,9 +604,44 @@ describe('PDPServer', () => {
601604 await pdpServer . uploadPiece ( testData )
602605 assert . fail ( 'Should have thrown error' )
603606 } catch ( error : any ) {
604- assert . include ( error . message , 'Failed to create upload session ' )
607+ assert . include ( error . message , 'Failed to upload piece ' )
605608 assert . include ( error . message , '500' )
606- assert . include ( error . message , 'Database error' )
609+ assert . include ( error . message , 'Upload failed' )
610+ }
611+ } )
612+
613+ it ( 'should throw on finalization error' , async ( ) => {
614+ const testData = new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 ] )
615+ const mockUuid = '12345678-90ab-cdef-1234-567890abcdef'
616+
617+ server . use (
618+ http . post ( 'http://pdp.local/pdp/piece/uploads' , async ( ) => {
619+ return HttpResponse . text ( 'Created' , {
620+ status : 201 ,
621+ headers : {
622+ Location : `/pdp/piece/uploads/${ mockUuid } ` ,
623+ } ,
624+ } )
625+ } ) ,
626+ http . put ( 'http://pdp.local/pdp/piece/uploads/:uuid' , async ( ) => {
627+ return HttpResponse . text ( 'No Content' , {
628+ status : 204 ,
629+ } )
630+ } ) ,
631+ http . post ( 'http://pdp.local/pdp/piece/uploads/:uuid' , async ( ) => {
632+ return HttpResponse . text ( 'CommP mismatch' , {
633+ status : 400 ,
634+ } )
635+ } )
636+ )
637+
638+ try {
639+ await pdpServer . uploadPiece ( testData )
640+ assert . fail ( 'Should have thrown error' )
641+ } catch ( error : any ) {
642+ assert . include ( error . message , 'Failed to finalize upload' )
643+ assert . include ( error . message , '400' )
644+ assert . include ( error . message , 'CommP mismatch' )
607645 }
608646 } )
609647 } )
0 commit comments