-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
http.test.ts
168 lines (147 loc) · 4.88 KB
/
http.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
import { TransportOptions } from '@sentry/types';
import { SentryError } from '@sentry/utils';
import * as HttpsProxyAgent from 'https-proxy-agent';
import { HTTPTransport } from '../../src/transports/http';
const mockSetEncoding = jest.fn();
const dsn = 'http://9e9fd4523d784609a5fc0ebb1080592f@sentry.io:8989/mysubpath/50622';
const transportPath = '/mysubpath/api/50622/store/';
let mockReturnCode = 200;
let mockHeaders = {};
function createTransport(options: TransportOptions): HTTPTransport {
const transport = new HTTPTransport(options);
transport.module = {
request: jest.fn().mockImplementation((_options: any, callback: any) => ({
end: () => {
callback({
headers: mockHeaders,
setEncoding: mockSetEncoding,
statusCode: mockReturnCode,
});
},
on: jest.fn(),
})),
};
return transport;
}
function assertBasicOptions(options: any): void {
expect(options.headers['X-Sentry-Auth']).toContain('sentry_version');
expect(options.headers['X-Sentry-Auth']).toContain('sentry_client');
expect(options.headers['X-Sentry-Auth']).toContain('sentry_key');
expect(options.port).toEqual('8989');
expect(options.path).toEqual(transportPath);
expect(options.hostname).toEqual('sentry.io');
}
describe('HTTPTransport', () => {
beforeEach(() => {
mockReturnCode = 200;
mockHeaders = {};
jest.clearAllMocks();
});
test('send 200', async () => {
const transport = createTransport({ dsn });
await transport.sendEvent({
message: 'test',
});
const requestOptions = (transport.module!.request as jest.Mock).mock.calls[0][0];
assertBasicOptions(requestOptions);
expect(mockSetEncoding).toHaveBeenCalled();
});
test('send 400', async () => {
mockReturnCode = 400;
const transport = createTransport({ dsn });
try {
await transport.sendEvent({
message: 'test',
});
} catch (e) {
const requestOptions = (transport.module!.request as jest.Mock).mock.calls[0][0];
assertBasicOptions(requestOptions);
expect(e).toEqual(new SentryError(`HTTP Error (${mockReturnCode})`));
}
});
test('send x-sentry-error header', async () => {
mockReturnCode = 429;
mockHeaders = {
'x-sentry-error': 'test-failed',
};
const transport = createTransport({ dsn });
try {
await transport.sendEvent({
message: 'test',
});
} catch (e) {
const requestOptions = (transport.module!.request as jest.Mock).mock.calls[0][0];
assertBasicOptions(requestOptions);
expect(e).toEqual(new SentryError(`HTTP Error (${mockReturnCode}): test-failed`));
}
});
test('back-off using Retry-After header', async () => {
const retryAfterSeconds = 10;
mockReturnCode = 429;
mockHeaders = {
'Retry-After': retryAfterSeconds,
};
const transport = createTransport({ dsn });
const now = Date.now();
const mock = jest
.spyOn(Date, 'now')
// Check for first event
.mockReturnValueOnce(now)
// Setting disabledUntil
.mockReturnValueOnce(now)
// Check for second event
.mockReturnValueOnce(now + (retryAfterSeconds / 2) * 1000)
// Check for third event
.mockReturnValueOnce(now + retryAfterSeconds * 1000);
try {
await transport.sendEvent({ message: 'test' });
} catch (e) {
expect(e).toEqual(new SentryError(`HTTP Error (${mockReturnCode})`));
}
try {
await transport.sendEvent({ message: 'test' });
} catch (e) {
expect(e).toEqual(
new SentryError(`Transport locked till ${new Date(now + retryAfterSeconds * 1000)} due to too many requests.`),
);
}
try {
await transport.sendEvent({ message: 'test' });
} catch (e) {
expect(e).toEqual(new SentryError(`HTTP Error (${mockReturnCode})`));
}
mock.mockRestore();
});
test('transport options', async () => {
mockReturnCode = 200;
const transport = createTransport({
dsn,
headers: {
a: 'b',
},
});
await transport.sendEvent({
message: 'test',
});
const requestOptions = (transport.module!.request as jest.Mock).mock.calls[0][0];
assertBasicOptions(requestOptions);
expect(requestOptions.headers).toEqual(expect.objectContaining({ a: 'b' }));
});
test('http proxy', async () => {
mockReturnCode = 200;
const transport = createTransport({
dsn,
httpProxy: 'http://example.com:8080',
});
await transport.sendEvent({
message: 'test',
});
const requestOptions = (transport.module!.request as jest.Mock).mock.calls[0][0];
assertBasicOptions(requestOptions);
expect(requestOptions.agent).toBeInstanceOf(HttpsProxyAgent);
expect(requestOptions.agent.secureProxy).toEqual(false);
expect(requestOptions.agent.proxy).toEqual(
expect.objectContaining({ protocol: 'http:', port: 8080, host: 'example.com' }),
);
});
});