Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 16a006f

Browse files
committed
Add ipfs.files.get tests.
1 parent d8e4819 commit 16a006f

File tree

2 files changed

+100
-19
lines changed

2 files changed

+100
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
},
2929
"homepage": "https://github.com/ipfs/interface-ipfs-core#readme",
3030
"dependencies": {
31-
"bl": "^1.1.2",
3231
"bs58": "^3.0.0",
3332
"chai": "^3.5.0",
33+
"concat-stream": "^1.5.1",
3434
"detect-node": "^2.0.3",
3535
"ipfs-merkle-dag": "^0.6.0",
3636
"readable-stream": "1.1.13"

src/files.js

Lines changed: 99 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const Readable = require('readable-stream')
77
const path = require('path')
88
const fs = require('fs')
99
const isNode = require('detect-node')
10-
const bl = require('bl')
10+
const concat = require('concat-stream')
1111

1212
module.exports = (common) => {
13-
describe('.files', () => {
13+
describe.only('.files', () => {
1414
let smallFile
1515
let bigFile
1616
let ipfs
@@ -200,18 +200,6 @@ module.exports = (common) => {
200200
})
201201
})
202202

203-
it('with a multihash', (done) => {
204-
const mhBuf = new Buffer(bs58.decode('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'))
205-
ipfs.cat(mhBuf, (err, stream) => {
206-
expect(err).to.not.exist
207-
stream.pipe(bl((err, data) => {
208-
expect(err).to.not.exist
209-
expect(data.toString()).to.contain('Check out some of the other files in this directory:')
210-
done()
211-
}))
212-
})
213-
})
214-
215203
it('streams a large file', (done) => {
216204
const hash = 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq'
217205
ipfs.cat(hash, (err, stream) => {
@@ -222,6 +210,15 @@ module.exports = (common) => {
222210
done()
223211
}))
224212
})
213+
214+
it('with a multihash', (done) => {
215+
const mhBuf = new Buffer(bs58.decode('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'))
216+
ipfs.cat(mhBuf, (err, stream) => {
217+
expect(err).to.not.exist
218+
stream.pipe(concat((data) => {
219+
expect(data.toString()).to.contain('Check out some of the other files in this directory:')
220+
done()
221+
}))
225222
})
226223
})
227224
})
@@ -249,8 +246,7 @@ module.exports = (common) => {
249246
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
250247
ipfs.cat(hash)
251248
.then((stream) => {
252-
stream.pipe(bl((err, data) => {
253-
expect(err).to.not.exist
249+
stream.pipe(concat((data) => {
254250
expect(data.toString()).to.contain('Check out some of the other files in this directory:')
255251
done()
256252
}))
@@ -281,8 +277,7 @@ module.exports = (common) => {
281277
const hash = new Buffer(bs58.decode('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'))
282278
ipfs.cat(hash)
283279
.then((stream) => {
284-
stream.pipe(bl((err, bldata) => {
285-
expect(err).to.not.exist
280+
stream.pipe(concat((bldata) => {
286281
expect(bldata.toString()).to.contain('Check out some of the other files in this directory:')
287282
done()
288283
}))
@@ -293,5 +288,91 @@ module.exports = (common) => {
293288
})
294289
})
295290
})
291+
292+
describe('.get', () => {
293+
it('with a base58 encoded multihash', (done) => {
294+
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
295+
ipfs.files.get(hash, (err, stream) => {
296+
expect(err).to.not.exist
297+
stream.pipe(concat((files) => {
298+
expect(err).to.not.exist
299+
expect(files).to.be.length(1)
300+
expect(files[0].path).to.equal(hash)
301+
files[0].content.pipe(concat((content) => {
302+
expect(content.toString()).to.contain('Check out some of the other files in this directory:')
303+
done()
304+
}))
305+
}))
306+
})
307+
})
308+
309+
it('with a multihash', (done) => {
310+
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
311+
const mhBuf = new Buffer(bs58.decode(hash))
312+
ipfs.files.get(mhBuf, (err, stream) => {
313+
expect(err).to.not.exist
314+
stream.pipe(concat((files) => {
315+
expect(files).to.be.length(1)
316+
expect(files[0].path).to.deep.equal(mhBuf)
317+
files[0].content.pipe(concat((content) => {
318+
expect(content.toString()).to.contain('Check out some of the other files in this directory:')
319+
done()
320+
}))
321+
}))
322+
})
323+
})
324+
325+
it('large file', (done) => {
326+
const hash = 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq'
327+
ipfs.files.get(hash, (err, stream) => {
328+
expect(err).to.not.exist
329+
stream.pipe(concat((files) => {
330+
expect(files).to.be.length(1)
331+
expect(files[0].path).to.equal(hash)
332+
files[0].content.pipe(concat((content) => {
333+
expect(content).to.deep.equal(bigFile)
334+
done()
335+
}))
336+
}))
337+
})
338+
})
339+
340+
describe('promise', () => {
341+
it('with a base58 encoded string', (done) => {
342+
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
343+
ipfs.files.get(hash)
344+
.then((stream) => {
345+
stream.pipe(concat((files) => {
346+
expect(files).to.be.length(1)
347+
expect(files[0].path).to.equal(hash)
348+
files[0].content.pipe(concat((content) => {
349+
expect(content.toString()).to.contain('Check out some of the other files in this directory:')
350+
done()
351+
}))
352+
}))
353+
})
354+
.catch((err) => {
355+
expect(err).to.not.exist
356+
})
357+
})
358+
359+
it('errors on invalid key', (done) => {
360+
const hash = 'somethingNotMultihash'
361+
ipfs.files.get(hash)
362+
.then((stream) => {})
363+
.catch((err) => {
364+
expect(err).to.exist
365+
const errString = err.toString()
366+
if (errString === 'Error: invalid ipfs ref path') {
367+
expect(err.toString()).to.contain('Error: invalid ipfs ref path')
368+
}
369+
if (errString === 'Error: Invalid Key') {
370+
expect(err.toString()).to.contain('Error: Invalid Key')
371+
}
372+
done()
373+
})
374+
})
375+
})
376+
})
296377
})
297378
}

0 commit comments

Comments
 (0)