Skip to content

Commit 41e7b4c

Browse files
authored
chore(miniflare): Add KV ArrayBuffer & stream tests (#13561)
1 parent ab43203 commit 41e7b4c

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

packages/miniflare/test/plugins/kv/index.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,27 @@ test("get: returns value", async ({ expect }) => {
117117
expect(result).toBe("value");
118118
});
119119

120+
test("get: returns ArrayBuffer values", async ({ expect }) => {
121+
const { kv } = ctx;
122+
const bytes = Uint8Array.from([0, 1, 2, 127, 128, 254, 255]);
123+
await kv.put("array-buffer-key", bytes.buffer);
124+
125+
const result = await kv.get("array-buffer-key", "arrayBuffer");
126+
expect(result).not.toBeNull();
127+
expect(new Uint8Array(result as NonNullable<typeof result>)).toEqual(bytes);
128+
});
129+
130+
test("get: returns stream values", async ({ expect }) => {
131+
const { kv } = ctx;
132+
const bytes = Uint8Array.from([255, 0, 10, 20, 30, 200]);
133+
await kv.put("stream-key", new Blob([bytes]).stream());
134+
135+
const result = await kv.get("stream-key", "stream");
136+
expect(new Uint8Array(await new Response(result).arrayBuffer())).toEqual(
137+
bytes
138+
);
139+
});
140+
120141
test("bulk get: returns value", async ({ expect }) => {
121142
const { kv } = ctx;
122143
await kv.put("key1", "value1");

packages/miniflare/test/plugins/local-explorer/kv.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,19 @@ describe("KV API", () => {
260260
expect(response.status).toBe(200);
261261
expect(await response.text()).toBe("special-value");
262262
});
263+
264+
test("returns raw bytes for ArrayBuffer values", async ({ expect }) => {
265+
const kv = await mf.getKVNamespace("TEST_KV");
266+
const bytes = Uint8Array.from([0, 1, 2, 127, 128, 254, 255]);
267+
await kv.put("binary-get-key", bytes.buffer);
268+
269+
const response = await mf.dispatchFetch(
270+
`${BASE_URL}/storage/kv/namespaces/test-kv-id/values/binary-get-key`
271+
);
272+
273+
expect(response.status).toBe(200);
274+
expect(new Uint8Array(await response.arrayBuffer())).toEqual(bytes);
275+
});
263276
});
264277

265278
describe("PUT /storage/kv/namespaces/:namespaceId/values/:keyName", () => {
@@ -316,6 +329,33 @@ describe("KV API", () => {
316329
errors: [expect.objectContaining({ code: 10013 })],
317330
});
318331
});
332+
333+
test("writes streamed binary values", async ({ expect }) => {
334+
const bytes = Uint8Array.from([255, 0, 10, 20, 30, 200]);
335+
const response = await mf.dispatchFetch(
336+
`${BASE_URL}/storage/kv/namespaces/test-kv-id/values/put-stream-key`,
337+
{
338+
body: new Blob([bytes]).stream(),
339+
duplex: "half",
340+
headers: {
341+
"Content-Type": "application/octet-stream",
342+
},
343+
method: "PUT",
344+
}
345+
);
346+
347+
expect(response.status).toBe(200);
348+
expect(await response.json()).toMatchObject({ success: true });
349+
350+
const kv = await mf.getKVNamespace("TEST_KV");
351+
const stored = await kv.get("put-stream-key", { type: "arrayBuffer" });
352+
353+
expect(stored).not.toBeNull();
354+
if (stored === null) {
355+
throw new Error("Expected put-stream-key to be stored in KV");
356+
}
357+
expect(new Uint8Array(stored)).toEqual(bytes);
358+
});
319359
});
320360

321361
describe("DELETE /storage/kv/namespaces/:namespaceId/values/:keyName", () => {

0 commit comments

Comments
 (0)