23 lines
894 B
TypeScript
23 lines
894 B
TypeScript
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
|
|
import {expectArrayPayload, readJsonPayload} from '../src/apiPayload.ts'
|
|
|
|
test('a truncated successful JSON response is rejected instead of becoming an object', async () => {
|
|
const response = new Response('{"messages":[', {
|
|
status: 200,
|
|
headers: {'Content-Type': 'application/json'},
|
|
})
|
|
await assert.rejects(() => readJsonPayload(response), /incomplète/i)
|
|
})
|
|
|
|
test('a valid JSON response is decoded', async () => {
|
|
const response = new Response('[{"id":"message-1"}]', {status: 200})
|
|
assert.deepEqual(await readJsonPayload(response), [{id: 'message-1'}])
|
|
})
|
|
|
|
test('array endpoints reject a valid but malformed payload', () => {
|
|
assert.throws(() => expectArrayPayload({}, 'messages'), /messages.*invalide/i)
|
|
assert.deepEqual(expectArrayPayload([{id: 'message-1'}], 'messages'), [{id: 'message-1'}])
|
|
})
|