@@ -29,6 +29,8 @@ const key = '54321'
29
29
const value = 'some value'
30
30
const apiToken = 'some token'
31
31
const signedURL = 'https://signed.url/123456789'
32
+ const edgeToken = 'some other token'
33
+ const edgeURL = 'https://cloudfront.url'
32
34
33
35
describe ( 'get' , ( ) => {
34
36
test ( 'Reads from the blob store using API credentials' , async ( ) => {
@@ -69,6 +71,37 @@ describe('get', () => {
69
71
expect ( store . fulfilled ) . toBeTruthy ( )
70
72
} )
71
73
74
+ test ( 'Reads from the blob store using context credentials' , async ( ) => {
75
+ const store = new MockFetch ( )
76
+ . get ( {
77
+ headers : { authorization : `Bearer ${ edgeToken } ` } ,
78
+ response : new Response ( value ) ,
79
+ url : `${ edgeURL } /${ siteID } /production/${ key } ` ,
80
+ } )
81
+ . get ( {
82
+ headers : { authorization : `Bearer ${ edgeToken } ` } ,
83
+ response : new Response ( value ) ,
84
+ url : `${ edgeURL } /${ siteID } /production/${ key } ` ,
85
+ } )
86
+
87
+ const blobs = new Blobs ( {
88
+ authentication : {
89
+ contextURL : edgeURL ,
90
+ token : edgeToken ,
91
+ } ,
92
+ fetcher : store . fetcher ,
93
+ siteID,
94
+ } )
95
+
96
+ const string = await blobs . get ( key )
97
+ expect ( string ) . toBe ( value )
98
+
99
+ const stream = await blobs . get ( key , { type : 'stream' } )
100
+ expect ( await streamToString ( stream as unknown as NodeJS . ReadableStream ) ) . toBe ( value )
101
+
102
+ expect ( store . fulfilled ) . toBeTruthy ( )
103
+ } )
104
+
72
105
test ( 'Returns `null` when the pre-signed URL returns a 404' , async ( ) => {
73
106
const store = new MockFetch ( )
74
107
. get ( {
@@ -93,6 +126,26 @@ describe('get', () => {
93
126
expect ( store . fulfilled ) . toBeTruthy ( )
94
127
} )
95
128
129
+ test ( 'Returns `null` when the edge URL returns a 404' , async ( ) => {
130
+ const store = new MockFetch ( ) . get ( {
131
+ headers : { authorization : `Bearer ${ edgeToken } ` } ,
132
+ response : new Response ( null , { status : 404 } ) ,
133
+ url : `${ edgeURL } /${ siteID } /production/${ key } ` ,
134
+ } )
135
+
136
+ const blobs = new Blobs ( {
137
+ authentication : {
138
+ contextURL : edgeURL ,
139
+ token : edgeToken ,
140
+ } ,
141
+ fetcher : store . fetcher ,
142
+ siteID,
143
+ } )
144
+
145
+ expect ( await blobs . get ( key ) ) . toBeNull ( )
146
+ expect ( store . fulfilled ) . toBeTruthy ( )
147
+ } )
148
+
96
149
test ( 'Throws when the API returns a non-200 status code' , async ( ) => {
97
150
const store = new MockFetch ( ) . get ( {
98
151
headers : { authorization : `Bearer ${ apiToken } ` } ,
@@ -141,6 +194,29 @@ describe('get', () => {
141
194
expect ( store . fulfilled ) . toBeTruthy ( )
142
195
} )
143
196
197
+ test ( 'Throws when an edge URL returns a non-200 status code' , async ( ) => {
198
+ const store = new MockFetch ( ) . get ( {
199
+ headers : { authorization : `Bearer ${ edgeToken } ` } ,
200
+ response : new Response ( null , { status : 401 } ) ,
201
+ url : `${ edgeURL } /${ siteID } /production/${ key } ` ,
202
+ } )
203
+
204
+ const blobs = new Blobs ( {
205
+ authentication : {
206
+ contextURL : edgeURL ,
207
+ token : edgeToken ,
208
+ } ,
209
+ fetcher : store . fetcher ,
210
+ siteID,
211
+ } )
212
+
213
+ await expect ( async ( ) => await blobs . get ( key ) ) . rejects . toThrowError (
214
+ 'get operation has failed: store returned a 401 response' ,
215
+ )
216
+
217
+ expect ( store . fulfilled ) . toBeTruthy ( )
218
+ } )
219
+
144
220
test ( 'Returns `null` when the blob entry contains an expiry date in the past' , async ( ) => {
145
221
const store = new MockFetch ( )
146
222
. get ( {
@@ -225,6 +301,28 @@ describe('set', () => {
225
301
expect ( store . fulfilled ) . toBeTruthy ( )
226
302
} )
227
303
304
+ test ( 'Writes to the blob store using context credentials' , async ( ) => {
305
+ const store = new MockFetch ( ) . put ( {
306
+ body : value ,
307
+ headers : { authorization : `Bearer ${ edgeToken } ` , 'cache-control' : 'max-age=0, stale-while-revalidate=60' } ,
308
+ response : new Response ( null ) ,
309
+ url : `${ edgeURL } /${ siteID } /production/${ key } ` ,
310
+ } )
311
+
312
+ const blobs = new Blobs ( {
313
+ authentication : {
314
+ contextURL : edgeURL ,
315
+ token : edgeToken ,
316
+ } ,
317
+ fetcher : store . fetcher ,
318
+ siteID,
319
+ } )
320
+
321
+ await blobs . set ( key , value )
322
+
323
+ expect ( store . fulfilled ) . toBeTruthy ( )
324
+ } )
325
+
228
326
test ( 'Accepts a TTL parameter' , async ( ) => {
229
327
const ttl = new Date ( Date . now ( ) + 15_000 )
230
328
const store = new MockFetch ( )
@@ -398,6 +496,30 @@ describe('set', () => {
398
496
expect ( store . fulfilled ) . toBeTruthy ( )
399
497
} )
400
498
499
+ test ( 'Throws when the edge URL returns a non-200 status code' , async ( ) => {
500
+ const store = new MockFetch ( ) . put ( {
501
+ body : value ,
502
+ headers : { authorization : `Bearer ${ edgeToken } ` , 'cache-control' : 'max-age=0, stale-while-revalidate=60' } ,
503
+ response : new Response ( null , { status : 401 } ) ,
504
+ url : `${ edgeURL } /${ siteID } /production/${ key } ` ,
505
+ } )
506
+
507
+ const blobs = new Blobs ( {
508
+ authentication : {
509
+ contextURL : edgeURL ,
510
+ token : edgeToken ,
511
+ } ,
512
+ fetcher : store . fetcher ,
513
+ siteID,
514
+ } )
515
+
516
+ await expect ( async ( ) => await blobs . set ( key , value ) ) . rejects . toThrowError (
517
+ 'put operation has failed: store returned a 401 response' ,
518
+ )
519
+
520
+ expect ( store . fulfilled ) . toBeTruthy ( )
521
+ } )
522
+
401
523
test ( 'Throws when the instance is missing required configuration properties' , async ( ) => {
402
524
const { fetcher } = new MockFetch ( )
403
525
@@ -425,7 +547,7 @@ describe('set', () => {
425
547
)
426
548
} )
427
549
428
- test ( 'Retries failed operations' , async ( ) => {
550
+ test ( 'Retries failed operations when using API credentials ' , async ( ) => {
429
551
const store = new MockFetch ( )
430
552
. put ( {
431
553
headers : { authorization : `Bearer ${ apiToken } ` } ,
@@ -477,6 +599,47 @@ describe('set', () => {
477
599
478
600
expect ( store . fulfilled ) . toBeTruthy ( )
479
601
} )
602
+
603
+ test ( 'Retries failed operations when using context credentials' , async ( ) => {
604
+ const store = new MockFetch ( )
605
+ . put ( {
606
+ body : value ,
607
+ headers : { authorization : `Bearer ${ edgeToken } ` , 'cache-control' : 'max-age=0, stale-while-revalidate=60' } ,
608
+ response : new Response ( null , { status : 500 } ) ,
609
+ url : `${ edgeURL } /${ siteID } /production/${ key } ` ,
610
+ } )
611
+ . put ( {
612
+ body : value ,
613
+ headers : { authorization : `Bearer ${ edgeToken } ` , 'cache-control' : 'max-age=0, stale-while-revalidate=60' } ,
614
+ response : new Error ( 'Some network problem' ) ,
615
+ url : `${ edgeURL } /${ siteID } /production/${ key } ` ,
616
+ } )
617
+ . put ( {
618
+ body : value ,
619
+ headers : { authorization : `Bearer ${ edgeToken } ` , 'cache-control' : 'max-age=0, stale-while-revalidate=60' } ,
620
+ response : new Response ( null , { headers : { 'X-RateLimit-Reset' : '10' } , status : 429 } ) ,
621
+ url : `${ edgeURL } /${ siteID } /production/${ key } ` ,
622
+ } )
623
+ . put ( {
624
+ body : value ,
625
+ headers : { authorization : `Bearer ${ edgeToken } ` , 'cache-control' : 'max-age=0, stale-while-revalidate=60' } ,
626
+ response : new Response ( null ) ,
627
+ url : `${ edgeURL } /${ siteID } /production/${ key } ` ,
628
+ } )
629
+
630
+ const blobs = new Blobs ( {
631
+ authentication : {
632
+ contextURL : edgeURL ,
633
+ token : edgeToken ,
634
+ } ,
635
+ fetcher : store . fetcher ,
636
+ siteID,
637
+ } )
638
+
639
+ await blobs . set ( key , value )
640
+
641
+ expect ( store . fulfilled ) . toBeTruthy ( )
642
+ } )
480
643
} )
481
644
482
645
describe ( 'setJSON' , ( ) => {
@@ -509,6 +672,28 @@ describe('setJSON', () => {
509
672
expect ( store . fulfilled ) . toBeTruthy ( )
510
673
} )
511
674
675
+ test ( 'Writes to the blob store using context credentials' , async ( ) => {
676
+ const store = new MockFetch ( ) . put ( {
677
+ body : JSON . stringify ( { value } ) ,
678
+ headers : { authorization : `Bearer ${ edgeToken } ` , 'cache-control' : 'max-age=0, stale-while-revalidate=60' } ,
679
+ response : new Response ( null ) ,
680
+ url : `${ edgeURL } /${ siteID } /production/${ key } ` ,
681
+ } )
682
+
683
+ const blobs = new Blobs ( {
684
+ authentication : {
685
+ contextURL : edgeURL ,
686
+ token : edgeToken ,
687
+ } ,
688
+ fetcher : store . fetcher ,
689
+ siteID,
690
+ } )
691
+
692
+ await blobs . setJSON ( key , { value } )
693
+
694
+ expect ( store . fulfilled ) . toBeTruthy ( )
695
+ } )
696
+
512
697
test ( 'Accepts a TTL parameter' , async ( ) => {
513
698
const ttl = new Date ( Date . now ( ) + 15_000 )
514
699
const store = new MockFetch ( )
@@ -567,6 +752,27 @@ describe('delete', () => {
567
752
expect ( store . fulfilled ) . toBeTruthy ( )
568
753
} )
569
754
755
+ test ( 'Deletes from the blob store using context credentials' , async ( ) => {
756
+ const store = new MockFetch ( ) . delete ( {
757
+ headers : { authorization : `Bearer ${ edgeToken } ` } ,
758
+ response : new Response ( null ) ,
759
+ url : `${ edgeURL } /${ siteID } /production/${ key } ` ,
760
+ } )
761
+
762
+ const blobs = new Blobs ( {
763
+ authentication : {
764
+ contextURL : edgeURL ,
765
+ token : edgeToken ,
766
+ } ,
767
+ fetcher : store . fetcher ,
768
+ siteID,
769
+ } )
770
+
771
+ await blobs . delete ( key )
772
+
773
+ expect ( store . fulfilled ) . toBeTruthy ( )
774
+ } )
775
+
570
776
test ( 'Throws when the API returns a non-200 status code' , async ( ) => {
571
777
const store = new MockFetch ( ) . delete ( {
572
778
headers : { authorization : `Bearer ${ apiToken } ` } ,
@@ -588,6 +794,29 @@ describe('delete', () => {
588
794
expect ( store . fulfilled ) . toBeTruthy ( )
589
795
} )
590
796
797
+ test ( 'Throws when the edge URL returns a non-200 status code' , async ( ) => {
798
+ const store = new MockFetch ( ) . delete ( {
799
+ headers : { authorization : `Bearer ${ edgeToken } ` } ,
800
+ response : new Response ( null , { status : 401 } ) ,
801
+ url : `${ edgeURL } /${ siteID } /production/${ key } ` ,
802
+ } )
803
+
804
+ const blobs = new Blobs ( {
805
+ authentication : {
806
+ contextURL : edgeURL ,
807
+ token : edgeToken ,
808
+ } ,
809
+ fetcher : store . fetcher ,
810
+ siteID,
811
+ } )
812
+
813
+ await expect ( async ( ) => await blobs . delete ( key ) ) . rejects . toThrowError (
814
+ 'delete operation has failed: store returned a 401 response' ,
815
+ )
816
+
817
+ expect ( store . fulfilled ) . toBeTruthy ( )
818
+ } )
819
+
591
820
test ( 'Throws when the instance is missing required configuration properties' , async ( ) => {
592
821
const { fetcher } = new MockFetch ( )
593
822
0 commit comments