Skip to content
← Public packages

@kentcdodds/x

X API v2 helpers for posts, search, DMs, and encrypted X Chat. Generic Activity CRC/HMAC utilities only — @kodykoala Pam wake lives in @kentcdodds/kodykoala-activity.

FollowFork

Files

limited-body.test.ts

46 lines · 1.1 KB · TypeScript
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { readLimitedBody } from '../src/limited-body.ts'
test('small convenience bodies stay byte exact', async () =>
	assert.deepEqual(
		await readLimitedBody(new Response(new Uint8Array([0, 255, 42]))),
		new Uint8Array([0, 255, 42]),
	))
test('oversized length is cancelled before body read', async () => {
	let cancelled = false
	await assert.rejects(
		readLimitedBody(
			new Response(
				new ReadableStream({
					cancel() {
						cancelled = true
					},
				}),
				{ headers: { 'content-length': '20' } },
			),
			4,
		),
		/exceeds/,
	)
	assert(cancelled)
})
test('unknown-length streams are bounded and disposed', async () => {
	let cancelled = false
	const r = new Response(
		new ReadableStream({
			pull(c) {
				c.enqueue(new Uint8Array(3))
			},
			cancel() {
				cancelled = true
			},
		}),
	)
	await assert.rejects(readLimitedBody(r, 4), /exceeds/)
	assert(cancelled)
})
test('partial declared body is refused', async () =>
	assert.rejects(
		readLimitedBody(new Response('x', { headers: { 'content-length': '3' } }), 4),
		/Partial/,
	))