1 | const Readable = require('stream').Readable
|
2 | const Writable = require('stream').Writable
|
3 | const assert = require('assert')
|
4 | const Catchment = require('catchment')
|
5 | const write = require('../../src/write')
|
6 |
|
7 | function createWs(nextArg) {
|
8 | const allData = []
|
9 | const allRawData = []
|
10 | const ws = new Writable({
|
11 | write: (chunk, encoding, next) => {
|
12 | allData.push(String(chunk))
|
13 | allRawData.push(chunk)
|
14 | next(nextArg)
|
15 | },
|
16 | })
|
17 | return { ws, allData, allRawData }
|
18 | }
|
19 |
|
20 | const writeTestSuite = {
|
21 | 'should write a string to the stream': () => {
|
22 | const testString = 'hello world'
|
23 | const ws = createWs()
|
24 | return write(ws.ws, testString)
|
25 | .then(() => {
|
26 | assert.deepEqual(ws.allData, [
|
27 | testString,
|
28 | ])
|
29 | assert(!ws.writable)
|
30 | })
|
31 | },
|
32 | 'should pipe a readable to the stream': () => {
|
33 | const testString = 'hello world'
|
34 | const ws = createWs()
|
35 | const rs = new Readable({
|
36 | read: () => {
|
37 | rs.push(testString)
|
38 | rs.push(null)
|
39 | },
|
40 | })
|
41 | return write(ws.ws, rs)
|
42 | .then((res) => {
|
43 | assert.deepEqual(ws.allData, [
|
44 | testString,
|
45 | ])
|
46 | assert(res._writableState.ended)
|
47 | assert.strictEqual(res, ws.ws)
|
48 | })
|
49 | },
|
50 | 'should reject when reabable is not readable': () => {
|
51 | const testString = 'hello world'
|
52 | const ws = createWs()
|
53 | const rs = new Readable({
|
54 | read: () => {
|
55 | rs.push(testString)
|
56 | rs.push(null)
|
57 | },
|
58 | })
|
59 | const catchment = new Catchment()
|
60 | rs.pipe(catchment)
|
61 | return catchment.promise
|
62 | .then(() => {
|
63 | return write(ws.ws, rs)
|
64 | })
|
65 | .then(() => {
|
66 | throw new Error('Should have been rejected')
|
67 | }, (err) => {
|
68 | if (!/Stream is not readable/.test(err.message)) {
|
69 | throw err
|
70 | }
|
71 | })
|
72 | },
|
73 | 'should reject with an error when readable throws': () => {
|
74 | const ws = createWs()
|
75 | const error = new Error('test-error')
|
76 | const rs = new Readable({
|
77 | read() {
|
78 | process.nextTick(() => this.emit('error', error))
|
79 | return
|
80 | },
|
81 | })
|
82 | return write(ws.ws, rs)
|
83 | .then(() => {
|
84 | throw new Error('Should have been rejected')
|
85 | }, (err) => {
|
86 | assert.strictEqual(err, error)
|
87 | })
|
88 | },
|
89 | 'should reject when writable throws': () => {
|
90 | const testString = 'hello world'
|
91 | const error = new Error('test-error')
|
92 | const ws = createWs(error)
|
93 | const rs = new Readable({
|
94 | read: () => {
|
95 | rs.push(testString)
|
96 | rs.push(null)
|
97 | },
|
98 | })
|
99 | return write(ws.ws, rs)
|
100 | .then(() => {
|
101 | throw new Error('Should have been rejected')
|
102 | }, (err) => {
|
103 | assert.strictEqual(err, error)
|
104 | })
|
105 | },
|
106 | 'should write nothing when null given': () => {
|
107 | const ws = createWs()
|
108 | return write(ws.ws, null)
|
109 | .then(() => {
|
110 | assert.deepEqual(ws.allData, [])
|
111 | assert(!ws.writable)
|
112 | })
|
113 | },
|
114 | 'should write buffer': () => {
|
115 | const testString = 'hello world'
|
116 | const buffer = Buffer.from(testString)
|
117 | const ws = createWs()
|
118 | return write(ws.ws, buffer)
|
119 | .then(() => {
|
120 | assert.deepEqual(ws.allRawData, [
|
121 | buffer,
|
122 | ])
|
123 | assert(!ws.writable)
|
124 | })
|
125 | },
|
126 | 'should reject if writable is not Writable': () => {
|
127 | return write('string')
|
128 | .then(() => {
|
129 | throw new Error('Should have been rejected')
|
130 | }, (err) => {
|
131 | if (!/Writable stream expected/.test(err.message)) {
|
132 | throw err
|
133 | }
|
134 | })
|
135 | },
|
136 | }
|
137 |
|
138 | module.exports = writeTestSuite
|