Skip to content
Myr

retry-timing.ts

This file

Path

test/retry-timing.ts

162 lines · 7.6 KiB · TypeScript

Last changed by

RefactorSindre Sorhusbefore adoptiona5ed57b

History of this file

Take it

More testsSindre Sorhusbefore adoptioncd1dba0

lines 1–73why, one step back
1import test from 'ava';
2import {calculateRetryTimingDelay, getRetryTimingHeader} from '../source/core/retry-timing.js';34// RFC 9110 §10.2.3 defines delay-seconds as 1*DIGIT, not JavaScript number syntax.5// https://www.rfc-editor.org/rfc/rfc9110.html#section-10.2.36for (const value of ['-1', '+1', '1.5', '1e3', '1_000', '0b10', '0o10', '10seconds', '1 0', '12']) {7 test(`Retry-After rejects non-decimal delay ${JSON.stringify(value)}`, t => {8 t.is(calculateRetryTimingDelay({value, allowTimestamp: false}), undefined);9 });10}1112test('Retry-After accepts leading zeroes without interpreting them as octal', t => {13 t.is(calculateRetryTimingDelay({value: '000012', allowTimestamp: false}), 12_000);14});1516test('Retry-After preserves integer seconds beyond the signed 32-bit range', t => {17 t.is(calculateRetryTimingDelay({value: '2147483648', allowTimestamp: false}), 2_147_483_648_000);18});1920// Exercise invalid components independently so another invalid component cannot mask a missing check.21// HTTP-date syntax is case-sensitive: RFC 9110 §5.6.7.22for (const [description, value] of [23 ['zero day', 'Wed, 00 Jan 2031 12:00:00 GMT'],24 ['day overflow', 'Fri, 32 Jan 2031 12:00:00 GMT'],25 ['short month overflow', 'Thu, 31 Apr 2031 12:00:00 GMT'],26 ['non-leap February', 'Sat, 29 Feb 2031 12:00:00 GMT'],27 ['unknown month', 'Wed, 01 Foo 2031 12:00:00 GMT'],28 ['unknown weekday', 'Foo, 01 Jan 2031 12:00:00 GMT'],29 ['hour overflow', 'Wed, 01 Jan 2031 24:00:00 GMT'],30 ['minute overflow', 'Wed, 01 Jan 2031 12:60:00 GMT'],31 ['second overflow', 'Wed, 01 Jan 2031 12:00:61 GMT'],32 ['non-GMT zone', 'Wed, 01 Jan 2031 12:00:00 UTC'],33 ['numeric zone', 'Wed, 01 Jan 2031 12:00:00 +0000'],34 ['ISO timestamp', '2031-01-01T12:00:00Z'],35 ['lowercase date', 'wed, 01 jan 2031 12:00:00 gmt'],36 ['unpadded IMF day', 'Wed, 1 Jan 2031 12:00:00 GMT'],37]) {38 test(`Retry-After rejects HTTP dates with ${description}`, t => {39 t.is(calculateRetryTimingDelay({value: value!, allowTimestamp: false}), undefined);40 });41}4243for (const [description, value, now, expected] of [44 ['leap day', 'Thu, 29 Feb 2024 00:00:00 GMT', '2024-02-28T23:59:59Z', 1000],45 ['space-padded asctime day', 'Sun Nov 6 08:49:37 1994', '1994-11-06T08:49:36Z', 1000],46 ['past date', 'Wed, 01 Jan 2031 12:00:00 GMT', '2031-01-01T12:00:01Z', 0],47 ['current date', 'Wed, 01 Jan 2031 12:00:00 GMT', '2031-01-01T12:00:00Z', 0],48] as const) {49 test.serial(`Retry-After calculates the delay for a ${description}`, t => {50 const originalNow = Date.now;51 t.teardown(() => {52 Date.now = originalNow;53 });54 Date.now = () => Date.parse(now);5556 t.is(calculateRetryTimingDelay({value, allowTimestamp: false}), expected);57 });58}5960test('Retry-After header names and outer whitespace are normalized by Headers', t => {61 const header = getRetryTimingHeader(new Headers({'rEtRy-AfTeR': '\t 012 \t'}));62 t.deepEqual(header, {value: '012', allowTimestamp: false});63 t.is(calculateRetryTimingDelay(header!), 12_000);64});6566test('multiple Retry-After values do not become a valid delay or date', t => {67 const headers = new Headers();68 headers.append('Retry-After', '1');69 headers.append('Retry-After', '2');70 const header = getRetryTimingHeader(headers);71 t.deepEqual(header, {value: '1, 2', allowTimestamp: false});72 t.is(calculateRetryTimingDelay(header!), undefined);73});

Skip wrapping a body a hook read, and read a past reset epoch as a timestampSindre Sorhusbefore adoption23ca564

lines 74–90why, one step back
74
75// The "current era" cutoff used to be a hardcoded 2024-01-01, so a reset value from just before it fell below the cutoff and was read as delay seconds. A `RateLimit-Reset` of 1700000000 (November 2023) became a 54-year delay.76for (const [description, value, expected] of [77 ['a November 2023 epoch as a reset in the past', '1700000000', 0],78 ['a January 2023 epoch as a reset in the past', '1672531200', 0],79 ['a 2001 epoch as a reset in the past', '1000000000', 0],80 ['a value just below the threshold as a delay', '999999999', 999_999_999_000],81 ['a small reset as a delay', '30', 30_000],82] as Array<[string, string, number]>) {83 test(`RateLimit-Reset reads ${description}`, t => {84 t.is(calculateRetryTimingDelay({value, allowTimestamp: true}), expected);85 });86}8788test('Retry-After numbers are never treated as timestamps', t => {89 t.is(calculateRetryTimingDelay({value: '1700000000', allowTimestamp: false}), 1_700_000_000_000);90});

RefactorSindre Sorhusbefore adoptiona5ed57b

lines 91–162why, one step back
91
92for (const [description, headers, expected] of [93 ['RateLimit-Reset over X-RateLimit-Retry-After', {'RateLimit-Reset': '1', 'X-RateLimit-Retry-After': '2'}, {value: '1', allowTimestamp: true}],94 ['X-RateLimit-Retry-After over X-RateLimit-Reset', {'X-RateLimit-Retry-After': '2', 'X-RateLimit-Reset': '3'}, {value: '2', allowTimestamp: false}],95 ['X-RateLimit-Reset over X-Rate-Limit-Reset', {'X-RateLimit-Reset': '3', 'X-Rate-Limit-Reset': '4'}, {value: '3', allowTimestamp: true}],96] as const) {97 test(`retry timing header precedence prefers ${description}`, t => {98 t.deepEqual(getRetryTimingHeader(new Headers(headers)), expected);99 });100}101102test('an empty X-RateLimit-Retry-After does not fall back to a reset header', t => {103 t.deepEqual(getRetryTimingHeader(new Headers({'X-RateLimit-Retry-After': '', 'X-RateLimit-Reset': '3'})), {value: '', allowTimestamp: false});104});105106test('HTTP dates with a year below 100 are not mapped to the 1900s', t => {107 // `Date.UTC()` maps years 0-99 to 1900-1999. Such a date must still be a valid past date, not a malformed one that falls back to the normal retry delay.108 t.is(calculateRetryTimingDelay({value: 'Fri, 01 Jan 0010 00:00:00 GMT', allowTimestamp: false}), 0);109 t.is(calculateRetryTimingDelay({value: 'Fri Jan 1 00:00:00 0010', allowTimestamp: false}), 0);110});111112test('RFC 850 date between 50 and 51 years in the future is interpreted as past', t => {113 // The RFC 850 century window uses the real clock, so build the date from it.114 const date = new Date();115 date.setUTCFullYear(date.getUTCFullYear() + 50, date.getUTCMonth() + 6);116 const weekday = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getUTCDay()];117 const month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][date.getUTCMonth()];118 const [day, year, time] = [119 String(date.getUTCDate()).padStart(2, '0'),120 String(date.getUTCFullYear() % 100).padStart(2, '0'),121 date.toISOString().slice(11, 19),122 ];123124 t.is(calculateRetryTimingDelay({value: `${weekday}, ${day}-${month}-${year} ${time} GMT`, allowTimestamp: false}), 0);125});126127test.serial('asctime date with a two-digit day is parsed', t => {128 const originalNow = Date.now;129 t.teardown(() => {130 Date.now = originalNow;131 });132 Date.now = () => Date.parse('2031-01-16T12:00:00Z');133134 t.is(calculateRetryTimingDelay({value: 'Thu Jan 16 12:00:02 2031', allowTimestamp: false}), 2000);135});136137test('Retry-After HTTP date in year 9999 gives a finite delay', t => {138 for (const value of ['Fri, 31 Dec 9999 23:59:59 GMT', 'Fri Dec 31 23:59:59 9999']) {139 const delay = calculateRetryTimingDelay({value, allowTimestamp: false});140 t.true(Number.isFinite(delay) && delay! > 0, `value: ${value}`);141 }142});143144for (const [description, value] of [145 ['IMF-fixdate', 'Wed, 31 Dec 2031 23:59:60 GMT'],146 ['asctime', 'Wed Dec 31 23:59:60 2031'],147] as const) {148 test.serial(`Retry-After ${description} leap second at the end of a year resolves to the start of the next year`, t => {149 const originalNow = Date.now;150 t.teardown(() => {151 Date.now = originalNow;152 });153 Date.now = () => Date.parse('2031-12-31T23:59:59Z');154155 t.is(calculateRetryTimingDelay({value, allowTimestamp: false}), 1000);156 });157}158159// `Number()` of a long digit string overflows to `Infinity` rather than `NaN`, so the delay is unbounded but still a usable number for Ky to cap.160test('Retry-After with more digits than a number can hold gives an infinite delay', t => {161 t.is(calculateRetryTimingDelay({value: '9'.repeat(400), allowTimestamp: false}), Number.POSITIVE_INFINITY);162});