-
-
Notifications
You must be signed in to change notification settings - Fork 334
/
browser.ts
489 lines (409 loc) 路 13.3 KB
/
browser.ts
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import test, {type ExecutionContext} from 'ava';
import busboy from 'busboy';
import express from 'express';
import {chromium, webkit, type Page} from 'playwright';
import type ky from '../source/index.js';
import {createHttpTestServer, type ExtendedHttpTestServer, type HttpServerOptions} from './helpers/create-http-test-server.js';
import {parseRawBody} from './helpers/parse-body.js';
import {browserTest, defaultBrowsersTest} from './helpers/with-page.js';
declare global {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Window {
ky: typeof ky;
}
}
const DIST_DIR = new URL('../distribution', import.meta.url).toString();
const createEsmTestServer = async (options?: HttpServerOptions) => {
const server = await createHttpTestServer(options);
server.use('/distribution', express.static(DIST_DIR.replace(/^file:\/\//, '')));
server.use((_, response, next) => {
response.set('Connection', 'close');
next();
});
return server;
};
const KY_SCRIPT = {
type: 'module',
content: `
import ky from '/distribution/index.js';
globalThis.ky = ky;
`,
};
const addKyScriptToPage = async (page: Page) => {
await page.addScriptTag(KY_SCRIPT);
await page.waitForFunction(() => typeof window.ky === 'function');
};
let server: ExtendedHttpTestServer;
test.beforeEach(async () => {
server = await createEsmTestServer();
});
test.afterEach(async () => {
await server.close();
});
defaultBrowsersTest('prefixUrl option', async (t: ExecutionContext, page: Page) => {
server.get('/', (_request, response) => {
response.end('zebra');
});
server.get('/api/unicorn', (_request, response) => {
response.end('rainbow');
});
await page.goto(server.url);
await addKyScriptToPage(page);
await t.throwsAsync(
page.evaluate(async () => window.ky('/foo', {prefixUrl: '/'})),
{message: /`input` must not begin with a slash when using `prefixUrl`/},
);
const results = await page.evaluate(async (url: string) => Promise.all([
window.ky(`${url}/api/unicorn`).text(),
// @ts-expect-error unsupported {prefixUrl: null} type
window.ky(`${url}/api/unicorn`, {prefixUrl: null}).text(),
window.ky('api/unicorn', {prefixUrl: url}).text(),
window.ky('api/unicorn', {prefixUrl: `${url}/`}).text(),
]), server.url);
t.deepEqual(results, ['rainbow', 'rainbow', 'rainbow', 'rainbow']);
});
defaultBrowsersTest('aborting a request', async (t: ExecutionContext, page: Page) => {
server.get('/', (_request, response) => {
response.end('meow');
});
server.get('/test', (_request, response) => {
setTimeout(() => {
response.end('ok');
}, 500);
});
await page.goto(server.url);
await addKyScriptToPage(page);
const errorName = await page.evaluate(async (url: string) => {
const controller = new AbortController();
const request = window.ky(`${url}/test`, {signal: controller.signal}).text();
controller.abort();
return request.catch(error_ => error_.name);
}, server.url);
t.is(errorName, 'AbortError');
});
defaultBrowsersTest('should copy origin response info when using `onDownloadProgress`', async (t: ExecutionContext, page: Page) => {
const json = {hello: 'world'};
const status = 202;
const statusText = 'Accepted';
server.get('/', (_request, response) => {
response.end('meow');
});
server.get('/test', (_request, response) => {
setTimeout(() => {
response.statusMessage = statusText;
response.status(status).header('X-ky-Header', 'ky').json(json);
}, 500);
});
await page.goto(server.url);
await addKyScriptToPage(page);
const data = await page.evaluate(async (url: string) => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const request = window.ky.get(`${url}/test`, {onDownloadProgress() {}}).then(async v => ({
headers: v.headers.get('X-ky-Header'),
status: v.status,
statusText: v.statusText,
data: await v.json(),
}));
return request;
}, server.url);
t.deepEqual(data, {
status,
headers: 'ky',
statusText,
data: json,
});
});
defaultBrowsersTest('should not copy response body with 204 status code when using `onDownloadProgress` ', async (t: ExecutionContext, page: Page) => {
const status = 204;
const statusText = 'No content';
server.get('/', (_request, response) => {
response.end('meow');
});
server.get('/test', (_request, response) => {
setTimeout(() => {
response.statusMessage = statusText;
response.status(status).header('X-ky-Header', 'ky').end(null);
}, 500);
});
await page.goto(server.url);
await addKyScriptToPage(page);
const data = await page.evaluate(async (url: string) => {
const progress: any = [];
let totalBytes = 0;
const response = await window.ky.get(`${url}/test`, {
onDownloadProgress(progressEvent) {
progress.push(progressEvent);
},
}).then(async v => {
totalBytes = Number(v.headers.get('content-length')) || 0;
return ({
headers: v.headers.get('X-ky-Header'),
status: v.status,
statusText: v.statusText,
});
});
return {
response,
progress,
totalBytes,
};
}, server.url);
t.deepEqual(data.response, {
status,
headers: 'ky',
statusText,
});
t.deepEqual(data.progress, [{
percent: 1,
totalBytes: data.totalBytes,
transferredBytes: 0,
}]);
});
browserTest('aborting a request with onDownloadProgress', [chromium], async (t: ExecutionContext, page: Page) => {
server.get('/', (_request, response) => {
response.end('meow');
});
server.get('/test', (_request, response) => {
response.writeHead(200, {
'content-length': '4',
});
response.write('me');
setTimeout(() => {
response.end('ow');
}, 1000);
});
await page.goto(server.url);
await addKyScriptToPage(page);
const error = await page.evaluate(async (url: string) => {
const controller = new AbortController();
// eslint-disable-next-line @typescript-eslint/no-empty-function
const request = window.ky(`${url}/test`, {signal: controller.signal, onDownloadProgress() {}}).text();
setTimeout(() => {
controller.abort();
}, 500);
return request.catch(error_ => error_.toString());
}, server.url);
// This should be an AbortError like in the 'aborting a request' test, but there is a bug in Chromium
t.is(error, 'TypeError: Failed to fetch');
});
defaultBrowsersTest(
'throws TimeoutError even though it does not support AbortController',
async (t: ExecutionContext, page: Page) => {
server.get('/', (_request, response) => {
response.end();
});
server.get('/slow', (_request, response) => {
setTimeout(() => {
response.end('ok');
}, 1000);
});
await page.goto(server.url);
await page.addScriptTag({content: 'window.AbortController = undefined;\n'});
await addKyScriptToPage(page);
const error = await page.evaluate(async (url: string) => {
const request = window.ky(`${url}/slow`, {timeout: 500}).text();
return request.catch(error_ => ({
message: error_.toString(),
request: {url: error_.request.url},
}));
}, server.url);
if (typeof error !== 'object') {
throw new TypeError('Expected to have an object error');
}
t.is(error.message, 'TimeoutError: Request timed out');
t.is(error.request.url, `${server.url}/slow`);
},
);
// TODO: Fix.
// browserTest('onDownloadProgress works', [chromium, webkit], async (t: ExecutionContext, page: Page) => {
// server.get('/', (_request, response) => {
// response.writeHead(200, {
// 'content-length': '4',
// });
// response.write('me');
// setTimeout(() => {
// response.end('ow');
// }, 1000);
// });
// await page.goto(server.url);
// await addKyScriptToPage(page);
// const result = await page.evaluate(async (url: string) => {
// // `new TextDecoder('utf-8').decode` hangs up?
// const decodeUtf8 = (array: Uint8Array) => String.fromCodePoint(...array);
// const data: any[] = [];
// const text = await window
// .ky(url, {
// onDownloadProgress(progress, chunk) {
// const stringifiedChunk = decodeUtf8(chunk);
// data.push([progress, stringifiedChunk]);
// },
// })
// .text();
// return {data, text};
// }, server.url);
// t.deepEqual(result.data, [
// [{percent: 0, transferredBytes: 0, totalBytes: 4}, ''],
// [{percent: 0.5, transferredBytes: 2, totalBytes: 4}, 'me'],
// [{percent: 1, transferredBytes: 4, totalBytes: 4}, 'ow'],
// ]);
// t.is(result.text, 'meow');
// });
defaultBrowsersTest('throws if onDownloadProgress is not a function', async (t: ExecutionContext, page: Page) => {
server.get('/', (_request, response) => {
response.end();
});
await page.goto(server.url);
await addKyScriptToPage(page);
const error = await page.evaluate(async (url: string) => {
// @ts-expect-error
const request = window.ky(url, {onDownloadProgress: 1}).text();
return request.catch(error_ => error_.toString());
}, server.url);
t.is(error, 'TypeError: The `onDownloadProgress` option must be a function');
});
defaultBrowsersTest('throws if does not support ReadableStream', async (t: ExecutionContext, page: Page) => {
server.get('/', (_request, response) => {
response.end();
});
await page.goto(server.url);
await page.addScriptTag({content: 'window.ReadableStream = undefined;\n'});
await addKyScriptToPage(page);
const error = await page.evaluate(async (url: string) => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const request = window.ky(url, {onDownloadProgress() {}}).text();
return request.catch(error_ => error_.toString());
}, server.url);
t.is(error, 'Error: Streams are not supported in your environment. `ReadableStream` is missing.');
});
defaultBrowsersTest('FormData with searchParams', async (t: ExecutionContext, page: Page) => {
t.plan(3);
server.get('/', (_request, response) => {
response.end();
});
server.post('/', async (request, response) => {
const requestBody = await parseRawBody(request);
const contentType = request.headers['content-type'];
const boundary = contentType!.split('boundary=')[1];
t.truthy(requestBody.includes(boundary!));
t.regex(requestBody, /bubblegum pie/);
t.deepEqual(request.query, {foo: '1'});
response.end();
});
await page.goto(server.url);
await addKyScriptToPage(page);
await page.evaluate(async (url: string) => {
const formData = new window.FormData();
formData.append('file', new window.File(['bubblegum pie'], 'my-file'));
return window.ky(url, {
method: 'post',
searchParams: 'foo=1',
body: formData,
});
}, server.url);
});
defaultBrowsersTest('FormData with searchParams ("multipart/form-data" parser)', async (t: ExecutionContext, page: Page) => {
t.plan(3);
server.get('/', (_request, response) => {
response.end();
});
server.post('/', async (request, response) => {
const [body, error] = await new Promise(resolve => {
// @ts-expect-error
const busboyInstance = busboy({headers: request.headers});
busboyInstance.on('error', (error: Error) => {
resolve([null, error]);
});
// eslint-disable-next-line max-params
busboyInstance.on('file', async (fieldname, file, filename, encoding, mimetype) => {
let fileContent = '';
try {
for await (const chunk of file) {
fileContent += chunk; // eslint-disable-line @typescript-eslint/restrict-plus-operands
}
resolve([{fieldname, filename, fileContent}, undefined]);
} catch (error_: unknown) {
resolve([null, error_]);
}
});
busboyInstance.on('finish', () => {
response.writeHead(303, {Connection: 'close', Location: '/'});
response.end();
});
setTimeout(() => {
resolve([null, new Error('Timeout')]);
}, 3000);
request.pipe(busboyInstance);
});
t.falsy(error);
t.deepEqual(request.query, {foo: '1'});
t.deepEqual(body, {
fieldname: 'file',
filename: {
filename: 'my-file',
encoding: '7bit',
mimeType: 'text/plain',
},
fileContent: 'bubblegum pie',
});
});
await page.goto(server.url);
await addKyScriptToPage(page);
await page.evaluate(async url => {
const formData = new window.FormData();
formData.append('file', new window.File(['bubblegum pie'], 'my-file', {type: 'text/plain'}));
return window.ky(url, {
method: 'post',
searchParams: 'foo=1',
body: formData,
});
}, server.url);
});
defaultBrowsersTest(
'headers are preserved when input is a Request and there are searchParams in the options',
async (t: ExecutionContext, page: Page) => {
t.plan(2);
server.get('/', (_request, response) => {
response.end();
});
server.get('/test', (request, response) => {
t.is(request.headers['content-type'], 'text/css');
t.deepEqual(request.query, {foo: '1'});
response.end();
});
await page.goto(server.url);
await addKyScriptToPage(page);
await page.evaluate(async (url: string) => {
const request = new window.Request(`${url}/test`, {
headers: {'content-type': 'text/css'},
});
return window
.ky(request, {
searchParams: 'foo=1',
})
.text();
}, server.url);
},
);
browserTest('retry with body', [chromium, webkit], async (t: ExecutionContext, page: Page) => {
t.plan(4);
let requestCount = 0;
server.get('/', (_request, response) => {
response.end('zebra');
});
server.put('/test', async (request, response) => {
requestCount++;
t.is(request.body, 'foo');
response.sendStatus(502);
});
await page.goto(server.url);
await addKyScriptToPage(page);
await t.throwsAsync(
page.evaluate(async (url: string) => window.ky(`${url}/test`, {
body: 'foo',
method: 'PUT',
retry: 1,
}), server.url),
{message: /HTTPError: Request failed with status code 502 Bad Gateway/},
);
t.is(requestCount, 2);
});