-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { withValidation } from "./validation"; | ||
import { describe, it, expect, vi } from "vitest"; | ||
import { newZodValidator, ZodApiEndpoints } from "../zod"; | ||
import { z } from "zod"; | ||
import { toLCObj } from "../utils"; | ||
|
||
const newFetch = ( | ||
body: Record<string, string>, | ||
headers: Record<string, string>, | ||
) => { | ||
return vi.fn( | ||
() => new Response(JSON.stringify(body), { headers: toLCObj(headers) }), | ||
) as unknown as typeof fetch; | ||
}; | ||
describe("withValidation", () => { | ||
const pathMap = { | ||
"/:paramsName": { | ||
get: { | ||
query: z.object({ queryName: z.string() }), | ||
headers: z.object(toLCObj({ headersName: z.string() })), | ||
params: z.object({ paramsName: z.string() }), | ||
responses: { | ||
200: { | ||
body: z.object({ bodyNameRes: z.string() }), | ||
headers: z.object(toLCObj({ headersNameRes: z.string() })), | ||
}, | ||
400: { body: z.object({ message: z.string() }) }, | ||
}, | ||
}, | ||
post: { | ||
body: z.object({ bodyName: z.string() }), | ||
responses: { 200: { body: z.object({ bodyNameRes: z.string() }) } }, | ||
}, | ||
}, | ||
} satisfies ZodApiEndpoints; | ||
const path = "/p?queryName=q"; | ||
describe("invalid request", () => { | ||
const ft = newFetch({ bodyNameRes: "b" }, { headersNameRes: "h" }); | ||
const { req, res } = newZodValidator(pathMap); | ||
const fetchV = withValidation(ft, pathMap, req, res); | ||
it("query", async () => { | ||
await expect(() => | ||
fetchV("/p", { headers: { headersName: "h" } }), | ||
).rejects.toThrow( | ||
'{"reason":"query","issues":[{"code":"invalid_type","expected":"string","received":"undefined","path":["queryName"],"message":"Required"}],"name":"ZodError"}', | ||
); | ||
}); | ||
it("headers", async () => { | ||
await expect(() => fetchV(path, { headers: {} })).rejects.toThrow( | ||
'{"reason":"headers","issues":[{"code":"invalid_type","expected":"string","received":"undefined","path":["headersname"],"message":"Required"}],"name":"ZodError"}', | ||
); | ||
}); | ||
it("body", async () => { | ||
await expect(() => | ||
fetchV(path, { | ||
method: "post", | ||
body: "{}", | ||
headers: { headersName: "h" }, | ||
}), | ||
).rejects.toThrow( | ||
'{"reason":"body","issues":[{"code":"invalid_type","expected":"string","received":"undefined","path":["bodyName"],"message":"Required"}],"name":"ZodError"}', | ||
); | ||
}); | ||
}); | ||
describe("status code 200", () => { | ||
it("valid response", async () => { | ||
const ft = newFetch({ bodyNameRes: "b" }, { headersNameRes: "h" }); | ||
const { req, res } = newZodValidator(pathMap); | ||
const fetchV = withValidation(ft, pathMap, req, res); | ||
const r = await fetchV(path, { headers: { headersName: "h" } }); | ||
expect(await r.json()).toEqual({ bodyNameRes: "b" }); | ||
}); | ||
describe("invalid response", () => { | ||
it("body", async () => { | ||
const ft = newFetch({ invalid: "invalid" }, { headersNameRes: "h" }); | ||
const { req, res } = newZodValidator(pathMap); | ||
const fetchV = withValidation(ft, pathMap, req, res); | ||
await expect(() => | ||
fetchV(path, { headers: { headersName: "h" } }), | ||
).rejects.toThrow( | ||
'{"reason":"body","issues":[{"code":"invalid_type","expected":"string","received":"undefined","path":["bodyNameRes"],"message":"Required"}],"name":"ZodError"}', | ||
); | ||
}); | ||
it("headers", async () => { | ||
const ft = newFetch({ bodyNameRes: "b" }, { invalid: "invalid" }); | ||
const { req, res } = newZodValidator(pathMap); | ||
const fetchV = withValidation(ft, pathMap, req, res); | ||
await expect(() => | ||
fetchV(path, { headers: { headersName: "h" } }), | ||
).rejects.toThrow( | ||
'{"reason":"headers","issues":[{"code":"invalid_type","expected":"string","received":"undefined","path":["headersnameres"],"message":"Required"}],"name":"ZodError"}', | ||
); | ||
}); | ||
}); | ||
}); | ||
describe("status code 400", () => { | ||
it("valid response", async () => { | ||
const ft = newFetch({ message: "m" }, { headersNameRes: "h" }); | ||
const { req, res } = newZodValidator(pathMap); | ||
const fetchV = withValidation(ft, pathMap, req, res); | ||
const r = await fetchV(path, { headers: { headersName: "h" } }); | ||
expect(await r.json()).toEqual({ message: "m" }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters