|
| 1 | +import { |
| 2 | + DEFAULT_RETRY_AFTER, |
| 3 | + disabledUntil, |
| 4 | + isRateLimited, |
| 5 | + parseRetryAfterHeader, |
| 6 | + RateLimits, |
| 7 | + updateRateLimits, |
| 8 | +} from '../src/ratelimit'; |
| 9 | + |
| 10 | +describe('parseRetryAfterHeader()', () => { |
| 11 | + test('should fallback to 60s when incorrect header provided', () => { |
| 12 | + expect(parseRetryAfterHeader('x')).toEqual(DEFAULT_RETRY_AFTER); |
| 13 | + }); |
| 14 | + |
| 15 | + test('should correctly parse delay-based header', () => { |
| 16 | + expect(parseRetryAfterHeader('1337')).toEqual(1337 * 1000); |
| 17 | + }); |
| 18 | + |
| 19 | + test('should correctly parse date-based header', () => { |
| 20 | + expect( |
| 21 | + parseRetryAfterHeader('Wed, 21 Oct 2015 07:28:13 GMT', new Date('Wed, 21 Oct 2015 07:28:00 GMT').getTime()), |
| 22 | + ).toEqual(13 * 1000); |
| 23 | + }); |
| 24 | +}); |
| 25 | + |
| 26 | +describe('disabledUntil()', () => { |
| 27 | + test('should return 0 when no match', () => { |
| 28 | + expect(disabledUntil({}, 'error')).toEqual(0); |
| 29 | + }); |
| 30 | + |
| 31 | + test('should return matched value', () => { |
| 32 | + expect(disabledUntil({ error: 42 }, 'error')).toEqual(42); |
| 33 | + }); |
| 34 | + |
| 35 | + test('should fallback to `all` category', () => { |
| 36 | + expect(disabledUntil({ all: 42 }, 'error')).toEqual(42); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +describe('isRateLimited()', () => { |
| 41 | + test('should return false when no match', () => { |
| 42 | + expect(isRateLimited({}, 'error')).toEqual(false); |
| 43 | + }); |
| 44 | + |
| 45 | + test('should return false when matched value is in the past', () => { |
| 46 | + expect(isRateLimited({ error: 10 }, 'error', 42)).toEqual(false); |
| 47 | + }); |
| 48 | + |
| 49 | + test('should return true when matched value is in the future', () => { |
| 50 | + expect(isRateLimited({ error: 50 }, 'error', 42)).toEqual(true); |
| 51 | + }); |
| 52 | + |
| 53 | + test('should fallback to the `all` category when given one is not matched', () => { |
| 54 | + expect(isRateLimited({ all: 10 }, 'error', 42)).toEqual(false); |
| 55 | + expect(isRateLimited({ all: 50 }, 'error', 42)).toEqual(true); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe('updateRateLimits()', () => { |
| 60 | + test('should return same limits when no headers provided', () => { |
| 61 | + const rateLimits: RateLimits = { |
| 62 | + error: 42, |
| 63 | + transaction: 1337, |
| 64 | + }; |
| 65 | + const headers = {}; |
| 66 | + const updatedRateLimits = updateRateLimits(rateLimits, headers); |
| 67 | + expect(updatedRateLimits).toEqual(rateLimits); |
| 68 | + }); |
| 69 | + |
| 70 | + test('should update the `all` category based on `retry-after` header ', () => { |
| 71 | + const rateLimits: RateLimits = {}; |
| 72 | + const headers = { |
| 73 | + 'retry-after': '42', |
| 74 | + }; |
| 75 | + const updatedRateLimits = updateRateLimits(rateLimits, headers, 0); |
| 76 | + expect(updatedRateLimits.all).toEqual(42 * 1000); |
| 77 | + }); |
| 78 | + |
| 79 | + test('should update a single category based on `x-sentry-rate-limits` header', () => { |
| 80 | + const rateLimits: RateLimits = {}; |
| 81 | + const headers = { |
| 82 | + 'x-sentry-rate-limits': '13:error', |
| 83 | + }; |
| 84 | + const updatedRateLimits = updateRateLimits(rateLimits, headers, 0); |
| 85 | + expect(updatedRateLimits.error).toEqual(13 * 1000); |
| 86 | + }); |
| 87 | + |
| 88 | + test('should update multiple categories based on `x-sentry-rate-limits` header', () => { |
| 89 | + const rateLimits: RateLimits = {}; |
| 90 | + const headers = { |
| 91 | + 'x-sentry-rate-limits': '13:error;transaction', |
| 92 | + }; |
| 93 | + const updatedRateLimits = updateRateLimits(rateLimits, headers, 0); |
| 94 | + expect(updatedRateLimits.error).toEqual(13 * 1000); |
| 95 | + expect(updatedRateLimits.transaction).toEqual(13 * 1000); |
| 96 | + }); |
| 97 | + |
| 98 | + test('should update multiple categories with different values based on multi `x-sentry-rate-limits` header', () => { |
| 99 | + const rateLimits: RateLimits = {}; |
| 100 | + const headers = { |
| 101 | + 'x-sentry-rate-limits': '13:error,15:transaction', |
| 102 | + }; |
| 103 | + const updatedRateLimits = updateRateLimits(rateLimits, headers, 0); |
| 104 | + expect(updatedRateLimits.error).toEqual(13 * 1000); |
| 105 | + expect(updatedRateLimits.transaction).toEqual(15 * 1000); |
| 106 | + }); |
| 107 | + |
| 108 | + test('should use last entry from multi `x-sentry-rate-limits` header for a given category', () => { |
| 109 | + const rateLimits: RateLimits = {}; |
| 110 | + const headers = { |
| 111 | + 'x-sentry-rate-limits': '13:error,15:transaction;error', |
| 112 | + }; |
| 113 | + const updatedRateLimits = updateRateLimits(rateLimits, headers, 0); |
| 114 | + expect(updatedRateLimits.error).toEqual(15 * 1000); |
| 115 | + expect(updatedRateLimits.transaction).toEqual(15 * 1000); |
| 116 | + }); |
| 117 | + |
| 118 | + test('should fallback to `all` if `x-sentry-rate-limits` header is missing a category', () => { |
| 119 | + const rateLimits: RateLimits = {}; |
| 120 | + const headers = { |
| 121 | + 'x-sentry-rate-limits': '13', |
| 122 | + }; |
| 123 | + const updatedRateLimits = updateRateLimits(rateLimits, headers, 0); |
| 124 | + expect(updatedRateLimits.all).toEqual(13 * 1000); |
| 125 | + }); |
| 126 | + |
| 127 | + test('should use 60s default if delay in `x-sentry-rate-limits` header is malformed', () => { |
| 128 | + const rateLimits: RateLimits = {}; |
| 129 | + const headers = { |
| 130 | + 'x-sentry-rate-limits': 'x', |
| 131 | + }; |
| 132 | + const updatedRateLimits = updateRateLimits(rateLimits, headers, 0); |
| 133 | + expect(updatedRateLimits.all).toEqual(60 * 1000); |
| 134 | + }); |
| 135 | + |
| 136 | + test('should preserve limits for categories not in header', () => { |
| 137 | + const rateLimits: RateLimits = { |
| 138 | + error: 1337, |
| 139 | + }; |
| 140 | + const headers = { |
| 141 | + 'x-sentry-rate-limits': '13:transaction', |
| 142 | + }; |
| 143 | + const updatedRateLimits = updateRateLimits(rateLimits, headers, 0); |
| 144 | + expect(updatedRateLimits.error).toEqual(1337); |
| 145 | + expect(updatedRateLimits.transaction).toEqual(13 * 1000); |
| 146 | + }); |
| 147 | + |
| 148 | + test('should give priority to `x-sentry-rate-limits` over `retry-after` header if both provided', () => { |
| 149 | + const rateLimits: RateLimits = {}; |
| 150 | + const headers = { |
| 151 | + 'retry-after': '42', |
| 152 | + 'x-sentry-rate-limits': '13:error', |
| 153 | + }; |
| 154 | + const updatedRateLimits = updateRateLimits(rateLimits, headers, 0); |
| 155 | + expect(updatedRateLimits.error).toEqual(13 * 1000); |
| 156 | + expect(updatedRateLimits.all).toBeUndefined(); |
| 157 | + }); |
| 158 | +}); |
0 commit comments