forked from unjs/h3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.test.ts
68 lines (62 loc) · 1.53 KB
/
web.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
import { describe, it, expect, beforeEach } from "vitest";
import {
createApp,
App,
eventHandler,
WebHandler,
toWebHandler,
readBody,
} from "../src";
describe("Web handler", () => {
let app: App;
let handler: WebHandler;
beforeEach(() => {
app = createApp({ debug: true });
handler = toWebHandler(app);
});
it("works", async () => {
app.use(
"/test",
eventHandler(async (event) => {
const body =
event.method === "POST" ? await readBody(event) : undefined;
event.node.res.statusCode = 201;
event.node.res.statusMessage = "Created";
return {
method: event.method,
path: event.path,
headers: [...event.headers.entries()],
body,
contextKeys: Object.keys(event.context),
};
}),
);
const res = await handler(
new Request(new URL("/test/foo/bar", "http://localhost"), {
method: "POST",
headers: {
"X-Test": "true",
},
body: "request body",
}),
{
test: true,
},
);
expect(res.status).toBe(201);
expect(res.statusText).toBe("Created");
expect([...res.headers.entries()]).toMatchObject([
["content-type", "application/json"],
]);
expect(await res.json()).toMatchObject({
method: "POST",
path: "/foo/bar",
body: "request body",
headers: [
["content-type", "text/plain;charset=UTF-8"],
["x-test", "true"],
],
contextKeys: ["test"],
});
});
});