@@ -93,6 +93,35 @@ describe('get', () => {
93
93
expect ( await blobs . get ( key ) ) . toBeNull ( )
94
94
} )
95
95
96
+ test ( 'Throws when the API returns a non-200 status code' , async ( ) => {
97
+ const fetcher = async ( ...args : Parameters < typeof globalThis . fetch > ) => {
98
+ const [ url , options ] = args
99
+ const headers = options ?. headers as Record < string , string >
100
+
101
+ expect ( options ?. method ) . toBe ( 'get' )
102
+
103
+ if ( url === `https://api.netlify.com/api/v1/sites/${ siteID } /blobs/${ key } ?context=production` ) {
104
+ expect ( headers . authorization ) . toBe ( `Bearer ${ apiToken } ` )
105
+
106
+ return new Response ( null , { status : 401 , statusText : 'Unauthorized' } )
107
+ }
108
+
109
+ throw new Error ( `Unexpected fetch call: ${ url } ` )
110
+ }
111
+
112
+ const blobs = new Blobs ( {
113
+ authentication : {
114
+ token : apiToken ,
115
+ } ,
116
+ fetcher,
117
+ siteID,
118
+ } )
119
+
120
+ expect ( async ( ) => await blobs . get ( key ) ) . rejects . toThrowError (
121
+ 'get operation has failed: API returned a 401 response' ,
122
+ )
123
+ } )
124
+
96
125
test ( 'Throws when a pre-signed URL returns a non-200 status code' , async ( ) => {
97
126
const fetcher = async ( ...args : Parameters < typeof globalThis . fetch > ) => {
98
127
const [ url , options ] = args
@@ -242,6 +271,41 @@ describe('set', () => {
242
271
243
272
await blobs . set ( key , value , { ttl } )
244
273
} )
274
+
275
+ test ( 'Throws when a pre-signed URL returns a non-200 status code' , async ( ) => {
276
+ const fetcher = async ( ...args : Parameters < typeof globalThis . fetch > ) => {
277
+ const [ url , options ] = args
278
+ const headers = options ?. headers as Record < string , string >
279
+
280
+ expect ( options ?. method ) . toBe ( 'put' )
281
+
282
+ if ( url === `https://api.netlify.com/api/v1/sites/${ siteID } /blobs/${ key } ?context=production` ) {
283
+ const data = JSON . stringify ( { url : signedURL } )
284
+
285
+ expect ( headers . authorization ) . toBe ( `Bearer ${ apiToken } ` )
286
+
287
+ return new Response ( data )
288
+ }
289
+
290
+ if ( url === signedURL ) {
291
+ return new Response ( 'Something went wrong' , { status : 401 } )
292
+ }
293
+
294
+ throw new Error ( `Unexpected fetch call: ${ url } ` )
295
+ }
296
+
297
+ const blobs = new Blobs ( {
298
+ authentication : {
299
+ token : apiToken ,
300
+ } ,
301
+ fetcher,
302
+ siteID,
303
+ } )
304
+
305
+ expect ( async ( ) => await blobs . set ( key , 'value' ) ) . rejects . toThrowError (
306
+ 'put operation has failed: Something went wrong' ,
307
+ )
308
+ } )
245
309
} )
246
310
247
311
describe ( 'delete' , ( ) => {
@@ -281,4 +345,39 @@ describe('delete', () => {
281
345
282
346
await blobs . delete ( key )
283
347
} )
348
+
349
+ test ( 'Throws when a pre-signed URL returns a non-200 status code' , async ( ) => {
350
+ const fetcher = async ( ...args : Parameters < typeof globalThis . fetch > ) => {
351
+ const [ url , options ] = args
352
+ const headers = options ?. headers as Record < string , string >
353
+
354
+ expect ( options ?. method ) . toBe ( 'delete' )
355
+
356
+ if ( url === `https://api.netlify.com/api/v1/sites/${ siteID } /blobs/${ key } ?context=production` ) {
357
+ const data = JSON . stringify ( { url : signedURL } )
358
+
359
+ expect ( headers . authorization ) . toBe ( `Bearer ${ apiToken } ` )
360
+
361
+ return new Response ( data )
362
+ }
363
+
364
+ if ( url === signedURL ) {
365
+ return new Response ( 'Something went wrong' , { status : 401 } )
366
+ }
367
+
368
+ throw new Error ( `Unexpected fetch call: ${ url } ` )
369
+ }
370
+
371
+ const blobs = new Blobs ( {
372
+ authentication : {
373
+ token : apiToken ,
374
+ } ,
375
+ fetcher,
376
+ siteID,
377
+ } )
378
+
379
+ expect ( async ( ) => await blobs . delete ( key ) ) . rejects . toThrowError (
380
+ 'delete operation has failed: Something went wrong' ,
381
+ )
382
+ } )
284
383
} )
0 commit comments