forked from AubaidFarrukh/smart-retry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegrations.test.ts
More file actions
147 lines (112 loc) · 4.81 KB
/
Copy pathintegrations.test.ts
File metadata and controls
147 lines (112 loc) · 4.81 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
import { FetchRetry } from '../src/integrations';
import fs from 'fs';
import path from 'path';
const TEST_LOG_PATH = path.join(__dirname, 'fetch-retry-test.json');
describe('FetchRetry body serialization', () => {
let fetchRetry: FetchRetry;
let fetchMock: jest.Mock;
const originalFetch = global.fetch;
beforeEach(() => {
if (fs.existsSync(TEST_LOG_PATH)) {
fs.unlinkSync(TEST_LOG_PATH);
}
fetchRetry = new FetchRetry({ maxRetries: 1 }, TEST_LOG_PATH);
fetchMock = jest.fn().mockResolvedValue(new Response(null, { status: 200 }));
global.fetch = fetchMock as unknown as typeof fetch;
});
afterEach(() => {
global.fetch = originalFetch;
if (fs.existsSync(TEST_LOG_PATH)) {
fs.unlinkSync(TEST_LOG_PATH);
}
});
it('JSON-stringifies plain object bodies and sets Content-Type', async () => {
await fetchRetry.post('/data', { hello: 'world' });
const [, init] = fetchMock.mock.calls[0];
expect(init.body).toBe(JSON.stringify({ hello: 'world' }));
expect(init.headers).toMatchObject({ 'Content-Type': 'application/json' });
});
it('passes FormData through untouched without forcing Content-Type', async () => {
const form = new FormData();
form.append('file', 'contents');
await fetchRetry.post('/upload', form);
const [, init] = fetchMock.mock.calls[0];
expect(init.body).toBe(form);
expect(init.headers).toBeUndefined();
});
it('passes URLSearchParams through untouched', async () => {
const params = new URLSearchParams({ a: '1' });
await fetchRetry.post('/form', params);
const [, init] = fetchMock.mock.calls[0];
expect(init.body).toBe(params);
});
it('passes an already-serialized string body through untouched', async () => {
const raw = '{"already":"json"}';
await fetchRetry.post('/raw', raw);
const [, init] = fetchMock.mock.calls[0];
expect(init.body).toBe(raw);
expect(init.headers).toBeUndefined();
});
it('does not force a body or Content-Type when none is provided', async () => {
await fetchRetry.delete('/thing');
const [, init] = fetchMock.mock.calls[0];
expect(init.body).toBeUndefined();
expect(init.headers).toBeUndefined();
});
it('applies the same handling for put and patch', async () => {
const form = new FormData();
await fetchRetry.put('/upload', form);
let [, init] = fetchMock.mock.calls[0];
expect(init.body).toBe(form);
await fetchRetry.patch('/data', { hello: 'world' });
[, init] = fetchMock.mock.calls[1];
expect(init.body).toBe(JSON.stringify({ hello: 'world' }));
expect(init.headers).toMatchObject({ 'Content-Type': 'application/json' });
});
});
describe('FetchRetry failure logging', () => {
let fetchMock: jest.Mock;
const originalFetch = global.fetch;
beforeEach(() => {
if (fs.existsSync(TEST_LOG_PATH)) {
fs.unlinkSync(TEST_LOG_PATH);
}
});
afterEach(() => {
global.fetch = originalFetch;
if (fs.existsSync(TEST_LOG_PATH)) {
fs.unlinkSync(TEST_LOG_PATH);
}
});
it('records the real url, method, headers, and body for a failed POST', async () => {
fetchMock = jest.fn().mockResolvedValue(new Response(null, { status: 500 }));
global.fetch = fetchMock as unknown as typeof fetch;
const fetchRetry = new FetchRetry({ maxRetries: 1 }, TEST_LOG_PATH);
await expect(fetchRetry.post('/orders', { id: 1 })).rejects.toThrow();
const [failure] = await fetchRetry.getRetryManager().getFailedRequests();
expect(failure.url).toBe('/orders');
expect(failure.method).toBe('POST');
expect(failure.headers).toMatchObject({ 'content-type': 'application/json' });
expect(failure.body).toBe(JSON.stringify({ id: 1 }));
expect(failure.statusCode).toBe(500);
});
it('records the real url and method for a failed DELETE', async () => {
fetchMock = jest.fn().mockResolvedValue(new Response(null, { status: 503 }));
global.fetch = fetchMock as unknown as typeof fetch;
const fetchRetry = new FetchRetry({ maxRetries: 1 }, TEST_LOG_PATH);
await expect(fetchRetry.delete('/orders/1')).rejects.toThrow();
const [failure] = await fetchRetry.getRetryManager().getFailedRequests();
expect(failure.url).toBe('/orders/1');
expect(failure.method).toBe('DELETE');
});
it('records the url and method even when fetch itself throws a network error', async () => {
fetchMock = jest.fn().mockRejectedValue(new TypeError('fetch failed'));
global.fetch = fetchMock as unknown as typeof fetch;
const fetchRetry = new FetchRetry({ maxRetries: 1 }, TEST_LOG_PATH);
await expect(fetchRetry.get('/status')).rejects.toThrow('fetch failed');
const [failure] = await fetchRetry.getRetryManager().getFailedRequests();
expect(failure.url).toBe('/status');
expect(failure.method).toBe('GET');
expect(failure.error).toBe('fetch failed');
});
});