Skip to content
Myr

http-semantics.ts

This file

Path

test/http-semantics.ts

155 lines · 5.3 KiB · TypeScript

Last changed by

More testsSindre Sorhusbefore adoptioncd1dba0

History of this file

Take it

More testsSindre Sorhusbefore adoptioncd1dba0

lines 1–155why, one step back
1import test from 'ava';
2import ky, {HTTPError} from '../source/index.js';3import {createHttpTestServer} from './helpers/create-http-test-server.js';45// RFC 9110 §§9.3.2 and 8.6 allow HEAD to advertise the corresponding GET representation's length without sending content.6// https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.27for (const status of [200, 404]) {8 test(`HEAD ${status} preserves representation headers without consuming a body`, async t => {9 const server = await createHttpTestServer(t);10 let progressCalls = 0;11 let parserCalls = 0;12 server.head('/', (_request, response) => {13 response.writeHead(status, {'content-type': 'application/json', 'content-length': '1234'}).end();14 });15 const request = ky.head(server.url, {16 retry: 0,17 onDownloadProgress() {18 progressCalls++;19 },20 parseJson() {21 parserCalls++;22 return {};23 },24 });25 if (status === 200) {26 const response = await request;27 t.is(response.headers.get('content-length'), '1234');28 t.is(await response.text(), '');29 } else {30 const error = await t.throwsAsync(request, {instanceOf: HTTPError});31 t.is(error.response.status, 404);32 t.is(error.response.headers.get('content-length'), '1234');33 t.is(error.data, undefined);34 }3536 t.is(progressCalls, 0);37 t.is(parserCalls, 0);38 });39}4041// RFC 9110 §§15.3.5–15.3.6 prohibit content in 204 and 205 responses.42for (const status of [204, 205]) {43 test(`${status} completes without body progress and retains response metadata`, async t => {44 const server = await createHttpTestServer(t);45 server.get('/', (_request, response) => {46 response.writeHead(status, {etag: '"version-2"'}).end();47 });48 let progressCalls = 0;49 const response = await ky(server.url, {50 onDownloadProgress() {51 progressCalls++;52 },53 });5455 t.is(response.status, status);56 t.true(response.ok);57 t.is(response.headers.get('etag'), '"version-2"');58 t.is(await response.text(), '');59 t.is(progressCalls, 0);60 });61}6263// A 304 has no content; Content-Length may describe the selected representation (RFC 9110 §§15.4.5 and 8.6).64for (const [condition, value] of [65 ['if-none-match', '"version-1"'],66 ['if-modified-since', 'Wed, 01 Jan 2025 00:00:00 GMT'],67] as const) {68 test(`conditional requests preserve ${condition} and expose bodyless 304 responses`, async t => {69 const server = await createHttpTestServer(t);70 server.get('/', (request, response) => {71 t.is(request.headers[condition], value);72 response.writeHead(304, {etag: '"version-1"', 'last-modified': 'Wed, 01 Jan 2025 00:00:00 GMT', 'content-length': '42'}).end();73 });74 let progressCalls = 0;75 const response = await ky(server.url, {76 headers: {[condition]: value},77 throwHttpErrors: false,78 onDownloadProgress() {79 progressCalls++;80 },81 });8283 t.is(response.status, 304);84 t.false(response.ok);85 t.is(response.headers.get('etag'), '"version-1"');86 t.is(response.headers.get('content-length'), '42');87 t.is(await response.text(), '');88 t.is(progressCalls, 0);89 });90}9192test('304 uses the default HTTP error policy without retrying or parsing absent JSON', async t => {93 const server = await createHttpTestServer(t);94 let requests = 0;95 let parserCalls = 0;96 server.get('/', (_request, response) => {97 requests++;98 response.writeHead(304, {etag: '"version-1"', 'content-type': 'application/json'}).end();99 });100 const error = await t.throwsAsync(ky(server.url, {101 headers: {'if-none-match': '"version-1"'},102 parseJson() {103 parserCalls++;104 return {};105 },106 }), {instanceOf: HTTPError});107108 t.is(error.response.status, 304);109 t.is(error.data, undefined);110 t.is(requests, 1);111 t.is(parserCalls, 0);112});113114// RFC 9110 §§14.4 and 15.3.7: a single-range response carries the selected bytes and their Content-Range.115test('206 preserves byte-range headers and reports progress for the partial representation', async t => {116 const server = await createHttpTestServer(t);117 server.get('/', (request, response) => {118 t.is(request.headers.range, 'bytes=2-5');119 t.is(request.headers['if-range'], '"version-1"');120 response.writeHead(206, {'content-range': 'bytes 2-5/10', 'content-length': '4'}).end('2345');121 });122 const chunks: Uint8Array[] = [];123 const transferred: number[] = [];124 const response = await ky(server.url, {125 headers: {range: 'bytes=2-5', 'if-range': '"version-1"'},126 onDownloadProgress(progress, chunk) {127 chunks.push(chunk);128 transferred.push(progress.transferredBytes);129 },130 });131132 t.is(response.status, 206);133 t.true(response.ok);134 t.is(response.headers.get('content-range'), 'bytes 2-5/10');135 t.is(await response.text(), '2345');136 t.is(Buffer.concat(chunks).toString(), '2345');137 t.is(transferred.at(-1), 4);138});139140test('416 preserves the unsatisfied Content-Range and error body without retrying', async t => {141 // RFC 9110 §15.5.17 permits Content-Range to indicate the current representation length.142 const server = await createHttpTestServer(t);143 let requests = 0;144 server.get('/', (request, response) => {145 requests++;146 t.is(request.headers.range, 'bytes=100-');147 response.writeHead(416, {'content-range': 'bytes */10', 'content-type': 'text/plain'}).end('Range unavailable');148 });149 const error = await t.throwsAsync(ky(server.url, {headers: {range: 'bytes=100-'}}), {instanceOf: HTTPError});150151 t.is(error.response.status, 416);152 t.is(error.response.headers.get('content-range'), 'bytes */10');153 t.is(error.data, 'Range unavailable');154 t.is(requests, 1);155});