-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathfetchEvent.spec.ts
More file actions
102 lines (76 loc) · 2.93 KB
/
fetchEvent.spec.ts
File metadata and controls
102 lines (76 loc) · 2.93 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
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
import * as h3 from "h3";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createFetchEvent, getFetchEvent, mergeResponseHeaders } from "./fetchEvent.ts";
vi.mock(import("h3"), async mod => {
return {
...(await mod()),
getRequestIP: vi.fn(),
};
});
const mockedH3 = vi.mocked(h3);
const createMockH3Event = (): h3.H3Event => {
const event = new h3.H3Event(new Request("http://localhost/test"));
event.res.status = 200;
event.res.statusText = "OK";
return event;
};
describe("fetchEvent", () => {
let mockH3Event: h3.H3Event;
beforeEach(() => {
mockH3Event = createMockH3Event();
vi.clearAllMocks();
mockedH3.getRequestIP.mockReturnValue("127.0.0.1");
});
describe("createFetchEvent", () => {
it("should create a FetchEvent from H3Event", () => {
const fetchEvent = createFetchEvent(mockH3Event);
expect(fetchEvent).toEqual({
request: mockH3Event.req,
response: expect.any(Object),
clientAddress: "127.0.0.1",
locals: {},
nativeEvent: mockH3Event,
});
});
it("should fall back to event.context.clientAddress when getRequestIP returns undefined", () => {
mockedH3.getRequestIP.mockReturnValue(undefined);
mockH3Event.context.clientAddress = "10.0.0.1";
const fetchEvent = createFetchEvent(mockH3Event);
expect(fetchEvent.clientAddress).toBe("10.0.0.1");
});
it("should have undefined clientAddress when neither getRequestIP nor event.context.clientAddress is set", () => {
mockedH3.getRequestIP.mockReturnValue(undefined);
const fetchEvent = createFetchEvent(mockH3Event);
expect(fetchEvent.clientAddress).toBeUndefined();
});
it("should create response stub with correct properties", () => {
const fetchEvent = createFetchEvent(mockH3Event);
expect(fetchEvent.response).toHaveProperty("status");
expect(fetchEvent.response).toHaveProperty("statusText");
expect(fetchEvent.response).toHaveProperty("headers");
});
});
describe("getFetchEvent", () => {
it("should create and cache FetchEvent on first call", () => {
const fetchEvent = getFetchEvent(mockH3Event);
expect(mockH3Event.context.solidFetchEvent).toBe(fetchEvent);
expect(fetchEvent.nativeEvent).toBe(mockH3Event);
});
it("should return cached FetchEvent on subsequent calls", () => {
const firstCall = getFetchEvent(mockH3Event);
const secondCall = getFetchEvent(mockH3Event);
expect(firstCall).toBe(secondCall);
});
});
describe("mergeResponseHeaders", () => {
it("should merge headers from Headers object to H3Event", () => {
const headers = new Headers({
"content-type": "application/json",
"x-custom": "value",
});
mergeResponseHeaders(mockH3Event, headers);
expect(headers.get("content-type")).toBe("application/json");
expect(headers.get("x-custom")).toBe("value");
});
});
});