|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it, vi } from 'vitest' |
| 5 | +import { PayloadSizeLimitError } from '@/lib/core/utils/stream-limits' |
| 6 | +import { stringifyRequestWithinLimit } from '@/tools/request-body-size.server' |
| 7 | + |
| 8 | +describe('stringifyRequestWithinLimit', () => { |
| 9 | + it.each([ |
| 10 | + { 'escaped"key\n': '\u0000é😀\ud800', empty: {}, array: [1, null, true] }, |
| 11 | + { missing: undefined, array: [undefined, () => 1, Symbol('omitted'), Number.NaN] }, |
| 12 | + { date: new Date('2026-01-01T00:00:00Z'), boxed: [Object(1), Object('é'), Object(false)] }, |
| 13 | + ])('preserves native JSON and its exact UTF-8 limit for %j', (value) => { |
| 14 | + const expected = JSON.stringify(value) |
| 15 | + const bytes = Buffer.byteLength(expected, 'utf8') |
| 16 | + expect(stringifyRequestWithinLimit(value, bytes)).toBe(expected) |
| 17 | + expect(() => stringifyRequestWithinLimit(value, bytes - 1)).toThrow(PayloadSizeLimitError) |
| 18 | + }) |
| 19 | + |
| 20 | + it('invokes getters, toJSON and boxed conversions only once', () => { |
| 21 | + const getter = vi.fn(() => ({ toJSON })) |
| 22 | + const toJSON = vi.fn(() => 'value') |
| 23 | + const numberConversion = vi.fn(() => 3) |
| 24 | + const stringConversion = vi.fn(() => 'text') |
| 25 | + const value = { |
| 26 | + get data() { |
| 27 | + return getter() |
| 28 | + }, |
| 29 | + number: Object.assign(Object(1), { [Symbol.toPrimitive]: numberConversion }), |
| 30 | + string: Object.assign(Object('original'), { [Symbol.toPrimitive]: stringConversion }), |
| 31 | + } |
| 32 | + |
| 33 | + expect(stringifyRequestWithinLimit(value, 100)).toBe( |
| 34 | + '{"data":"value","number":3,"string":"text"}' |
| 35 | + ) |
| 36 | + for (const hook of [getter, toJSON, numberConversion, stringConversion]) { |
| 37 | + expect(hook).toHaveBeenCalledTimes(1) |
| 38 | + } |
| 39 | + }) |
| 40 | + |
| 41 | + it('stops native traversal before later getters on oversized strings', () => { |
| 42 | + const later = vi.fn() |
| 43 | + const value = { |
| 44 | + large: '\u0000'.repeat(100), |
| 45 | + get later() { |
| 46 | + return later() |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + expect(() => stringifyRequestWithinLimit(value, 50)).toThrow(PayloadSizeLimitError) |
| 51 | + expect(later).not.toHaveBeenCalled() |
| 52 | + }) |
| 53 | + |
| 54 | + it('preserves native omissions, shared objects, cycle errors and bigint conversion errors', () => { |
| 55 | + const shared = { a: 1 } |
| 56 | + expect(stringifyRequestWithinLimit([shared, shared], 100)).toBe( |
| 57 | + JSON.stringify([shared, shared]) |
| 58 | + ) |
| 59 | + expect(stringifyRequestWithinLimit(undefined, 100)).toBeUndefined() |
| 60 | + const cycle: { self?: unknown } = {} |
| 61 | + cycle.self = cycle |
| 62 | + expect(() => stringifyRequestWithinLimit(cycle, 100)).toThrow(TypeError) |
| 63 | + const number = Object.assign(Object(1), { [Symbol.toPrimitive]: () => 1n }) |
| 64 | + expect(() => JSON.stringify(number)).toThrow(TypeError) |
| 65 | + expect(() => stringifyRequestWithinLimit(number, 100)).toThrow(TypeError) |
| 66 | + expect(() => stringifyRequestWithinLimit(1n, 100)).toThrow(TypeError) |
| 67 | + }) |
| 68 | +}) |
0 commit comments