|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const expect = require('chai').expect |
| 5 | +const bl = require('bl') |
| 6 | +const bs58 = require('bs58') |
| 7 | +const Readable = require('readable-stream') |
| 8 | + |
| 9 | +module.exports = (common) => { |
| 10 | + describe('.file', () => { |
| 11 | + let ipfs |
| 12 | + |
| 13 | + before((done) => { |
| 14 | + common.setup((err, _ipfs) => { |
| 15 | + expect(err).to.not.exist |
| 16 | + ipfs = _ipfs |
| 17 | + done() |
| 18 | + }) |
| 19 | + }) |
| 20 | + |
| 21 | + after((done) => { |
| 22 | + common.teardown(done) |
| 23 | + }) |
| 24 | + |
| 25 | + // go-ipfs http and js-ipfs core have different responses |
| 26 | + describe('.add', () => { |
| 27 | + it('add', (done) => { |
| 28 | + const buffered = new Buffer('some data') |
| 29 | + const rs = new Readable() |
| 30 | + rs.push(buffered) |
| 31 | + rs.push(null) |
| 32 | + const arr = [] |
| 33 | + const filePair = {path: 'data.txt', content: rs} |
| 34 | + arr.push(filePair) |
| 35 | + ipfs.add(arr, (err, res) => { |
| 36 | + expect(err).to.not.exist |
| 37 | + expect(res[0].path).to.equal('data.txt') |
| 38 | + expect(res[0].size).to.equal(17) |
| 39 | + expect(bs58.encode(res[0].multihash).toString()).to.equal('QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS') |
| 40 | + done() |
| 41 | + }) |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + describe('.cat', () => { |
| 46 | + it('returns file stream', (done) => { |
| 47 | + const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB' |
| 48 | + ipfs.cat(hash, (err, file) => { |
| 49 | + expect(err).to.not.exist |
| 50 | + file.pipe(bl((err, bldata) => { |
| 51 | + expect(err).to.not.exist |
| 52 | + expect(bldata.toString()).to.contain('Check out some of the other files in this directory:') |
| 53 | + done() |
| 54 | + })) |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + // This fails on js-ipfs-api |
| 59 | + it.skip('takes a buffer input', (done) => { |
| 60 | + const mhBuf = new Buffer(bs58.decode('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB')) |
| 61 | + ipfs.cat(mhBuf, (err, file) => { |
| 62 | + expect(err).to.not.exist |
| 63 | + file.pipe(bl((err, bldata) => { |
| 64 | + expect(err).to.not.exist |
| 65 | + expect(bldata.toString()).to.contain('Check out some of the other files in this directory:') |
| 66 | + done() |
| 67 | + })) |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + // You can add a large file to your ipfs repo and change the hash to the file after installing js-ipfs |
| 72 | + it.skip('returns a large file', (done) => { |
| 73 | + const hash = '' |
| 74 | + ipfs.cat(hash, (err, file) => { |
| 75 | + expect(err).to.not.exist |
| 76 | + file.pipe(bl((err, bldata) => { |
| 77 | + expect(err).to.not.exist |
| 78 | + done() |
| 79 | + })) |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + it('returns error on invalid key', (done) => { |
| 84 | + const hash = 'somethingNotMultihash' |
| 85 | + ipfs.cat(hash, (err, file) => { |
| 86 | + expect(err).to.exist |
| 87 | + const errString = err.toString() |
| 88 | + if (errString === 'Error: invalid ipfs ref path') { |
| 89 | + expect(err.toString()).to.contain('Error: invalid ipfs ref path') |
| 90 | + } |
| 91 | + if (errString === 'Error: Invalid Key') { |
| 92 | + expect(err.toString()).to.contain('Error: Invalid Key') |
| 93 | + } |
| 94 | + done() |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + describe('promise', () => { |
| 99 | + it('files.cat', (done) => { |
| 100 | + const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB' |
| 101 | + ipfs.cat(hash) |
| 102 | + .then((stream) => { |
| 103 | + stream.pipe(bl((err, bldata) => { |
| 104 | + expect(err).to.not.exist |
| 105 | + expect(bldata.toString()).to.contain('Check out some of the other files in this directory:') |
| 106 | + done() |
| 107 | + })) |
| 108 | + }) |
| 109 | + .catch((err) => { |
| 110 | + expect(err).to.not.exist |
| 111 | + }) |
| 112 | + }) |
| 113 | + |
| 114 | + it('returns error on invalid key', (done) => { |
| 115 | + const hash = 'somethingNotMultihash' |
| 116 | + ipfs.cat(hash) |
| 117 | + .then((stream) => {}) |
| 118 | + .catch((err) => { |
| 119 | + expect(err).to.exist |
| 120 | + const errString = err.toString() |
| 121 | + if (errString === 'Error: invalid ipfs ref path') { |
| 122 | + expect(err.toString()).to.contain('Error: invalid ipfs ref path') |
| 123 | + } |
| 124 | + if (errString === 'Error: Invalid Key') { |
| 125 | + expect(err.toString()).to.contain('Error: Invalid Key') |
| 126 | + } |
| 127 | + done() |
| 128 | + }) |
| 129 | + }) |
| 130 | + }) |
| 131 | + }) |
| 132 | + }) |
| 133 | +} |
0 commit comments