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
|
- import type { Response } from 'node-fetch';
- import fetch from 'node-fetch';
- import { fetchWithRetries } from '../src/fetch';
- jest.mock('node-fetch');
- const mockedFetch = jest.mocked(fetch);
- const mockedFetchResponse = (
- ok: boolean,
- response: object,
- headers: Record<string, string> = {},
- ) => {
- return {
- ok,
- status: ok ? 200 : 429,
- statusText: ok ? 'OK' : 'Too Many Requests',
- data: response,
- headers: {
- get: (name: string) => {
- if (name === 'content-type') {
- return 'application/json';
- }
- if (headers[name] !== undefined) {
- return headers[name];
- }
- return null;
- },
- },
- } as unknown as Response;
- };
- const mockedSetTimeout = (reqTimeout: number) =>
- jest.spyOn(global, 'setTimeout').mockImplementation((cb: Function, ms?: number) => {
- if (ms !== reqTimeout) {
- cb();
- }
- return 0 as any;
- });
- describe('fetchWithRetries', () => {
- beforeEach(() => {
- jest.useFakeTimers();
- });
- afterEach(() => {
- mockedFetch.mockReset();
- jest.useRealTimers();
- });
- afterAll(() => {
- jest.clearAllMocks();
- });
- it('should fetch data', async () => {
- const url = 'https://api.example.com/data';
- const response = { data: 'test data' };
- mockedFetch.mockResolvedValueOnce(mockedFetchResponse(true, response));
- const result = await fetchWithRetries(url, {}, 1000);
- expect(mockedFetch).toHaveBeenCalledTimes(1);
- expect(result).toMatchObject({ data: response });
- });
- it('should retry after given time if rate limited, using X-Limit headers', async () => {
- const url = 'https://api.example.com/data';
- const response = { data: 'test data' };
- const rateLimitReset = 47_000;
- const timeout = 1234;
- const now = Date.now();
- const setTimoutMock = mockedSetTimeout(timeout);
- mockedFetch
- .mockResolvedValueOnce(
- mockedFetchResponse(false, response, {
- 'X-RateLimit-Remaining': '0',
- 'X-RateLimit-Reset': `${(now + rateLimitReset) / 1000}`,
- }),
- )
- .mockResolvedValueOnce(mockedFetchResponse(true, response));
- const result = await fetchWithRetries(url, {}, timeout);
- const waitTime = setTimoutMock.mock.calls[1][1];
- expect(mockedFetch).toHaveBeenCalledTimes(2);
- expect(waitTime).toBeGreaterThan(rateLimitReset);
- expect(waitTime).toBeLessThanOrEqual(rateLimitReset + 1000);
- expect(result).toMatchObject({ data: response });
- });
- it('should retry after given time if rate limited, using status and Retry-After', async () => {
- const url = 'https://api.example.com/data';
- const response = { data: 'test data' };
- const retryAfter = 15;
- const timeout = 1234;
- const setTimoutMock = mockedSetTimeout(timeout);
- mockedFetch
- .mockResolvedValueOnce(
- mockedFetchResponse(false, response, { 'Retry-After': String(retryAfter) }),
- )
- .mockResolvedValueOnce(mockedFetchResponse(true, response));
- const result = await fetchWithRetries(url, {}, timeout);
- const waitTime = setTimoutMock.mock.calls[1][1];
- expect(mockedFetch).toHaveBeenCalledTimes(2);
- expect(waitTime).toBe(retryAfter * 1000);
- expect(result).toMatchObject({ data: response });
- });
- it('should retry after default wait time if rate limited and wait time not found', async () => {
- const url = 'https://api.example.com/data';
- const response = { data: 'test data' };
- const timeout = 1234;
- const setTimoutMock = mockedSetTimeout(timeout);
- mockedFetch
- .mockResolvedValueOnce(mockedFetchResponse(false, response))
- .mockResolvedValueOnce(mockedFetchResponse(true, response));
- const result = await fetchWithRetries(url, {}, timeout);
- const waitTime = setTimoutMock.mock.calls[1][1];
- expect(mockedFetch).toHaveBeenCalledTimes(2);
- expect(waitTime).toBe(60_000);
- expect(result).toMatchObject({ data: response });
- });
- });
|