Skip to content
Myr

bytes.ts

This file

Path

test/bytes.ts

82 lines · 2.7 KiB · TypeScript

Last changed by

RefactorSindre Sorhusbefore adoptiona5ed57b

History of this file

Take it

The walk stopped after 200 commits: lines older than 37fc250 point at it.

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 1–8why, one step back
1import {Buffer} from 'node:buffer';
2import test from 'ava';3import ky from '../source/index.js';4import {createHttpTestServer} from './helpers/create-http-test-server.js';56const supportsBytes = typeof (globalThis.Response?.prototype as unknown as {bytes?: unknown})?.bytes === 'function';78test('.bytes() returns Uint8Array when supported', async t => {

Simplify testsSindre Sorhusbefore adoptiondfa3f59

line 9why, one step back
9 const server = await createHttpTestServer(t);

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 10–24why, one step back
10
11 server.get('/', (request, response) => {12 t.is(request.headers.accept, '*/*');13 // Send raw binary bytes14 response.end(Buffer.from([0, 1, 2, 255]));15 });1617 if (!supportsBytes) {18 await ky(server.url).text();19 t.pass();20 return;21 }2223 const bytes = await ky(server.url).bytes();24 t.true(bytes instanceof Uint8Array);

Fix bytes shortcut return type for Blob and Response compatibilitySindre Sorhusbefore adoption481f6f7

line 25why, one step back
25 t.true(bytes.buffer instanceof ArrayBuffer);

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

line 26why, one step back
26 t.deepEqual([...bytes], [0, 1, 2, 255]);

Fix bytes shortcut return type for Blob and Response compatibilitySindre Sorhusbefore adoption481f6f7

lines 27–28why, one step back
27 t.deepEqual(new Uint8Array(await new Blob([bytes]).arrayBuffer()), bytes);
28 t.deepEqual(new Uint8Array(await new Response(bytes).arrayBuffer()), bytes);

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 29–31why, one step back
29});
3031test('.bytes() throws on HTTP errors when supported', async t => {

Simplify testsSindre Sorhusbefore adoptiondfa3f59

line 32why, one step back
32 const server = await createHttpTestServer(t);

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 33–45why, one step back
33
34 server.get('/', (_request, response) => {35 response.status(400).end('nope');36 });3738 if (!supportsBytes) {39 await ky(server.url, {throwHttpErrors: false}).text();40 t.pass();41 return;42 }4344 await t.throwsAsync(ky(server.url).bytes(), {message: /Bad Request/});45});

RefactorSindre Sorhusbefore adoptiona5ed57b

lines 46–82why, one step back
46
47// Support is checked on each call rather than when Ky is imported, so a polyfill added later is picked up, also by the promise that reports a setup error.48test.serial('.bytes() is offered only while Response.prototype.bytes exists', async t => {49 const descriptor = Object.getOwnPropertyDescriptor(Response.prototype, 'bytes');50 t.teardown(() => {51 Reflect.deleteProperty(Response.prototype, 'bytes');52 if (descriptor) {53 Object.defineProperty(Response.prototype, 'bytes', descriptor);54 }55 });5657 const fetch = async () => new Response('ok');5859 Reflect.deleteProperty(Response.prototype, 'bytes');6061 const unsupportedPromise = ky('https://example.com', {fetch});62 const unsupportedSetupErrorPromise = ky('https://example.com', {timeout: -1});63 t.false('bytes' in unsupportedPromise);64 t.false('bytes' in unsupportedSetupErrorPromise);65 await t.throwsAsync(unsupportedSetupErrorPromise);66 await unsupportedPromise;6768 Object.defineProperty(Response.prototype, 'bytes', {69 configurable: true,70 writable: true,71 async value(this: Response) {72 return new Uint8Array(await this.arrayBuffer());73 },74 });7576 const supportedPromise = ky('https://example.com', {fetch});77 const supportedSetupErrorPromise = ky('https://example.com', {timeout: -1});78 t.true('bytes' in supportedPromise);79 t.true('bytes' in supportedSetupErrorPromise);80 await t.throwsAsync(supportedSetupErrorPromise);81 t.deepEqual([...await supportedPromise.bytes()], [111, 107]);82});