oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1// post.test.mjs — post's timeout (review round 4, L3): a request that
2// never answers is aborted, so the saver can retry; a keepalive one is
3// never timed out.
4
5import { test } from 'node:test'
6import assert from 'node:assert/strict'
7import { poster } from './post.mjs'
8
9const hanging = (url, init) => new Promise((resolve, reject) => {
10 init.signal?.addEventListener('abort', () => reject(new Error('aborted')))
11})
12
13test('a POST that never answers is aborted after the timeout', async () => {
14 const post = poster(hanging, 20)
15 const started = Date.now()
16 await assert.rejects(post('/p/acme/draft', { body: 'x' }))
17 assert.ok(Date.now() - started < 1000)
18})
19
20test('a keepalive POST carries no signal, and an answer clears the timer', async () => {
21 let seen = null
22 const post = poster(async (url, init) => { seen = init; return { status: 200, json: async () => ({ ok: true }) } }, 20)
23 assert.deepEqual(await post('/x', { a: '1' }, { keepalive: true }), { status: 200, json: { ok: true } })
24 assert.equal(seen.signal, undefined)
25 assert.equal(seen.keepalive, true)
26})
27