This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
/
index.test.js
58 lines (51 loc) · 1.94 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
const streamToBuffer = require('../');
const assert = require('assert');
const path = require('path');
const fs = require('fs');
describe('stream-to-buf', () => {
it('should collect buffer success', async () => {
const small = fs.createReadStream(path.join(__dirname, 'fixtures/smallfile'));
let buffer = await streamToBuffer(small);
assert(buffer.length === 1024);
const big = fs.createReadStream(path.join(__dirname, 'fixtures/bigfile'));
buffer = await streamToBuffer(big);
assert(buffer.length === 102400);
});
it('should collect buffer when end and close both emitted', async () => {
const small = fs.createReadStream(path.join(__dirname, 'fixtures/smallfile'));
small.once('end', () => small.emit('close'));
const buffer = await streamToBuffer(small);
assert(buffer.length === 1024);
});
it('should return undefined if not readable', async () => {
const buffer = await streamToBuffer('foo');
assert(!buffer);
});
it('should collect buffer with maxSize', async () => {
const small = fs.createReadStream(path.join(__dirname, 'fixtures/smallfile'));
const big = fs.createReadStream(path.join(__dirname, 'fixtures/bigfile'));
const buffer = await streamToBuffer(small, { maxSize: '2kb' });
assert(buffer.length === 1024);
try {
await streamToBuffer(big, { maxSize: '2kb' });
throw new Error('should not execute');
} catch (err) {
assert(err.message === 'entity size exceed 2kb');
assert(err.code === 'ENTITY_TOO_LARGE');
assert(err.status === 413);
}
});
it('should throw end by error', async () => {
const stream = fs.createReadStream(path.join(__dirname, 'fixtures/bigfile'));
setImmediate(() => {
stream.emit('error', new Error('mock error'));
});
try {
await streamToBuffer(stream);
throw new Error('should not execute');
} catch (err) {
assert(err.message === 'mock error');
}
});
});