@@ -51,6 +51,12 @@ const firstFetchCall = (fetchFn: ReturnType<typeof vi.fn<typeof fetch>>) => {
5151 return call ;
5252} ;
5353
54+ const responseWithCancel = ( status : number , statusText ?: string ) => {
55+ const cancel = vi . fn ( ) ;
56+ const body = new ReadableStream < Uint8Array > ( { cancel } ) ;
57+ return { response : new Response ( body , { status, statusText } ) , cancel } ;
58+ } ;
59+
5460// ─── isPrivateOrReservedIP ───────────────────────────────────────────────────
5561
5662describe ( "isPrivateOrReservedIP" , ( ) => {
@@ -477,7 +483,8 @@ describe("uploadToConsentUrl", () => {
477483 } ) ;
478484
479485 it ( "allows upload to a valid SharePoint URL and performs PUT" , async ( ) => {
480- const mockFetch = vi . fn < typeof fetch > ( async ( ) => new Response ( null , { status : 200 } ) ) ;
486+ const { response, cancel } = responseWithCancel ( 200 ) ;
487+ const mockFetch = vi . fn < typeof fetch > ( async ( ) => response ) ;
481488 const buffer = Buffer . from ( "file content" ) ;
482489
483490 await uploadToConsentUrl ( {
@@ -500,14 +507,12 @@ describe("uploadToConsentUrl", () => {
500507 expect ( opts ?. body ) . toEqual ( new Uint8Array ( buffer ) ) ;
501508 expect ( opts ?. signal ) . toBeInstanceOf ( AbortSignal ) ;
502509 expect ( opts ?. signal ?. aborted ) . toBe ( false ) ;
510+ expect ( cancel ) . toHaveBeenCalledOnce ( ) ;
503511 } ) ;
504512
505513 it ( "throws on non-OK response after passing validation" , async ( ) => {
506- const mockFetch = vi . fn ( ) . mockResolvedValue ( {
507- ok : false ,
508- status : 403 ,
509- statusText : "Forbidden" ,
510- } ) ;
514+ const { response, cancel } = responseWithCancel ( 403 , "Forbidden" ) ;
515+ const mockFetch = vi . fn < typeof fetch > ( async ( ) => response ) ;
511516
512517 await expect (
513518 uploadToConsentUrl ( {
@@ -517,22 +522,6 @@ describe("uploadToConsentUrl", () => {
517522 validationOpts : { resolveFn : publicResolve } ,
518523 } ) ,
519524 ) . rejects . toThrow ( "File upload to consent URL failed: 403 Forbidden" ) ;
520- } ) ;
521-
522- it ( "cancels a non-OK consent upload response body before throwing" , async ( ) => {
523- const response = new Response ( "upload rejected" , { status : 403 , statusText : "Forbidden" } ) ;
524- const cancel = vi . spyOn ( response . body ! , "cancel" ) . mockResolvedValue ( undefined ) ;
525- const fetchFn = vi . fn < typeof fetch > ( async ( ) => response ) ;
526-
527- await expect (
528- uploadToConsentUrl ( {
529- url : "https://contoso.sharepoint.com/sites/uploads/file.pdf" ,
530- buffer : Buffer . from ( "data" ) ,
531- fetchFn,
532- validationOpts : { resolveFn : publicResolve } ,
533- } ) ,
534- ) . rejects . toThrow ( "File upload to consent URL failed: 403 Forbidden" ) ;
535-
536525 expect ( cancel ) . toHaveBeenCalledOnce ( ) ;
537526 } ) ;
538527
0 commit comments