Skip to content
Myr

buffer.test.ts

This file

Path

src/utils/buffer.test.ts

120 lines · 4.4 KiB · TypeScript

Last changed by

chore: remove `crypto-js` from dev dependencies (#3447)Yusuke Wadabefore adoptionf87c221

History of this file

Take it

The walk stopped after 200 commits: lines older than 81a1f07 point at it.

chore: remove `crypto-js` from dev dependencies (#3447)Yusuke Wadabefore adoptionf87c221

line 1why, one step back
1import { createHash } from 'crypto'

fix(validator): support multipart headers without a space separating boundary (#3286)Ernest Badubefore adoption81a1f07

lines 2–44why, one step back
2import { bufferToFormData, bufferToString, equal, timingSafeEqual } from './buffer'
34describe('equal', () => {5 it('should return true for identical ArrayBuffers', () => {6 const buffer1 = new ArrayBuffer(1)7 const buffer2 = buffer18 expect(equal(buffer1, buffer2)).toBe(true)9 })1011 it('should return false for ArrayBuffers of different lengths', () => {12 const buffer1 = new ArrayBuffer(1)13 const buffer2 = new ArrayBuffer(2)14 expect(equal(buffer1, buffer2)).toBe(false)15 })1617 it('should return false for ArrayBuffers with different content', () => {18 const buffer1 = new Uint8Array([1, 2, 3, 4]).buffer19 const buffer2 = new Uint8Array([2, 2, 3, 4]).buffer20 expect(equal(buffer1, buffer2)).toBe(false)21 })2223 it('should return true for ArrayBuffers with identical content', () => {24 const buffer1 = new Uint8Array([1, 2, 3, 4]).buffer25 const buffer2 = new Uint8Array([1, 2, 3, 4]).buffer26 expect(equal(buffer1, buffer2)).toBe(true)27 })28})2930describe('buffer', () => {31 it('positive', async () => {32 expect(33 await timingSafeEqual(34 '127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935',35 '127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935'36 )37 ).toBe(true)38 expect(await timingSafeEqual('a', 'a')).toBe(true)39 expect(await timingSafeEqual('', '')).toBe(true)40 // eslint-disable-next-line @typescript-eslint/ban-ts-comment41 // @ts-ignore42 expect(await timingSafeEqual(undefined, undefined)).toBe(true)43 expect(await timingSafeEqual(true, true)).toBe(true)44 expect(await timingSafeEqual(false, false)).toBe(true)

chore: remove `crypto-js` from dev dependencies (#3447)Yusuke Wadabefore adoptionf87c221

lines 45–49why, one step back
45 expect(
46 await timingSafeEqual(true, true, (d: boolean) =>47 createHash('sha256').update(d.toString()).digest('hex')48 )49 )

fix(validator): support multipart headers without a space separating boundary (#3286)Ernest Badubefore adoption81a1f07

lines 50–120why, one step back
50 })
5152 it('negative', async () => {53 expect(await timingSafeEqual('a', 'b')).toBe(false)54 expect(55 await timingSafeEqual('a', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')56 ).toBe(false)57 expect(58 await timingSafeEqual('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'a')59 ).toBe(false)60 expect(await timingSafeEqual('alpha', 'beta')).toBe(false)61 expect(await timingSafeEqual(false, true)).toBe(false)62 // eslint-disable-next-line @typescript-eslint/ban-ts-comment63 // @ts-ignore64 expect(await timingSafeEqual(false, undefined)).toBe(false)65 expect(66 await timingSafeEqual(67 () => {},68 () => {}69 )70 ).toBe(false)71 expect(await timingSafeEqual({}, {})).toBe(false)72 expect(await timingSafeEqual({ a: 1 }, { a: 1 })).toBe(false)73 expect(await timingSafeEqual({ a: 1 }, { a: 2 })).toBe(false)74 expect(await timingSafeEqual([1, 2], [1, 2])).toBe(false)75 expect(await timingSafeEqual([1, 2], [1, 2, 3])).toBe(false)76 expect(await timingSafeEqual('a', 'b', () => undefined)).toBe(false)77 })78})7980describe('bufferToString', () => {81 it('Should return あいうえお', () => {82 const bytes = [227, 129, 130, 227, 129, 132, 227, 129, 134, 227, 129, 136, 227, 129, 138]83 const buffer = Uint8Array.from(bytes).buffer84 expect(bufferToString(buffer)).toBe('あいうえお')85 })86 it('should return the passed arguments as is ', () => {87 const notBuffer = 'あいうえお' as unknown as ArrayBuffer88 expect(bufferToString(notBuffer)).toBe('あいうえお')89 })90})9192describe('bufferToFormData', () => {93 it('Should parse multipart/form-data from ArrayBuffer', async () => {94 const encoder = new TextEncoder()95 const testData =96 '--sampleboundary\r\nContent-Disposition: form-data; name="test"\r\n\r\nHello\r\n--sampleboundary--'97 const arrayBuffer = encoder.encode(testData).buffer9899 const result = await bufferToFormData(100 arrayBuffer,101 'multipart/form-data; boundary=sampleboundary'102 )103104 expect(result.get('test')).toBe('Hello')105 })106107 it('Should parse application/x-www-form-urlencoded from ArrayBuffer', async () => {108 const encoder = new TextEncoder()109 const searchParams = new URLSearchParams()110 searchParams.append('id', '123')111 searchParams.append('title', 'Good title')112 const testData = searchParams.toString()113 const arrayBuffer = encoder.encode(testData).buffer114115 const result = await bufferToFormData(arrayBuffer, 'application/x-www-form-urlencoded')116117 expect(result.get('id')).toBe('123')118 expect(result.get('title')).toBe('Good title')119 })120})