@@ -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,35 @@ describe('set', () => {
242
271
243
272
await blobs . set ( key , value , { ttl } )
244
273
} )
274
+
275
+ test ( 'Throws when the API 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
+ expect ( headers . authorization ) . toBe ( `Bearer ${ apiToken } ` )
284
+
285
+ return new Response ( null , { status : 401 } )
286
+ }
287
+
288
+ throw new Error ( `Unexpected fetch call: ${ url } ` )
289
+ }
290
+
291
+ const blobs = new Blobs ( {
292
+ authentication : {
293
+ token : apiToken ,
294
+ } ,
295
+ fetcher,
296
+ siteID,
297
+ } )
298
+
299
+ expect ( async ( ) => await blobs . set ( key , 'value' ) ) . rejects . toThrowError (
300
+ 'put operation has failed: API returned a 401 response' ,
301
+ )
302
+ } )
245
303
} )
246
304
247
305
describe ( 'delete' , ( ) => {
@@ -281,4 +339,37 @@ describe('delete', () => {
281
339
282
340
await blobs . delete ( key )
283
341
} )
342
+
343
+ test ( 'Throws when the API returns a non-200 status code' , async ( ) => {
344
+ const fetcher = async ( ...args : Parameters < typeof globalThis . fetch > ) => {
345
+ const [ url , options ] = args
346
+ const headers = options ?. headers as Record < string , string >
347
+
348
+ expect ( options ?. method ) . toBe ( 'delete' )
349
+
350
+ if ( url === `https://api.netlify.com/api/v1/sites/${ siteID } /blobs/${ key } ?context=production` ) {
351
+ expect ( headers . authorization ) . toBe ( `Bearer ${ apiToken } ` )
352
+
353
+ return new Response ( null , { status : 401 } )
354
+ }
355
+
356
+ if ( url === signedURL ) {
357
+ return new Response ( 'Something went wrong' , { status : 401 } )
358
+ }
359
+
360
+ throw new Error ( `Unexpected fetch call: ${ url } ` )
361
+ }
362
+
363
+ const blobs = new Blobs ( {
364
+ authentication : {
365
+ token : apiToken ,
366
+ } ,
367
+ fetcher,
368
+ siteID,
369
+ } )
370
+
371
+ expect ( async ( ) => await blobs . delete ( key ) ) . rejects . toThrowError (
372
+ 'delete operation has failed: API returned a 401 response' ,
373
+ )
374
+ } )
284
375
} )
0 commit comments