2024-01-12 06:18:46 -08:00
|
|
|
import { Readable } from 'node:stream';
|
|
|
|
import { createGunzip } from 'node:zlib';
|
2024-07-11 08:03:52 -07:00
|
|
|
import { binaryToBuffer } from '@/BinaryData/utils';
|
2024-01-12 06:18:46 -08:00
|
|
|
|
|
|
|
describe('BinaryData/utils', () => {
|
2024-07-11 08:03:52 -07:00
|
|
|
describe('binaryToBuffer', () => {
|
2024-01-12 06:18:46 -08:00
|
|
|
it('should handle buffer objects', async () => {
|
|
|
|
const body = Buffer.from('test');
|
2024-07-11 08:03:52 -07:00
|
|
|
expect((await binaryToBuffer(body)).toString()).toEqual('test');
|
2024-01-12 06:18:46 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle valid uncompressed Readable streams', async () => {
|
|
|
|
const body = Readable.from(Buffer.from('test'));
|
2024-07-11 08:03:52 -07:00
|
|
|
expect((await binaryToBuffer(body)).toString()).toEqual('test');
|
2024-01-12 06:18:46 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle valid compressed Readable streams', async () => {
|
|
|
|
const gunzip = createGunzip();
|
|
|
|
const body = Readable.from(
|
|
|
|
Buffer.from('1f8b08000000000000032b492d2e01000c7e7fd804000000', 'hex'),
|
|
|
|
).pipe(gunzip);
|
2024-07-11 08:03:52 -07:00
|
|
|
expect((await binaryToBuffer(body)).toString()).toEqual('test');
|
2024-01-12 06:18:46 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw on invalid compressed Readable streams', async () => {
|
|
|
|
const gunzip = createGunzip();
|
|
|
|
const body = Readable.from(Buffer.from('0001f8b080000000000000000', 'hex')).pipe(gunzip);
|
2024-07-11 08:03:52 -07:00
|
|
|
await expect(binaryToBuffer(body)).rejects.toThrow(
|
|
|
|
new Error('Failed to decompress response'),
|
|
|
|
);
|
2024-01-12 06:18:46 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|