Repository navigation
Expand file tree
/
Copy pathscoping.test.ts
More file actions
58 lines (52 loc) · 1.98 KB
/
Copy pathscoping.test.ts
File metadata and controls
58 lines (52 loc) · 1.98 KB
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
import { describe, expect, it } from "vitest";
import { createExpressApp, listenExpress } from "../src/express.js";
import { createFastifyApp } from "../src/fastify.js";
import { Ledger } from "../src/webhook.js";
/**
* The raw body is the webhook's business and nobody else's.
*
* Both recipes say the rest of the application keeps its JSON parser. That is
* what makes them safe to paste into an existing app, and it is the part of
* the recipe a reader is least likely to check, so it is checked here: a
* second route, added the way an application would add one, still receives
* a parsed object.
*/
const order = JSON.stringify({ orderId: "ord_1" });
describe("the raw body stays scoped to the webhook route", () => {
it("Express", async () => {
const app = createExpressApp(new Ledger());
app.post("/orders", (request, response) => {
response.json({ parsed: !Buffer.isBuffer(request.body) && request.body?.orderId });
});
const running = await listenExpress(app);
try {
const response = await fetch(`${running.baseUrl}/orders`, {
method: "POST",
headers: { "content-type": "application/json" },
body: order,
});
expect(await response.json()).toEqual({ parsed: "ord_1" });
} finally {
await running.close();
}
});
it("Fastify", async () => {
const app = createFastifyApp(new Ledger());
// Registered at the root, outside the webhook plugin, as an application's
// other routes would be.
app.post("/orders", async (request) => ({
parsed: !Buffer.isBuffer(request.body) && (request.body as { orderId?: string }).orderId,
}));
const baseUrl = await app.listen({ port: 0, host: "127.0.0.1" });
try {
const response = await fetch(`${baseUrl}/orders`, {
method: "POST",
headers: { "content-type": "application/json" },
body: order,
});
expect(await response.json()).toEqual({ parsed: "ord_1" });
} finally {
await app.close();
}
});
});