-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.test.ts
126 lines (99 loc) · 3.05 KB
/
plugin.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { describe, expect, it } from "vitest";
import ironSession from "./plugin";
import fastify from "fastify";
import { unsealData } from "iron-session";
const testCookieName = "test";
const testPassword = "at-least-32-characters-long-secret";
function setupApp() {
const app = fastify();
app.register(ironSession, {
cookieName: testCookieName,
password: testPassword,
});
return app;
}
describe("plugin", () => {
const testApp = fastify();
testApp.register(ironSession, {
cookieName: testCookieName,
password: testPassword,
});
testApp.get("/", async (request, reply) => {
return { hello: "world" };
});
describe("initialization", () => {
it("should be able to register the plugin", async () => {
const app = fastify();
await app.register(ironSession, {
cookieName: testCookieName,
password: testPassword,
});
expect(app.hasPlugin("iron-session")).toBeTruthy();
});
it("should be able to register plugin with different session names", async () => {
const app = fastify();
await app.register(ironSession, [
{
cookieName: testCookieName,
password: testPassword,
},
{
sessionName: "another-session",
cookieName: "another-session",
password: testPassword,
},
]);
expect(app.hasPlugin("iron-session")).toBeTruthy();
});
});
describe("requests", () => {
it("should be able to complete a request", async () => {
const response = await testApp.inject({
method: "GET",
url: "/",
});
expect(response.statusCode).toBe(200);
});
it("should not have a null session", async () => {
const _app = setupApp();
_app.get("/session1", async (req, reply) => {
expect(await req.session()).not.toBeNull();
return { hello: "world" };
});
const res = await _app.inject({
method: "GET",
url: "/session1",
});
expect(res.statusCode).toBe(200);
});
it("should be able to set a session value", async () => {
const _app = setupApp();
_app.get("/session2", async (req, reply) => {
const session = await req.session();
session.id = "123";
await session.save();
return { hello: "world" };
});
// Will throw an error within the request and return a 500 if the session is not set
_app.get("/session-check", async (req, reply) => {
expect((await req.session()).id).toBe("123");
return { hello: "world" };
});
const res = await _app.inject({
method: "GET",
url: "/session2",
});
const res2 = await _app.inject({
method: "GET",
url: "/session-check",
// Need to pass the cookies from the previous request
cookies: {
[testCookieName]: res.cookies[0].value,
},
});
expect(res.statusCode).toBe(200);
expect(res.headers["set-cookie"]).toHaveProperty("length");
expect(res2.statusCode).toBe(200);
});
});
});