-
Notifications
You must be signed in to change notification settings - Fork 735
/
kv.test.ts
1481 lines (1321 loc) · 56.9 KB
/
kv.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { writeFileSync } from "node:fs";
import { Headers } from "undici";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import {
setMockResponse,
unsetAllMocks,
unsetMockFetchKVGetValues,
setMockFetchKVGetValue,
} from "./helpers/mock-cfetch";
import { mockConsoleMethods } from "./helpers/mock-console";
import { clearConfirmMocks, mockConfirm } from "./helpers/mock-dialogs";
import { mockKeyListRequest } from "./helpers/mock-kv";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
import type { KeyValue, KVNamespaceInfo, NamespaceKeyInfo } from "../kv";
describe("wrangler", () => {
mockAccountId();
mockApiToken();
runInTempDir();
const std = mockConsoleMethods();
afterEach(() => {
unsetAllMocks();
clearConfirmMocks();
});
describe("kv:namespace", () => {
describe("create", () => {
function mockCreateRequest(expectedTitle: string) {
setMockResponse(
"/accounts/:accountId/storage/kv/namespaces",
"POST",
([_url, accountId], { body }) => {
expect(accountId).toEqual("some-account-id");
const title = JSON.parse(body as string).title;
expect(title).toEqual(expectedTitle);
return { id: "some-namespace-id" };
}
);
}
it("should error if no namespace is given", async () => {
await expect(
runWrangler("kv:namespace create")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Not enough non-option arguments: got 0, need at least 1"`
);
expect(std.out).toMatchInlineSnapshot(`
"
wrangler kv:namespace create <namespace>
Create a new namespace
Positionals:
namespace The name of the new namespace [string] [required]
Flags:
-c, --config Path to .toml configuration file [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Options:
-e, --env Perform on a specific environment [string]
--preview Interact with a preview namespace [boolean]"
`);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mNot enough non-option arguments: got 0, need at least 1[0m
"
`);
});
it("should error if the namespace to create contains spaces", async () => {
await expect(
runWrangler("kv:namespace create abc def ghi")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unknown arguments: def, ghi"`
);
expect(std.out).toMatchInlineSnapshot(`
"
wrangler kv:namespace create <namespace>
Create a new namespace
Positionals:
namespace The name of the new namespace [string] [required]
Flags:
-c, --config Path to .toml configuration file [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Options:
-e, --env Perform on a specific environment [string]
--preview Interact with a preview namespace [boolean]"
`);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mUnknown arguments: def, ghi[0m
"
`);
});
it("should error if the namespace to create is not valid", async () => {
await expect(
runWrangler("kv:namespace create abc-def")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"The namespace binding name \\"abc-def\\" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number."`
);
expect(std.out).toMatchInlineSnapshot(`
"
wrangler kv:namespace create <namespace>
Create a new namespace
Positionals:
namespace The name of the new namespace [string] [required]
Flags:
-c, --config Path to .toml configuration file [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Options:
-e, --env Perform on a specific environment [string]
--preview Interact with a preview namespace [boolean]"
`);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mThe namespace binding name \\"abc-def\\" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number.[0m
"
`);
});
it("should create a namespace", async () => {
mockCreateRequest("worker-UnitTestNamespace");
await runWrangler("kv:namespace create UnitTestNamespace");
expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating namespace with title \\"worker-UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
{ binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
`);
});
it("should create a preview namespace if configured to do so", async () => {
mockCreateRequest("worker-UnitTestNamespace_preview");
await runWrangler("kv:namespace create UnitTestNamespace --preview");
expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating namespace with title \\"worker-UnitTestNamespace_preview\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
{ binding = \\"UnitTestNamespace\\", preview_id = \\"some-namespace-id\\" }"
`);
});
it("should create a namespace using configured worker name", async () => {
writeFileSync("./wrangler.toml", 'name = "other-worker"', "utf-8");
mockCreateRequest("other-worker-UnitTestNamespace");
await runWrangler("kv:namespace create UnitTestNamespace");
expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating namespace with title \\"other-worker-UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
{ binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
`);
});
it("should create a namespace in an environment if configured to do so", async () => {
mockCreateRequest("worker-customEnv-UnitTestNamespace");
await runWrangler(
"kv:namespace create UnitTestNamespace --env customEnv"
);
expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating namespace with title \\"worker-customEnv-UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
{ binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
`);
});
});
describe("list", () => {
function mockListRequest(namespaces: unknown[]) {
const requests = { count: 0 };
setMockResponse(
"/accounts/:accountId/storage/kv/namespaces",
([_url, accountId], init, query) => {
requests.count++;
expect(accountId).toEqual("some-account-id");
expect(query.get("per_page")).toEqual("100");
expect(query.get("order")).toEqual("title");
expect(query.get("direction")).toEqual("asc");
expect(query.get("page")).toEqual(`${requests.count}`);
expect(init).toEqual({});
const pageSize = Number(query.get("per_page"));
const page = Number(query.get("page"));
return namespaces.slice((page - 1) * pageSize, page * pageSize);
}
);
return requests;
}
it("should list namespaces", async () => {
const kvNamespaces: KVNamespaceInfo[] = [
{ title: "title-1", id: "id-1" },
{ title: "title-2", id: "id-2" },
];
mockListRequest(kvNamespaces);
await runWrangler("kv:namespace list");
expect(std.err).toMatchInlineSnapshot(`""`);
const namespaces = JSON.parse(std.out);
expect(namespaces).toEqual(kvNamespaces);
});
it("should make multiple requests for paginated results", async () => {
// Create a lot of mock namespaces, so that the fetch requests will be paginated
const kvNamespaces: KVNamespaceInfo[] = [];
for (let i = 0; i < 550; i++) {
kvNamespaces.push({ title: "title-" + i, id: "id-" + i });
}
const requests = mockListRequest(kvNamespaces);
await runWrangler("kv:namespace list");
const namespaces = JSON.parse(std.out);
expect(namespaces).toEqual(kvNamespaces);
expect(requests.count).toEqual(6);
});
});
describe("delete", () => {
function mockDeleteRequest(expectedNamespaceId: string) {
const requests = { count: 0 };
setMockResponse(
"/accounts/:accountId/storage/kv/namespaces/:namespaceId",
"DELETE",
([_url, accountId, namespaceId]) => {
requests.count++;
expect(accountId).toEqual("some-account-id");
expect(namespaceId).toEqual(expectedNamespaceId);
return null;
}
);
return requests;
}
it("should delete a namespace specified by id", async () => {
const requests = mockDeleteRequest("some-namespace-id");
await runWrangler(
`kv:namespace delete --namespace-id some-namespace-id`
);
expect(requests.count).toEqual(1);
});
it("should delete a namespace specified by binding name", async () => {
writeWranglerConfig();
const requests = mockDeleteRequest("bound-id");
await runWrangler(
`kv:namespace delete --binding someBinding --preview false`
);
expect(requests.count).toEqual(1);
});
it("should delete a preview namespace specified by binding name", async () => {
writeWranglerConfig();
const requests = mockDeleteRequest("preview-bound-id");
await runWrangler(
`kv:namespace delete --binding someBinding --preview`
);
expect(requests.count).toEqual(1);
});
it("should error if a given binding name is not in the configured kv namespaces", async () => {
writeWranglerConfig();
await expect(runWrangler("kv:namespace delete --binding otherBinding"))
.rejects.toThrowErrorMatchingInlineSnapshot(`
"Not able to delete namespace.
A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."
`);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mNot able to delete namespace.[0m
A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
"
`);
});
it("should delete a namespace specified by binding name in a given environment", async () => {
writeWranglerConfig();
const requests = mockDeleteRequest("env-bound-id");
await runWrangler(
"kv:namespace delete --binding someBinding --env some-environment --preview false"
);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(requests.count).toEqual(1);
});
it("should delete a preview namespace specified by binding name in a given environment", async () => {
writeWranglerConfig();
const requests = mockDeleteRequest("preview-env-bound-id");
await runWrangler(
`kv:namespace delete --binding someBinding --env some-environment --preview`
);
expect(requests.count).toEqual(1);
});
});
});
describe("kv:key", () => {
describe("put", () => {
function mockKeyPutRequest(
expectedNamespaceId: string,
expectedKV: KeyValue
) {
const requests = { count: 0 };
setMockResponse(
"/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
"PUT",
([_url, accountId, namespaceId, key], { body }, query) => {
requests.count++;
expect(accountId).toEqual("some-account-id");
expect(namespaceId).toEqual(expectedNamespaceId);
expect(key).toEqual(expectedKV.key);
expect(body).toEqual(expectedKV.value);
if (expectedKV.expiration !== undefined) {
expect(query.get("expiration")).toEqual(
`${expectedKV.expiration}`
);
} else {
expect(query.has("expiration")).toBe(false);
}
if (expectedKV.expiration_ttl) {
expect(query.get("expiration_ttl")).toEqual(
`${expectedKV.expiration_ttl}`
);
} else {
expect(query.has("expiration_ttl")).toBe(false);
}
return null;
}
);
return requests;
}
it("should put a key in a given namespace specified by namespace-id", async () => {
const requests = mockKeyPutRequest("some-namespace-id", {
key: "my-key",
value: "my-value",
});
await runWrangler(
"kv:key put my-key my-value --namespace-id some-namespace-id"
);
expect(requests.count).toEqual(1);
expect(std.out).toMatchInlineSnapshot(
`"Writing the value \\"my-value\\" to key \\"my-key\\" on namespace some-namespace-id."`
);
expect(std.err).toMatchInlineSnapshot(`""`);
});
it("should encode the key in the api request to put a value", async () => {
const requests = mockKeyPutRequest("DS9", {
key: "%2Fmy-key",
value: "my-value",
});
await runWrangler("kv:key put /my-key my-value --namespace-id DS9");
expect(requests.count).toEqual(1);
expect(std.out).toMatchInlineSnapshot(
`"Writing the value \\"my-value\\" to key \\"/my-key\\" on namespace DS9."`
);
expect(std.err).toMatchInlineSnapshot(`""`);
});
it("should put a key in a given namespace specified by binding", async () => {
writeWranglerConfig();
const requests = mockKeyPutRequest("bound-id", {
key: "my-key",
value: "my-value",
});
await runWrangler(
"kv:key put my-key my-value --binding someBinding --preview false"
);
expect(std.out).toMatchInlineSnapshot(
`"Writing the value \\"my-value\\" to key \\"my-key\\" on namespace bound-id."`
);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(requests.count).toEqual(1);
});
it("should put a key in a given preview namespace specified by binding", async () => {
writeWranglerConfig();
const requests = mockKeyPutRequest("preview-bound-id", {
key: "my-key",
value: "my-value",
});
await runWrangler(
"kv:key put my-key my-value --binding someBinding --preview"
);
expect(std.out).toMatchInlineSnapshot(
`"Writing the value \\"my-value\\" to key \\"my-key\\" on namespace preview-bound-id."`
);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(requests.count).toEqual(1);
});
it("should add expiration and ttl properties when putting a key", async () => {
const requests = mockKeyPutRequest("some-namespace-id", {
key: "my-key",
value: "my-value",
expiration: 10,
expiration_ttl: 20,
});
await runWrangler(
"kv:key put my-key my-value --namespace-id some-namespace-id --expiration 10 --ttl 20"
);
expect(requests.count).toEqual(1);
expect(std.out).toMatchInlineSnapshot(
`"Writing the value \\"my-value\\" to key \\"my-key\\" on namespace some-namespace-id."`
);
expect(std.err).toMatchInlineSnapshot(`""`);
});
it("should put a key to the specified environment in a given namespace", async () => {
writeWranglerConfig();
const requests = mockKeyPutRequest("env-bound-id", {
key: "my-key",
value: "my-value",
});
await runWrangler(
"kv:key put my-key my-value --binding someBinding --env some-environment --preview false"
);
expect(std.out).toMatchInlineSnapshot(
`"Writing the value \\"my-value\\" to key \\"my-key\\" on namespace env-bound-id."`
);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(requests.count).toEqual(1);
});
it("should put a key with a value loaded from a given path", async () => {
writeFileSync("foo.txt", "file-contents", "utf-8");
const requests = mockKeyPutRequest("some-namespace-id", {
key: "my-key",
value: "file-contents",
});
await runWrangler(
"kv:key put my-key --namespace-id some-namespace-id --path foo.txt"
);
expect(std.out).toMatchInlineSnapshot(
`"Writing the contents of foo.txt to the key \\"my-key\\" on namespace some-namespace-id."`
);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(requests.count).toEqual(1);
});
it("should error if no key is provided", async () => {
await expect(
runWrangler("kv:key put")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Not enough non-option arguments: got 0, need at least 1"`
);
expect(std.out).toMatchInlineSnapshot(`
"
wrangler kv:key put <key> [value]
Writes a single key/value pair to the given namespace.
Positionals:
key The key to write to [string] [required]
value The value to write [string]
Flags:
-c, --config Path to .toml configuration file [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Options:
--binding The binding of the namespace to write to [string]
--namespace-id The id of the namespace to write to [string]
-e, --env Perform on a specific environment [string]
--preview Interact with a preview namespace [boolean]
--ttl Time for which the entries should be visible [number]
--expiration Time since the UNIX epoch after which the entry expires [number]
--path Read value from the file at a given path [string]"
`);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mNot enough non-option arguments: got 0, need at least 1[0m
"
`);
});
it("should error if no binding nor namespace is provided", async () => {
await expect(
runWrangler("kv:key put foo bar")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Exactly one of the arguments binding and namespace-id is required"`
);
expect(std.out).toMatchInlineSnapshot(`
"
wrangler kv:key put <key> [value]
Writes a single key/value pair to the given namespace.
Positionals:
key The key to write to [string] [required]
value The value to write [string]
Flags:
-c, --config Path to .toml configuration file [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Options:
--binding The binding of the namespace to write to [string]
--namespace-id The id of the namespace to write to [string]
-e, --env Perform on a specific environment [string]
--preview Interact with a preview namespace [boolean]
--ttl Time for which the entries should be visible [number]
--expiration Time since the UNIX epoch after which the entry expires [number]
--path Read value from the file at a given path [string]"
`);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mExactly one of the arguments binding and namespace-id is required[0m
"
`);
});
it("should error if both binding and namespace is provided", async () => {
await expect(
runWrangler("kv:key put foo bar --binding x --namespace-id y")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Arguments binding and namespace-id are mutually exclusive"`
);
expect(std.out).toMatchInlineSnapshot(`
"
wrangler kv:key put <key> [value]
Writes a single key/value pair to the given namespace.
Positionals:
key The key to write to [string] [required]
value The value to write [string]
Flags:
-c, --config Path to .toml configuration file [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Options:
--binding The binding of the namespace to write to [string]
--namespace-id The id of the namespace to write to [string]
-e, --env Perform on a specific environment [string]
--preview Interact with a preview namespace [boolean]
--ttl Time for which the entries should be visible [number]
--expiration Time since the UNIX epoch after which the entry expires [number]
--path Read value from the file at a given path [string]"
`);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mArguments binding and namespace-id are mutually exclusive[0m
"
`);
});
it("should error if no value nor path is provided", async () => {
await expect(
runWrangler("kv:key put key --namespace-id 12345")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Exactly one of the arguments value and path is required"`
);
expect(std.out).toMatchInlineSnapshot(`
"
wrangler kv:key put <key> [value]
Writes a single key/value pair to the given namespace.
Positionals:
key The key to write to [string] [required]
value The value to write [string]
Flags:
-c, --config Path to .toml configuration file [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Options:
--binding The binding of the namespace to write to [string]
--namespace-id The id of the namespace to write to [string]
-e, --env Perform on a specific environment [string]
--preview Interact with a preview namespace [boolean]
--ttl Time for which the entries should be visible [number]
--expiration Time since the UNIX epoch after which the entry expires [number]
--path Read value from the file at a given path [string]"
`);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mExactly one of the arguments value and path is required[0m
"
`);
});
it("should error if both value and path is provided", async () => {
await expect(
runWrangler("kv:key put key value --path xyz --namespace-id 12345")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Arguments value and path are mutually exclusive"`
);
expect(std.out).toMatchInlineSnapshot(`
"
wrangler kv:key put <key> [value]
Writes a single key/value pair to the given namespace.
Positionals:
key The key to write to [string] [required]
value The value to write [string]
Flags:
-c, --config Path to .toml configuration file [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Options:
--binding The binding of the namespace to write to [string]
--namespace-id The id of the namespace to write to [string]
-e, --env Perform on a specific environment [string]
--preview Interact with a preview namespace [boolean]
--ttl Time for which the entries should be visible [number]
--expiration Time since the UNIX epoch after which the entry expires [number]
--path Read value from the file at a given path [string]"
`);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mArguments value and path are mutually exclusive[0m
"
`);
});
it("should error if a given binding name is not in the configured kv namespaces", async () => {
writeWranglerConfig();
await expect(
runWrangler("kv:key put key value --binding otherBinding")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
);
expect(std.out).toMatchInlineSnapshot(`
"
[32mIf you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose[0m"
`);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mA namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".[0m
"
`);
});
it("should error if a given binding has both preview and non-preview and --preview is not specified", async () => {
writeWranglerConfig();
const requests = mockKeyPutRequest("preview-bound-id", {
key: "my-key",
value: "my-value",
});
await expect(
runWrangler("kv:key put my-key my-value --binding someBinding")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"someBinding has both a namespace ID and a preview ID. Specify \\"--preview\\" or \\"--preview false\\" to avoid writing data to the wrong namespace."`
);
expect(std.out).toMatchInlineSnapshot(`
"
[32mIf you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose[0m"
`);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1msomeBinding has both a namespace ID and a preview ID. Specify \\"--preview\\" or \\"--preview false\\" to avoid writing data to the wrong namespace.[0m
"
`);
expect(requests.count).toEqual(0);
});
});
describe("list", () => {
it("should list the keys of a namespace specified by namespace-id", async () => {
const keys = [
{ name: "key-1" },
{ name: "key-2", expiration: 123456789 },
{ name: "key-3", expiration_ttl: 666 },
];
mockKeyListRequest("some-namespace-id", keys);
await runWrangler("kv:key list --namespace-id some-namespace-id");
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.out).toMatchInlineSnapshot(`
"[
{
\\"name\\": \\"key-1\\"
},
{
\\"name\\": \\"key-2\\",
\\"expiration\\": 123456789
},
{
\\"name\\": \\"key-3\\",
\\"expiration_ttl\\": 666
}
]"
`);
});
it("should list the keys of a namespace specified by binding", async () => {
writeWranglerConfig();
const keys = [{ name: "key-1" }, { name: "key-2" }, { name: "key-3" }];
mockKeyListRequest("bound-id", keys);
await runWrangler("kv:key list --binding someBinding");
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.out).toMatchInlineSnapshot(`
"[
{
\\"name\\": \\"key-1\\"
},
{
\\"name\\": \\"key-2\\"
},
{
\\"name\\": \\"key-3\\"
}
]"
`);
});
it("should list the keys of a preview namespace specified by binding", async () => {
writeWranglerConfig();
const keys = [{ name: "key-1" }, { name: "key-2" }, { name: "key-3" }];
mockKeyListRequest("preview-bound-id", keys);
await runWrangler("kv:key list --binding someBinding --preview");
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.out).toMatchInlineSnapshot(`
"[
{
\\"name\\": \\"key-1\\"
},
{
\\"name\\": \\"key-2\\"
},
{
\\"name\\": \\"key-3\\"
}
]"
`);
});
it("should list the keys of a namespace specified by binding, in a given environment", async () => {
writeWranglerConfig();
const keys = [{ name: "key-1" }, { name: "key-2" }, { name: "key-3" }];
mockKeyListRequest("env-bound-id", keys);
await runWrangler(
"kv:key list --binding someBinding --env some-environment"
);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.out).toMatchInlineSnapshot(`
"[
{
\\"name\\": \\"key-1\\"
},
{
\\"name\\": \\"key-2\\"
},
{
\\"name\\": \\"key-3\\"
}
]"
`);
});
it("should list the keys of a preview namespace specified by binding, in a given environment", async () => {
writeWranglerConfig();
const keys = [{ name: "key-1" }, { name: "key-2" }, { name: "key-3" }];
mockKeyListRequest("preview-env-bound-id", keys);
await runWrangler(
"kv:key list --binding someBinding --preview --env some-environment"
);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.out).toMatchInlineSnapshot(`
"[
{
\\"name\\": \\"key-1\\"
},
{
\\"name\\": \\"key-2\\"
},
{
\\"name\\": \\"key-3\\"
}
]"
`);
});
// We'll run the next test with variations on the cursor
// that's returned on cloudflare's API after all results
// have been drained.
for (const blankCursorValue of [undefined, null, ""] as [
undefined,
null,
""
]) {
describe(`cursor - ${blankCursorValue}`, () => {
it("should make multiple requests for paginated results", async () => {
// Create a lot of mock keys, so that the fetch requests will be paginated
const keys: NamespaceKeyInfo[] = [];
for (let i = 0; i < 550; i++) {
keys.push({ name: "key-" + i });
}
// Ask for the keys in pages of size 100.
const requests = mockKeyListRequest(
"some-namespace-id",
keys,
100,
blankCursorValue
);
await runWrangler("kv:key list --namespace-id some-namespace-id");
expect(std.err).toMatchInlineSnapshot(`""`);
expect(JSON.parse(std.out)).toEqual(keys);
expect(requests.count).toEqual(6);
});
});
}
it("should error if a given binding name is not in the configured kv namespaces", async () => {
writeWranglerConfig();
await expect(
runWrangler("kv:key list --binding otherBinding")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mA namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".[0m
"
`);
expect(std.out).toMatchInlineSnapshot(`
"
[32mIf you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose[0m"
`);
});
});
describe("get", () => {
afterEach(() => {
unsetMockFetchKVGetValues();
});
it("should get a key in a given namespace specified by namespace-id", async () => {
setMockFetchKVGetValue(
"some-account-id",
"some-namespace-id",
"my-key",
"my-value"
);
await runWrangler("kv:key get my-key --namespace-id some-namespace-id");
expect(std.out).toMatchInlineSnapshot(`"my-value"`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
it("should get a key in a given namespace specified by binding", async () => {
writeWranglerConfig();
setMockFetchKVGetValue(
"some-account-id",
"bound-id",
"my-key",
"my-value"
);
await runWrangler(
"kv:key get my-key --binding someBinding --preview false"
);
expect(std.out).toMatchInlineSnapshot(`"my-value"`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
it("should get a key in a given preview namespace specified by binding", async () => {
writeWranglerConfig();
setMockFetchKVGetValue(
"some-account-id",
"preview-bound-id",
"my-key",
"my-value"
);
await runWrangler("kv:key get my-key --binding someBinding --preview");
expect(std.out).toMatchInlineSnapshot(`"my-value"`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
it("should get a key for the specified environment in a given namespace", async () => {
writeWranglerConfig();
setMockFetchKVGetValue(
"some-account-id",
"env-bound-id",
"my-key",
"my-value"
);
await runWrangler(
"kv:key get my-key --binding someBinding --env some-environment --preview false"
);
expect(std.out).toMatchInlineSnapshot(`"my-value"`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
it("should encode the key in the api request to get a value", async () => {
setMockFetchKVGetValue(
"some-account-id",
"some-namespace-id",
"%2Fmy%2Ckey",
"my-value"
);
await runWrangler(
"kv:key get /my,key --namespace-id some-namespace-id"
);
expect(std.out).toMatchInlineSnapshot(`"my-value"`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
it("should error if no key is provided", async () => {
await expect(
runWrangler("kv:key get")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Not enough non-option arguments: got 0, need at least 1"`
);
expect(std.out).toMatchInlineSnapshot(`
"
wrangler kv:key get <key>
Reads a single value by key from the given namespace.
Positionals:
key The key value to get. [string] [required]
Flags:
-c, --config Path to .toml configuration file [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Options:
--binding The name of the namespace to get from [string]
--namespace-id The id of the namespace to get from [string]
-e, --env Perform on a specific environment [string]
--preview Interact with a preview namespace [boolean] [default: false]"
`);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mNot enough non-option arguments: got 0, need at least 1[0m
"
`);
});
it("should error if no binding nor namespace is provided", async () => {
await expect(
runWrangler("kv:key get foo")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Exactly one of the arguments binding and namespace-id is required"`
);
expect(std.out).toMatchInlineSnapshot(`
"
wrangler kv:key get <key>
Reads a single value by key from the given namespace.
Positionals:
key The key value to get. [string] [required]
Flags:
-c, --config Path to .toml configuration file [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Options:
--binding The name of the namespace to get from [string]
--namespace-id The id of the namespace to get from [string]
-e, --env Perform on a specific environment [string]
--preview Interact with a preview namespace [boolean] [default: false]"
`);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mExactly one of the arguments binding and namespace-id is required[0m
"