@@ -70,66 +70,73 @@ describe('updateRateLimits()', () => {
70
70
test ( 'should update the `all` category based on `retry-after` header ' , ( ) => {
71
71
const rateLimits : RateLimits = { } ;
72
72
const headers = {
73
+ 'x-sentry-rate-limits' : null ,
73
74
'retry-after' : '42' ,
74
75
} ;
75
- const updatedRateLimits = updateRateLimits ( rateLimits , headers , 0 ) ;
76
+ const updatedRateLimits = updateRateLimits ( rateLimits , { headers } , 0 ) ;
76
77
expect ( updatedRateLimits . all ) . toEqual ( 42 * 1000 ) ;
77
78
} ) ;
78
79
79
80
test ( 'should update a single category based on `x-sentry-rate-limits` header' , ( ) => {
80
81
const rateLimits : RateLimits = { } ;
81
82
const headers = {
83
+ 'retry-after' : null ,
82
84
'x-sentry-rate-limits' : '13:error' ,
83
85
} ;
84
- const updatedRateLimits = updateRateLimits ( rateLimits , headers , 0 ) ;
86
+ const updatedRateLimits = updateRateLimits ( rateLimits , { headers } , 0 ) ;
85
87
expect ( updatedRateLimits . error ) . toEqual ( 13 * 1000 ) ;
86
88
} ) ;
87
89
88
90
test ( 'should update multiple categories based on `x-sentry-rate-limits` header' , ( ) => {
89
91
const rateLimits : RateLimits = { } ;
90
92
const headers = {
93
+ 'retry-after' : null ,
91
94
'x-sentry-rate-limits' : '13:error;transaction' ,
92
95
} ;
93
- const updatedRateLimits = updateRateLimits ( rateLimits , headers , 0 ) ;
96
+ const updatedRateLimits = updateRateLimits ( rateLimits , { headers } , 0 ) ;
94
97
expect ( updatedRateLimits . error ) . toEqual ( 13 * 1000 ) ;
95
98
expect ( updatedRateLimits . transaction ) . toEqual ( 13 * 1000 ) ;
96
99
} ) ;
97
100
98
101
test ( 'should update multiple categories with different values based on multi `x-sentry-rate-limits` header' , ( ) => {
99
102
const rateLimits : RateLimits = { } ;
100
103
const headers = {
104
+ 'retry-after' : null ,
101
105
'x-sentry-rate-limits' : '13:error,15:transaction' ,
102
106
} ;
103
- const updatedRateLimits = updateRateLimits ( rateLimits , headers , 0 ) ;
107
+ const updatedRateLimits = updateRateLimits ( rateLimits , { headers } , 0 ) ;
104
108
expect ( updatedRateLimits . error ) . toEqual ( 13 * 1000 ) ;
105
109
expect ( updatedRateLimits . transaction ) . toEqual ( 15 * 1000 ) ;
106
110
} ) ;
107
111
108
112
test ( 'should use last entry from multi `x-sentry-rate-limits` header for a given category' , ( ) => {
109
113
const rateLimits : RateLimits = { } ;
110
114
const headers = {
115
+ 'retry-after' : null ,
111
116
'x-sentry-rate-limits' : '13:error,15:transaction;error' ,
112
117
} ;
113
- const updatedRateLimits = updateRateLimits ( rateLimits , headers , 0 ) ;
118
+ const updatedRateLimits = updateRateLimits ( rateLimits , { headers } , 0 ) ;
114
119
expect ( updatedRateLimits . error ) . toEqual ( 15 * 1000 ) ;
115
120
expect ( updatedRateLimits . transaction ) . toEqual ( 15 * 1000 ) ;
116
121
} ) ;
117
122
118
123
test ( 'should fallback to `all` if `x-sentry-rate-limits` header is missing a category' , ( ) => {
119
124
const rateLimits : RateLimits = { } ;
120
125
const headers = {
126
+ 'retry-after' : null ,
121
127
'x-sentry-rate-limits' : '13' ,
122
128
} ;
123
- const updatedRateLimits = updateRateLimits ( rateLimits , headers , 0 ) ;
129
+ const updatedRateLimits = updateRateLimits ( rateLimits , { headers } , 0 ) ;
124
130
expect ( updatedRateLimits . all ) . toEqual ( 13 * 1000 ) ;
125
131
} ) ;
126
132
127
133
test ( 'should use 60s default if delay in `x-sentry-rate-limits` header is malformed' , ( ) => {
128
134
const rateLimits : RateLimits = { } ;
129
135
const headers = {
136
+ 'retry-after' : null ,
130
137
'x-sentry-rate-limits' : 'x' ,
131
138
} ;
132
- const updatedRateLimits = updateRateLimits ( rateLimits , headers , 0 ) ;
139
+ const updatedRateLimits = updateRateLimits ( rateLimits , { headers } , 0 ) ;
133
140
expect ( updatedRateLimits . all ) . toEqual ( 60 * 1000 ) ;
134
141
} ) ;
135
142
@@ -138,9 +145,10 @@ describe('updateRateLimits()', () => {
138
145
error : 1337 ,
139
146
} ;
140
147
const headers = {
148
+ 'retry-after' : null ,
141
149
'x-sentry-rate-limits' : '13:transaction' ,
142
150
} ;
143
- const updatedRateLimits = updateRateLimits ( rateLimits , headers , 0 ) ;
151
+ const updatedRateLimits = updateRateLimits ( rateLimits , { headers } , 0 ) ;
144
152
expect ( updatedRateLimits . error ) . toEqual ( 1337 ) ;
145
153
expect ( updatedRateLimits . transaction ) . toEqual ( 13 * 1000 ) ;
146
154
} ) ;
@@ -151,8 +159,31 @@ describe('updateRateLimits()', () => {
151
159
'retry-after' : '42' ,
152
160
'x-sentry-rate-limits' : '13:error' ,
153
161
} ;
154
- const updatedRateLimits = updateRateLimits ( rateLimits , headers , 0 ) ;
162
+ const updatedRateLimits = updateRateLimits ( rateLimits , { headers } , 0 ) ;
155
163
expect ( updatedRateLimits . error ) . toEqual ( 13 * 1000 ) ;
156
164
expect ( updatedRateLimits . all ) . toBeUndefined ( ) ;
157
165
} ) ;
166
+
167
+ test ( 'should apply a global rate limit of 60s when no headers are provided on a 429 status code' , ( ) => {
168
+ const rateLimits : RateLimits = { } ;
169
+ const updatedRateLimits = updateRateLimits ( rateLimits , { statusCode : 429 } , 0 ) ;
170
+ expect ( updatedRateLimits . all ) . toBe ( 60_000 ) ;
171
+ } ) ;
172
+
173
+ test ( 'should not apply a global rate limit specific headers are provided on a 429 status code' , ( ) => {
174
+ const rateLimits : RateLimits = { } ;
175
+ const headers = {
176
+ 'retry-after' : null ,
177
+ 'x-sentry-rate-limits' : '13:error' ,
178
+ } ;
179
+ const updatedRateLimits = updateRateLimits ( rateLimits , { statusCode : 429 , headers } , 0 ) ;
180
+ expect ( updatedRateLimits . error ) . toEqual ( 13 * 1000 ) ;
181
+ expect ( updatedRateLimits . all ) . toBeUndefined ( ) ;
182
+ } ) ;
183
+
184
+ test ( 'should not apply a default rate limit on a non-429 status code' , ( ) => {
185
+ const rateLimits : RateLimits = { } ;
186
+ const updatedRateLimits = updateRateLimits ( rateLimits , { statusCode : 200 } , 0 ) ;
187
+ expect ( updatedRateLimits ) . toEqual ( rateLimits ) ;
188
+ } ) ;
158
189
} ) ;
0 commit comments