buffer.test.ts
This file
Path
src/utils/buffer.test.ts
79 lines · 3.0 KiB · TypeScript
Last changed by
fix(validator): cache `arrayBuffer` to use after validation (#1393)Yusuke Wadabefore adoption5cbd89c
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980import { SHA256 as sha256CryptoJS } from 'crypto-js'import { timingSafeEqual, bufferToString, bufferToFormData } from './buffer'
describe('buffer', () => { it('positive', async () => { expect( await timingSafeEqual( '127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935', '127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935' ) ).toBe(true) expect(await timingSafeEqual('a', 'a')).toBe(true) expect(await timingSafeEqual('', '')).toBe(true) // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore expect(await timingSafeEqual(undefined, undefined)).toBe(true) expect(await timingSafeEqual(true, true)).toBe(true) expect(await timingSafeEqual(false, false)).toBe(true) expect(await timingSafeEqual(true, true, (d: string) => sha256CryptoJS(d).toString())) })
it('negative', async () => { expect(await timingSafeEqual('a', 'b')).toBe(false) expect( await timingSafeEqual('a', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') ).toBe(false) expect( await timingSafeEqual('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'a') ).toBe(false) expect(await timingSafeEqual('alpha', 'beta')).toBe(false) expect(await timingSafeEqual(false, true)).toBe(false) // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore expect(await timingSafeEqual(false, undefined)).toBe(false) expect( await timingSafeEqual( () => {}, () => {} ) ).toBe(false) expect(await timingSafeEqual({}, {})).toBe(false) expect(await timingSafeEqual({ a: 1 }, { a: 1 })).toBe(false) expect(await timingSafeEqual({ a: 1 }, { a: 2 })).toBe(false) expect(await timingSafeEqual([1, 2], [1, 2])).toBe(false) expect(await timingSafeEqual([1, 2], [1, 2, 3])).toBe(false) })})
describe('bufferToString', () => { it('Should return あいうえお', () => { const bytes = [227, 129, 130, 227, 129, 132, 227, 129, 134, 227, 129, 136, 227, 129, 138] const buffer = Uint8Array.from(bytes).buffer expect(bufferToString(buffer)).toBe('あいうえお') })})
describe('bufferToFormData', () => { it('Should parse multipart/form-data from ArrayBuffer', () => { const encoder = new TextEncoder() const testData = '--sampleboundary\r\nContent-Disposition: form-data; name="test"\r\n\r\nHello\r\n--sampleboundary--' const arrayBuffer = encoder.encode(testData).buffer
const result = bufferToFormData(arrayBuffer, 'multipart/form-data; boundary=sampleboundary')
expect(result.get('test')).toBe('Hello') })
it('Should parse application/x-www-form-urlencoded from ArrayBuffer', () => { const encoder = new TextEncoder() const testData = 'key1=value1&key2=value2' const arrayBuffer = encoder.encode(testData).buffer
const result = bufferToFormData(arrayBuffer, 'application/x-www-form-urlencoded')
expect(result.get('key1')).toBe('value1') expect(result.get('key2')).toBe('value2') })})