-
Notifications
You must be signed in to change notification settings - Fork 413
/
Copy pathchunked-cookies.test.ts
361 lines (293 loc) Β· 11.7 KB
/
chunked-cookies.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
CookieOptions,
deleteChunkedCookie,
getChunkedCookie,
RequestCookies,
ResponseCookies,
setChunkedCookie
} from "./cookies";
// Create mock implementation for RequestCookies and ResponseCookies
const createMocks = () => {
const cookieStore = new Map();
const reqCookies = {
get: vi.fn((...args) => {
const name = typeof args[0] === "string" ? args[0] : args[0].name;
if (cookieStore.has(name)) {
return { name, value: cookieStore.get(name) };
}
return undefined;
}),
getAll: vi.fn((...args) => {
if (args.length === 0) {
return Array.from(cookieStore.entries()).map(([name, value]) => ({
name,
value
}));
}
const name = typeof args[0] === "string" ? args[0] : args[0].name;
return cookieStore.has(name)
? [{ name, value: cookieStore.get(name) }]
: [];
}),
has: vi.fn((name) => cookieStore.has(name)),
set: vi.fn((...args) => {
const name = typeof args[0] === "string" ? args[0] : args[0].name;
const value = typeof args[0] === "string" ? args[1] : args[0].value;
cookieStore.set(name, value);
return reqCookies;
}),
delete: vi.fn((names) => {
if (Array.isArray(names)) {
return names.map((name) => cookieStore.delete(name));
}
return cookieStore.delete(names);
}),
clear: vi.fn(() => {
cookieStore.clear();
return reqCookies;
}),
get size() {
return cookieStore.size;
},
[Symbol.iterator]: vi.fn(() => cookieStore.entries())
};
const resCookies = {
get: vi.fn((...args) => {
const name = typeof args[0] === "string" ? args[0] : args[0].name;
if (cookieStore.has(name)) {
return { name, value: cookieStore.get(name) };
}
return undefined;
}),
getAll: vi.fn((...args) => {
if (args.length === 0) {
return Array.from(cookieStore.entries()).map(([name, value]) => ({
name,
value
}));
}
const name = typeof args[0] === "string" ? args[0] : args[0].name;
return cookieStore.has(name)
? [{ name, value: cookieStore.get(name) }]
: [];
}),
has: vi.fn((name) => cookieStore.has(name)),
set: vi.fn((...args) => {
const name = typeof args[0] === "string" ? args[0] : args[0].name;
const value = typeof args[0] === "string" ? args[1] : args[0].value;
cookieStore.set(name, value);
return resCookies;
}),
delete: vi.fn((...args) => {
const name = typeof args[0] === "string" ? args[0] : args[0].name;
cookieStore.delete(name);
return resCookies;
}),
toString: vi.fn(() => {
return Array.from(cookieStore.entries())
.map(([name, value]) => `${name}=${value}`)
.join("; ");
})
};
return { reqCookies, resCookies, cookieStore };
};
describe("Chunked Cookie Utils", () => {
let reqCookies: RequestCookies;
let resCookies: ResponseCookies;
let cookieStore: Map<any, any>;
beforeEach(() => {
const mocks = createMocks();
reqCookies = mocks.reqCookies;
resCookies = mocks.resCookies;
cookieStore = mocks.cookieStore;
// Spy on console.warn
vi.spyOn(console, "warn").mockImplementation(() => {});
});
afterEach(() => {
vi.clearAllMocks();
});
describe("setChunkedCookie", () => {
it("should set a single cookie when value is small enough", () => {
const name = "testCookie";
const value = "small value";
const options = { path: "/" } as CookieOptions;
setChunkedCookie(name, value, options, reqCookies, resCookies);
expect(resCookies.set).toHaveBeenCalledTimes(1);
expect(resCookies.set).toHaveBeenCalledWith(name, value, options);
expect(reqCookies.set).toHaveBeenCalledTimes(1);
expect(reqCookies.set).toHaveBeenCalledWith(name, value);
});
it("should split cookie into chunks when value exceeds max size", () => {
const name = "largeCookie";
const options = { path: "/" } as CookieOptions;
// Create a large string (8000 bytes)
const largeValue = "a".repeat(8000);
setChunkedCookie(name, largeValue, options, reqCookies, resCookies);
// Should create 3 chunks (8000 / 3500 β 2.3, rounded up to 3)
expect(resCookies.set).toHaveBeenCalledTimes(3);
expect(reqCookies.set).toHaveBeenCalledTimes(3);
// Check first chunk
expect(resCookies.set).toHaveBeenCalledWith(
`${name}__0`,
largeValue.slice(0, 3500),
options
);
// Check second chunk
expect(resCookies.set).toHaveBeenCalledWith(
`${name}__1`,
largeValue.slice(3500, 7000),
options
);
// Check third chunk
expect(resCookies.set).toHaveBeenCalledWith(
`${name}__2`,
largeValue.slice(7000),
options
);
});
it("should log a warning when cookie size exceeds warning threshold", () => {
const name = "warningCookie";
const options = { path: "/" } as CookieOptions;
// Create a value that exceeds the warning threshold (4096 bytes)
const value = "a".repeat(4097);
setChunkedCookie(name, value, options, reqCookies, resCookies);
expect(console.warn).toHaveBeenCalled();
});
describe("getChunkedCookie", () => {
it("should return undefined when cookie does not exist", () => {
const result = getChunkedCookie("nonexistent", reqCookies);
expect(result).toBeUndefined();
});
it("should return cookie value when it exists as a regular cookie", () => {
const name = "simpleCookie";
const value = "simple value";
// Setup the cookie
cookieStore.set(name, value);
const result = getChunkedCookie(name, reqCookies);
expect(result).toBe(value);
expect(reqCookies.get).toHaveBeenCalledWith(name);
});
it("should reconstruct value from chunks when cookie is chunked", () => {
const name = "chunkedCookie";
const chunk0 = "chunk0 value";
const chunk1 = "chunk1 value";
const chunk2 = "chunk2 value";
// Add the chunks to the store (out of order)
cookieStore.set(`${name}__1`, chunk1);
cookieStore.set(`${name}__0`, chunk0);
cookieStore.set(`${name}__2`, chunk2);
// Also add some unrelated cookies
cookieStore.set("otherCookie", "other value");
const result = getChunkedCookie(name, reqCookies);
// Should combine chunks in proper order
expect(result).toBe(`${chunk0}${chunk1}${chunk2}`);
});
it("should return undefined when chunks are not in a complete sequence", () => {
const name = "incompleteCookie";
// Add incomplete chunks (missing chunk1)
cookieStore.set(`${name}__0`, "chunk0");
cookieStore.set(`${name}__2`, "chunk2");
const result = getChunkedCookie(name, reqCookies);
expect(result).toBeUndefined();
expect(console.warn).toHaveBeenCalled();
});
});
describe("deleteChunkedCookie", () => {
it("should delete the regular cookie", () => {
const name = "regularCookie";
cookieStore.set(name, "regular value");
deleteChunkedCookie(name, reqCookies, resCookies);
expect(resCookies.delete).toHaveBeenCalledWith(name);
});
it("should delete all chunks of a cookie", () => {
const name = "chunkedCookie";
// Add chunks
cookieStore.set(`${name}__0`, "chunk0");
cookieStore.set(`${name}__1`, "chunk1");
cookieStore.set(`${name}__2`, "chunk2");
// Add unrelated cookie
cookieStore.set("otherCookie", "other value");
deleteChunkedCookie(name, reqCookies, resCookies);
// Should delete main cookie and 3 chunks
expect(resCookies.delete).toHaveBeenCalledTimes(4);
expect(resCookies.delete).toHaveBeenCalledWith(name);
expect(resCookies.delete).toHaveBeenCalledWith(`${name}__0`);
expect(resCookies.delete).toHaveBeenCalledWith(`${name}__1`);
expect(resCookies.delete).toHaveBeenCalledWith(`${name}__2`);
// Should not delete unrelated cookies
expect(resCookies.delete).not.toHaveBeenCalledWith("otherCookie");
});
});
describe("Edge Cases", () => {
it("should handle empty values correctly", () => {
const name = "emptyCookie";
const value = "";
const options = { path: "/" } as CookieOptions;
setChunkedCookie(name, value, options, reqCookies, resCookies);
expect(resCookies.set).toHaveBeenCalledTimes(1);
expect(resCookies.set).toHaveBeenCalledWith(name, value, options);
});
it("should handle values at the exact chunk boundary", () => {
const name = "boundaryValueCookie";
const value = "a".repeat(3500); // Exactly MAX_CHUNK_SIZE
const options = { path: "/" } as CookieOptions;
setChunkedCookie(name, value, options, reqCookies, resCookies);
// Should still fit in one cookie
expect(resCookies.set).toHaveBeenCalledTimes(1);
expect(resCookies.set).toHaveBeenCalledWith(name, value, options);
});
it("should handle special characters in cookie values", () => {
const name = "specialCharCookie";
const value =
'{"special":"characters","with":"quotation marks","and":"π emoji"}';
const options = { path: "/" } as CookieOptions;
setChunkedCookie(name, value, options, reqCookies, resCookies);
expect(resCookies.set).toHaveBeenCalledWith(name, value, options);
// Setup for retrieval
cookieStore.set(name, value);
const result = getChunkedCookie(name, reqCookies);
expect(result).toBe(value);
});
it("should handle multi-byte characters correctly", () => {
const name = "multiByteCookie";
// Create a test string with multi-byte characters (emojis)
const value = "Hello π world π with emojis π";
const options = { path: "/" } as CookieOptions;
// Store the cookie
setChunkedCookie(name, value, options, reqCookies, resCookies);
// For the retrieval test, manually set up the cookies
// We're testing the retrieval functionality, not the chunking itself
cookieStore.clear();
cookieStore.set(name, value);
// Verify retrieval works correctly with multi-byte characters
const result = getChunkedCookie(name, reqCookies);
expect(result).toBe(value);
// Verify emoji characters were preserved
expect(result).toContain("π");
expect(result).toContain("π");
expect(result).toContain("π");
});
it("should handle very large cookies properly", () => {
const name = "veryLargeCookie";
const value = "a".repeat(10000); // Will create multiple chunks
const options = { path: "/" } as CookieOptions;
setChunkedCookie(name, value, options, reqCookies, resCookies);
// Get chunks count (10000 / 3500 β 2.86, so we need 3 chunks)
const expectedChunks = Math.ceil(10000 / 3500);
expect(resCookies.set).toHaveBeenCalledTimes(expectedChunks);
// Clear and set up cookies for retrieval test
cookieStore.clear();
// Setup for getChunkedCookie retrieval
for (let i = 0; i < expectedChunks; i++) {
const start = i * 3500;
const end = Math.min((i + 1) * 3500, 10000);
cookieStore.set(`${name}__${i}`, value.slice(start, end));
}
const result = getChunkedCookie(name, reqCookies);
expect(result).toBe(value);
expect(result!.length).toBe(10000);
});
});
});
});