This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathget.spec.js
183 lines (157 loc) · 4.95 KB
/
get.spec.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const isNode = require('detect-node')
const fs = require('fs')
const concat = require('concat-stream')
const through = require('through2')
const streamEqual = require('stream-equal')
const path = require('path')
const loadFixture = require('aegir/fixtures')
const FactoryClient = require('./ipfs-factory/client')
const testfile = loadFixture(__dirname, '/fixtures/testfile.txt')
let testfileBig
let tfbPath
if (isNode) {
tfbPath = path.join(__dirname, '/fixtures/15mb.random')
testfileBig = fs.createReadStream(tfbPath, { bufferSize: 128 })
}
describe('.get', () => {
let ipfs
let fc
before(function (done) {
this.timeout(20 * 1000) // slow CI
fc = new FactoryClient()
fc.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})
after((done) => fc.dismantle(done))
describe('Callback API', () => {
it('add file for testing', (done) => {
const expectedMultihash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
ipfs.files.add(testfile, (err, res) => {
expect(err).to.not.exist()
expect(res).to.have.length(1)
expect(res[0].hash).to.equal(expectedMultihash)
expect(res[0].path).to.equal(expectedMultihash)
done()
})
})
it('get with no compression args', (done) => {
ipfs.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => {
expect(err).to.not.exist()
// accumulate the files and their content
var files = []
res.pipe(through.obj((file, enc, next) => {
file.content.pipe(concat((content) => {
files.push({
path: file.path,
content: content
})
next()
}))
}, () => {
expect(files).to.be.length(1)
expect(files[0].content.toString()).to.contain(testfile.toString())
done()
}))
})
})
it('get with archive true', (done) => {
ipfs.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {
archive: true
}, (err, res) => {
expect(err).to.not.exist()
// accumulate the files and their content
var files = []
res.pipe(through.obj((file, enc, next) => {
file.content.pipe(concat((content) => {
files.push({
path: file.path,
content: content
})
next()
}))
}, () => {
expect(files).to.be.length(1)
expect(files[0].content.toString()).to.contain(testfile.toString())
done()
}))
})
})
it('get err with out of range compression level', (done) => {
ipfs.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {
compress: true,
'compression-level': 10
}, (err, res) => {
expect(err).to.exist()
expect(err.toString()).to.equal('Error: Compression level must be between 1 and 9')
done()
})
})
it('get with compression level', (done) => {
ipfs.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {
compress: true,
'compression-level': 1
}, (err, res) => {
expect(err).to.not.exist()
done()
})
})
it('add a BIG file (for testing get)', (done) => {
if (!isNode) { return done() }
const bigFile = fs.readFileSync(tfbPath)
const expectedMultihash = 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq'
ipfs.files.add(bigFile, (err, res) => {
expect(err).to.not.exist()
expect(res).to.have.length(1)
expect(res[0].path).to.equal(expectedMultihash)
expect(res[0].hash).to.equal(expectedMultihash)
done()
})
})
it('get BIG file', (done) => {
if (!isNode) { return done() }
ipfs.get('Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq', (err, files) => {
expect(err).to.not.exist()
files.on('data', (file) => {
// Do not blow out the memory of nodejs :)
streamEqual(file.content, testfileBig, (err, equal) => {
expect(err).to.not.exist()
expect(equal).to.equal(true)
done()
})
})
})
})
})
describe('Promise API', () => {
it('get', (done) => {
ipfs.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
.then((files) => {
files.on('data', (file) => {
let buf = ''
file.content
.on('error', (err) => expect(err).to.not.exist())
.on('data', (data) => {
buf += data.toString()
})
.on('end', () => {
expect(buf).to.contain(testfile.toString())
done()
})
})
})
.catch((err) => {
expect(err).to.not.exist()
})
})
})
})