Skip to content
← Public packages

@kentcdodds/sentry

Inspect Sentry organizations, projects, issues, and event summaries.

FollowFork

Files

request-init.test.ts

37 lines · 1.3 KB · TypeScript
import assert from 'node:assert/strict'
import test from 'node:test'
import { buildRequestInit } from './request-init.ts'

test('top-level method and object body become a JSON request', () => {
	assert.deepEqual(buildRequestInit({ path: '/x/', method: 'put', body: { status: 'resolved' } }), {
		method: 'PUT',
		body: '{"status":"resolved"}',
		headers: { 'content-type': 'application/json' },
	})
})

test('init still works and top-level fields win over it', () => {
	assert.deepEqual(
		buildRequestInit({
			path: '/x/',
			method: 'DELETE',
			headers: { 'X-Test': 'b' },
			init: { method: 'POST', headers: { 'x-test': 'a', accept: 'text/plain' } },
		}),
		{ method: 'DELETE', headers: { 'x-test': 'b', accept: 'text/plain' } },
	)
	assert.deepEqual(buildRequestInit({ path: '/x/', init: { method: 'PUT', body: 'raw' } }), {
		method: 'PUT',
		body: 'raw',
	})
})

test('plain GET has no init', () => {
	assert.equal(buildRequestInit({ path: '/x/', query: { a: 1 } }), undefined)
})

test('unknown keys and a body without a method throw', () => {
	assert.throws(() => buildRequestInit({ path: '/x/', methd: 'PUT' }), /unknown keys: methd/)
	assert.throws(() => buildRequestInit({ path: '/x/', body: { a: 1 } }), /needs a method/)
	assert.throws(() => buildRequestInit({ path: '/x/', method: '' }), /non-empty string/)
})