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
|
- import { fetchWithRetries } from '../src/fetch';
- const mockedFetch = jest.spyOn(global, 'fetch');
- const mockedFetchResponse = (ok: boolean, response: object, headers: object = {}) => {
- const responseText = JSON.stringify(response);
- return {
- ok,
- status: ok ? 200 : 429,
- statusText: ok ? 'OK' : 'Too Many Requests',
- text: () => Promise.resolve(responseText),
- json: () => Promise.resolve(response),
- headers: new Headers({
- 'content-type': 'application/json',
- ...headers,
- }),
- } as Response;
- };
- const mockedSetTimeout = (reqTimeout: number) =>
- jest.spyOn(global, 'setTimeout').mockImplementation((cb: () => void, 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);
- await expect(result.json()).resolves.toEqual(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 setTimeoutMock = 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 = setTimeoutMock.mock.calls[1][1];
- expect(mockedFetch).toHaveBeenCalledTimes(2);
- expect(waitTime).toBeGreaterThan(rateLimitReset);
- expect(waitTime).toBeLessThanOrEqual(rateLimitReset + 1000);
- await expect(result.json()).resolves.toEqual(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 setTimeoutMock = mockedSetTimeout(timeout);
- mockedFetch
- .mockResolvedValueOnce(
- mockedFetchResponse(false, response, { 'Retry-After': String(retryAfter) }),
- )
- .mockResolvedValueOnce(mockedFetchResponse(true, response));
- const result = await fetchWithRetries(url, {}, timeout);
- const waitTime = setTimeoutMock.mock.calls[1][1];
- expect(mockedFetch).toHaveBeenCalledTimes(2);
- expect(waitTime).toBe(retryAfter * 1000);
- await expect(result.json()).resolves.toEqual(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 setTimeoutMock = mockedSetTimeout(timeout);
- mockedFetch
- .mockResolvedValueOnce(mockedFetchResponse(false, response))
- .mockResolvedValueOnce(mockedFetchResponse(true, response));
- const result = await fetchWithRetries(url, {}, timeout);
- const waitTime = setTimeoutMock.mock.calls[1][1];
- expect(mockedFetch).toHaveBeenCalledTimes(2);
- expect(waitTime).toBe(60_000);
- await expect(result.json()).resolves.toEqual(response);
- });
- });
|