diff --git a/test/fixture/api/storage/[...base].get.ts b/test/fixture/api/storage/[...base].get.ts deleted file mode 100644 index cfa60c46d5..0000000000 --- a/test/fixture/api/storage/[...base].get.ts +++ /dev/null @@ -1,11 +0,0 @@ -export default eventHandler(async (event) => { - const { key } = getQuery(event) - const base = event.context.params.base as string - const storage = useStorage(`cache:${base}`) - if (!key) { - return await storage.getKeys() - } - - const value = await storage.getItem(key as string) - return { key, value } -}) diff --git a/test/fixture/api/storage/[...base].put.ts b/test/fixture/api/storage/[...base].put.ts deleted file mode 100644 index 03c36ba91f..0000000000 --- a/test/fixture/api/storage/[...base].put.ts +++ /dev/null @@ -1,9 +0,0 @@ -export default eventHandler(async (event) => { - const { key, value } = await readBody(event) - const base = event.context.params.base as string - const storage = useStorage(`cache:${base}`) - - await storage.setItem(key, value) - - return { key, value } -}) diff --git a/test/fixture/api/storage/item.get.ts b/test/fixture/api/storage/item.get.ts new file mode 100644 index 0000000000..24ee602aca --- /dev/null +++ b/test/fixture/api/storage/item.get.ts @@ -0,0 +1,11 @@ +export default eventHandler(async (event) => { + const { base = "", key = "" } = getQuery(event) as Record; + const storage = useStorage(`test:${base}`); + + if (!key || key.endsWith(":")) { + return await storage.getKeys(); + } + + const value = await storage.getItem(key); + return value; +}); diff --git a/test/fixture/api/storage/item.put.ts b/test/fixture/api/storage/item.put.ts new file mode 100644 index 0000000000..8e106dd859 --- /dev/null +++ b/test/fixture/api/storage/item.put.ts @@ -0,0 +1,7 @@ +export default eventHandler(async (event) => { + const { base = "", key = "" } = getQuery(event) as Record; + const storage = useStorage(`test:${base}`); + const value = await readBody(event); + await storage.setItem(key, value); + return value; +}); diff --git a/test/presets/node.test.ts b/test/presets/node.test.ts index 0496f0ebd6..37e07d8c20 100644 --- a/test/presets/node.test.ts +++ b/test/presets/node.test.ts @@ -7,8 +7,8 @@ describe("nitro:preset:node", async () => { testNitro(ctx, async () => { const { listener } = await import(resolve(ctx.outDir, "server/index.mjs")); await startServer(ctx, listener); - return async ({ url }) => { - const res = await ctx.fetch(url); + return async ({ url, ...opts }) => { + const res = await ctx.fetch(url, opts); return res; }; }); diff --git a/test/presets/vercel.test.ts b/test/presets/vercel.test.ts index ac1f157d37..59ae5130a3 100644 --- a/test/presets/vercel.test.ts +++ b/test/presets/vercel.test.ts @@ -13,8 +13,8 @@ describe("nitro:preset:vercel", async () => { resolve(ctx.outDir, "functions/__nitro.func/index.mjs") ).then((r) => r.default || r); await startServer(ctx, handle); - return async ({ url }) => { - const res = await ctx.fetch(url); + return async ({ url, ...options }) => { + const res = await ctx.fetch(url, options); return res; }; }, diff --git a/test/tests.ts b/test/tests.ts index b35972064e..8ad834776f 100644 --- a/test/tests.ts +++ b/test/tests.ts @@ -1,9 +1,8 @@ import { resolve } from "pathe"; import { listen, Listener } from "listhen"; import destr from "destr"; -import { fetch } from "ofetch"; +import { fetch, FetchOptions } from "ofetch"; import { expect, it, afterAll } from "vitest"; -import { isWindows } from "std-env"; import { fileURLToPath } from "mlly"; import { joinURL } from "ufo"; import * as _nitro from "../src"; @@ -17,7 +16,7 @@ export interface Context { nitro?: Nitro; rootDir: string; outDir: string; - fetch: (url: string) => Promise; + fetch: (url: string, opts?: FetchOptions) => Promise; server?: Listener; isDev: boolean; } @@ -30,8 +29,11 @@ export async function setupTest(preset: string) { isDev: preset === "nitro-dev", rootDir: fixtureDir, outDir: resolve(fixtureDir, ".output", preset), - fetch: (url) => - fetch(joinURL(ctx.server!.url, url.slice(1)), { redirect: "manual" }), + fetch: (url, opts) => + fetch(joinURL(ctx.server!.url, url.slice(1)), { + redirect: "manual", + ...(opts as any), + }), }; const nitro = (ctx.nitro = await createNitro({ @@ -273,17 +275,37 @@ export function testNitro( }); }); - it("supports useStorage(base)", async () => { - const { data } = await callHandler({ url: "/api/storage/test" }); - expect(data).toMatchObject([]); - const { data: res } = await callHandler({ - url: '/api/storage/test', - method: 'PUT', - body: { key: 'hello', value: 'world' } + it("useStorage (with base)", async () => { + const putRes = await callHandler({ + url: "/api/storage/item?key=test:hello", + method: "PUT", + body: "world", }); - expect(res.key).toBe('hello'); - const { data: keys } = await callHandler({ url: "/api/storage/test" }); - expect(data).toMatchObject(['hello']); + expect(putRes.data).toMatchObject("world"); + + expect( + ( + await callHandler({ + url: "/api/storage/item?key=:", + }) + ).data + ).toMatchObject(["test:hello"]); + + expect( + ( + await callHandler({ + url: "/api/storage/item?base=test&key=:", + }) + ).data + ).toMatchObject(["hello"]); + + expect( + ( + await callHandler({ + url: "/api/storage/item?base=test&key=hello", + }) + ).data + ).toBe("world"); }); if (additionalTests) {