-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathratelimit.test.ts
240 lines (211 loc) · 8.7 KB
/
ratelimit.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
import { describe, expect, test } from 'vitest';
import type { RateLimits } from '../../src/utils-hoist/ratelimit';
import {
DEFAULT_RETRY_AFTER,
disabledUntil,
isRateLimited,
parseRetryAfterHeader,
updateRateLimits,
} from '../../src/utils-hoist/ratelimit';
describe('parseRetryAfterHeader()', () => {
test('should fallback to 60s when incorrect header provided', () => {
expect(parseRetryAfterHeader('x')).toEqual(DEFAULT_RETRY_AFTER);
});
test('should correctly parse delay-based header', () => {
expect(parseRetryAfterHeader('1337')).toEqual(1337 * 1000);
});
test('should correctly parse date-based header', () => {
expect(
parseRetryAfterHeader('Wed, 21 Oct 2015 07:28:13 GMT', new Date('Wed, 21 Oct 2015 07:28:00 GMT').getTime()),
).toEqual(13 * 1000);
});
});
describe('disabledUntil()', () => {
test('should return 0 when no match', () => {
expect(disabledUntil({}, 'error')).toEqual(0);
});
test('should return matched value', () => {
expect(disabledUntil({ error: 42 }, 'error')).toEqual(42);
});
test('should fallback to `all` category', () => {
expect(disabledUntil({ all: 42 }, 'error')).toEqual(42);
});
});
describe('isRateLimited()', () => {
test('should return false when no match', () => {
expect(isRateLimited({}, 'error')).toEqual(false);
});
test('should return false when matched value is in the past', () => {
expect(isRateLimited({ error: 10 }, 'error', 42)).toEqual(false);
});
test('should return true when matched value is in the future', () => {
expect(isRateLimited({ error: 50 }, 'error', 42)).toEqual(true);
});
test('should fallback to the `all` category when given one is not matched', () => {
expect(isRateLimited({ all: 10 }, 'error', 42)).toEqual(false);
expect(isRateLimited({ all: 50 }, 'error', 42)).toEqual(true);
});
});
describe('updateRateLimits()', () => {
test('should return same limits when no headers provided', () => {
const rateLimits: RateLimits = {
error: 42,
transaction: 1337,
};
const headers = {};
const updatedRateLimits = updateRateLimits(rateLimits, headers);
expect(updatedRateLimits).toEqual(rateLimits);
});
test('should update the `all` category based on `retry-after` header ', () => {
const rateLimits: RateLimits = {};
const headers = {
'x-sentry-rate-limits': null,
'retry-after': '42',
};
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0);
expect(updatedRateLimits.all).toEqual(42 * 1000);
});
test('should update a single category based on `x-sentry-rate-limits` header', () => {
const rateLimits: RateLimits = {};
const headers = {
'retry-after': null,
'x-sentry-rate-limits': '13:error',
};
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0);
expect(updatedRateLimits.error).toEqual(13 * 1000);
});
test('should update multiple categories based on `x-sentry-rate-limits` header', () => {
const rateLimits: RateLimits = {};
const headers = {
'retry-after': null,
'x-sentry-rate-limits': '13:error;transaction',
};
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0);
expect(updatedRateLimits.error).toEqual(13 * 1000);
expect(updatedRateLimits.transaction).toEqual(13 * 1000);
});
test('should update multiple categories with different values based on multi `x-sentry-rate-limits` header', () => {
const rateLimits: RateLimits = {};
const headers = {
'retry-after': null,
'x-sentry-rate-limits': '13:error,15:transaction',
};
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0);
expect(updatedRateLimits.error).toEqual(13 * 1000);
expect(updatedRateLimits.transaction).toEqual(15 * 1000);
});
test('should use last entry from multi `x-sentry-rate-limits` header for a given category', () => {
const rateLimits: RateLimits = {};
const headers = {
'retry-after': null,
'x-sentry-rate-limits': '13:error,15:transaction;error',
};
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0);
expect(updatedRateLimits.error).toEqual(15 * 1000);
expect(updatedRateLimits.transaction).toEqual(15 * 1000);
});
test('should fallback to `all` if `x-sentry-rate-limits` header is missing a category', () => {
const rateLimits: RateLimits = {};
const headers = {
'retry-after': null,
'x-sentry-rate-limits': '13',
};
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0);
expect(updatedRateLimits.all).toEqual(13 * 1000);
});
test('should use 60s default if delay in `x-sentry-rate-limits` header is malformed', () => {
const rateLimits: RateLimits = {};
const headers = {
'retry-after': null,
'x-sentry-rate-limits': 'x',
};
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0);
expect(updatedRateLimits.all).toEqual(60 * 1000);
});
test('should preserve limits for categories not in header', () => {
const rateLimits: RateLimits = {
error: 1337,
};
const headers = {
'retry-after': null,
'x-sentry-rate-limits': '13:transaction',
};
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0);
expect(updatedRateLimits.error).toEqual(1337);
expect(updatedRateLimits.transaction).toEqual(13 * 1000);
});
test('should give priority to `x-sentry-rate-limits` over `retry-after` header if both provided', () => {
const rateLimits: RateLimits = {};
const headers = {
'retry-after': '42',
'x-sentry-rate-limits': '13:error',
};
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0);
expect(updatedRateLimits.error).toEqual(13 * 1000);
expect(updatedRateLimits.all).toBeUndefined();
});
test('should apply a global rate limit of 60s when no headers are provided on a 429 status code', () => {
const rateLimits: RateLimits = {};
const updatedRateLimits = updateRateLimits(rateLimits, { statusCode: 429 }, 0);
expect(updatedRateLimits.all).toBe(60_000);
});
test('should not apply a global rate limit specific headers are provided on a 429 status code', () => {
const rateLimits: RateLimits = {};
const headers = {
'retry-after': null,
'x-sentry-rate-limits': '13:error',
};
const updatedRateLimits = updateRateLimits(rateLimits, { statusCode: 429, headers }, 0);
expect(updatedRateLimits.error).toEqual(13 * 1000);
expect(updatedRateLimits.all).toBeUndefined();
});
test('should not apply a default rate limit on a non-429 status code', () => {
const rateLimits: RateLimits = {};
const updatedRateLimits = updateRateLimits(rateLimits, { statusCode: 200 }, 0);
expect(updatedRateLimits).toEqual(rateLimits);
});
test('should apply a global rate limit with a scope on a 429 status code', () => {
const rateLimits: RateLimits = {};
const headers = {
'retry-after': null,
'x-sentry-rate-limits': '60::organization',
};
const updatedRateLimits = updateRateLimits(rateLimits, { statusCode: 429, headers }, 0);
expect(updatedRateLimits.all).toEqual(60_000);
});
});
describe('data category "metric_bucket"', () => {
test('should add limit for `metric_bucket` category when namespaces contain "custom"', () => {
const rateLimits: RateLimits = {};
const headers = {
'retry-after': null,
'x-sentry-rate-limits': '42:metric_bucket:::custom',
};
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0);
expect(updatedRateLimits.metric_bucket).toEqual(42 * 1000);
});
test('should not add limit for `metric_bucket` category when namespaces do not contain "custom"', () => {
const rateLimits: RateLimits = {};
const headers = {
'retry-after': null,
'x-sentry-rate-limits': '42:metric_bucket:::namespace1;namespace2',
};
const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0);
expect(updatedRateLimits.metric_bucket).toBeUndefined();
});
test('should add limit for `metric_bucket` category when namespaces are empty', () => {
const rateLimits: RateLimits = {};
const headers1 = {
'retry-after': null,
'x-sentry-rate-limits': '42:metric_bucket', // without semicolon at the end
};
const updatedRateLimits1 = updateRateLimits(rateLimits, { headers: headers1 }, 0);
expect(updatedRateLimits1.metric_bucket).toEqual(42 * 1000);
const headers2 = {
'retry-after': null,
'x-sentry-rate-limits': '42:metric_bucket:organization:quota_exceeded:', // with semicolon at the end
};
const updatedRateLimits2 = updateRateLimits(rateLimits, { headers: headers2 }, 0);
expect(updatedRateLimits2.metric_bucket).toEqual(42 * 1000);
});
});