-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathfetch.test.ts
101 lines (86 loc) · 3.13 KB
/
fetch.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
import { EventEnvelope, EventItem } from '@sentry/types';
import { createEnvelope, serializeEnvelope } from '@sentry/utils';
import { TextEncoder } from 'util';
import { makeFetchTransport } from '../../../src/transports/fetch';
import { BrowserTransportOptions } from '../../../src/transports/types';
import { FetchImpl } from '../../../src/transports/utils';
const DEFAULT_FETCH_TRANSPORT_OPTIONS: BrowserTransportOptions = {
url: 'https://sentry.io/api/42/store/?sentry_key=123&sentry_version=7',
recordDroppedEvent: () => undefined,
textEncoder: new TextEncoder(),
};
const ERROR_ENVELOPE = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, [
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }] as EventItem,
]);
class Headers {
headers: { [key: string]: string } = {};
get(key: string) {
return this.headers[key] || null;
}
set(key: string, value: string) {
this.headers[key] = value;
}
}
describe('NewFetchTransport', () => {
it('calls fetch with the given URL', async () => {
const mockFetch = jest.fn(() =>
Promise.resolve({
headers: new Headers(),
status: 200,
text: () => Promise.resolve({}),
}),
) as unknown as FetchImpl;
const transport = makeFetchTransport(DEFAULT_FETCH_TRANSPORT_OPTIONS, mockFetch);
expect(mockFetch).toHaveBeenCalledTimes(0);
await transport.send(ERROR_ENVELOPE);
expect(mockFetch).toHaveBeenCalledTimes(1);
expect(mockFetch).toHaveBeenLastCalledWith(DEFAULT_FETCH_TRANSPORT_OPTIONS.url, {
body: serializeEnvelope(ERROR_ENVELOPE, new TextEncoder()),
method: 'POST',
keepalive: true,
referrerPolicy: 'origin',
});
});
it('sets rate limit headers', async () => {
const headers = {
get: jest.fn(),
};
const mockFetch = jest.fn(() =>
Promise.resolve({
headers,
status: 200,
text: () => Promise.resolve({}),
}),
) as unknown as FetchImpl;
const transport = makeFetchTransport(DEFAULT_FETCH_TRANSPORT_OPTIONS, mockFetch);
expect(headers.get).toHaveBeenCalledTimes(0);
await transport.send(ERROR_ENVELOPE);
expect(headers.get).toHaveBeenCalledTimes(2);
expect(headers.get).toHaveBeenCalledWith('X-Sentry-Rate-Limits');
expect(headers.get).toHaveBeenCalledWith('Retry-After');
});
it('allows for custom options to be passed in', async () => {
const mockFetch = jest.fn(() =>
Promise.resolve({
headers: new Headers(),
status: 200,
text: () => Promise.resolve({}),
}),
) as unknown as FetchImpl;
const REQUEST_OPTIONS: RequestInit = {
referrerPolicy: 'strict-origin',
keepalive: false,
referrer: 'http://example.org',
};
const transport = makeFetchTransport(
{ ...DEFAULT_FETCH_TRANSPORT_OPTIONS, fetchOptions: REQUEST_OPTIONS },
mockFetch,
);
await transport.send(ERROR_ENVELOPE);
expect(mockFetch).toHaveBeenLastCalledWith(DEFAULT_FETCH_TRANSPORT_OPTIONS.url, {
body: serializeEnvelope(ERROR_ENVELOPE, new TextEncoder()),
method: 'POST',
...REQUEST_OPTIONS,
});
});
});