-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathpages-plugin.test.ts
More file actions
123 lines (104 loc) · 4.13 KB
/
pages-plugin.test.ts
File metadata and controls
123 lines (104 loc) · 4.13 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Note: These tests run the handler in Node.js, which has some differences to the cloudflare workers runtime.
// Although this is not ideal, this is the best we can do until we have a better way to test cloudflare workers.
import { beforeEach, describe, expect, test, vi } from 'vitest';
import type { CloudflareOptions } from '../src/client';
import { sentryPagesPlugin } from '../src/pages-plugin';
const MOCK_OPTIONS: CloudflareOptions = {
dsn: 'https://public@dsn.ingest.sentry.io/1337',
};
describe('sentryPagesPlugin', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('calls handler function if a function is provided', async () => {
const mockOptionsHandler = vi.fn().mockReturnValue(MOCK_OPTIONS);
const mockOnRequest = sentryPagesPlugin(mockOptionsHandler);
const MOCK_CONTEXT = {
request: new Request('https://example.com'),
functionPath: 'test',
waitUntil: vi.fn(),
passThroughOnException: vi.fn(),
next: () => Promise.resolve(new Response('test')),
env: { ASSETS: { fetch: vi.fn() } },
params: {},
data: {},
pluginArgs: MOCK_OPTIONS,
};
await mockOnRequest(MOCK_CONTEXT);
expect(mockOptionsHandler).toHaveBeenCalledTimes(1);
expect(mockOptionsHandler).toHaveBeenLastCalledWith(MOCK_CONTEXT);
});
test('passes through the response from the handler', async () => {
const response = new Response('test');
const mockOnRequest = sentryPagesPlugin(MOCK_OPTIONS);
const result = await mockOnRequest({
request: new Request('https://example.com'),
functionPath: 'test',
waitUntil: vi.fn(),
passThroughOnException: vi.fn(),
next: () => Promise.resolve(response),
env: { ASSETS: { fetch: vi.fn() } },
params: {},
data: {},
pluginArgs: MOCK_OPTIONS,
});
// Response may be wrapped for streaming detection, verify content
expect(result.status).toBe(response.status);
expect(await result.text()).toBe('test');
});
test('OPTIONS requests bypass SDK instrumentation', async () => {
const mockOptionsHandler = vi.fn().mockReturnValue(MOCK_OPTIONS);
const mockOnRequest = sentryPagesPlugin(mockOptionsHandler);
const mockNext = vi.fn().mockResolvedValue(new Response('options response'));
const result = await mockOnRequest({
request: new Request('https://example.com', { method: 'OPTIONS' }),
functionPath: 'test',
waitUntil: vi.fn(),
passThroughOnException: vi.fn(),
next: mockNext,
env: { ASSETS: { fetch: vi.fn() } },
params: {},
data: {},
pluginArgs: MOCK_OPTIONS,
});
expect(mockOptionsHandler).not.toHaveBeenCalled();
expect(mockNext).toHaveBeenCalledTimes(1);
expect(result.status).toBe(200);
});
test('HEAD requests bypass SDK instrumentation', async () => {
const mockOptionsHandler = vi.fn().mockReturnValue(MOCK_OPTIONS);
const mockOnRequest = sentryPagesPlugin(mockOptionsHandler);
const mockNext = vi.fn().mockResolvedValue(new Response('head response'));
const result = await mockOnRequest({
request: new Request('https://example.com', { method: 'HEAD' }),
functionPath: 'test',
waitUntil: vi.fn(),
passThroughOnException: vi.fn(),
next: mockNext,
env: { ASSETS: { fetch: vi.fn() } },
params: {},
data: {},
pluginArgs: MOCK_OPTIONS,
});
expect(mockOptionsHandler).not.toHaveBeenCalled();
expect(mockNext).toHaveBeenCalledTimes(1);
expect(result.status).toBe(200);
});
test('GET requests are instrumented', async () => {
const mockOptionsHandler = vi.fn().mockReturnValue(MOCK_OPTIONS);
const mockOnRequest = sentryPagesPlugin(mockOptionsHandler);
const mockNext = vi.fn().mockResolvedValue(new Response('get response'));
await mockOnRequest({
request: new Request('https://example.com', { method: 'GET' }),
functionPath: 'test',
waitUntil: vi.fn(),
passThroughOnException: vi.fn(),
next: mockNext,
env: { ASSETS: { fetch: vi.fn() } },
params: {},
data: {},
pluginArgs: MOCK_OPTIONS,
});
expect(mockOptionsHandler).toHaveBeenCalledTimes(1);
});
});