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

fix(get): update tests to reflect the tar-stream behaviour #88

Merged
merged 1 commit into from
Nov 8, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 54 additions & 35 deletions src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,21 @@ module.exports = (common) => {
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
ipfs.files.get(hash, (err, stream) => {
expect(err).to.not.exist
stream.pipe(concat((files) => {
expect(err).to.not.exist
expect(files).to.be.length(1)
expect(files[0].path).to.equal(hash)
files[0].content.pipe(concat((content) => {
expect(content.toString()).to.contain('Plz add me!')
done()

let files = []
stream.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].path).to.be.eql(hash)
expect(files[0].content.toString()).to.contain('Plz add me!')
done()
}))
})
})
Expand All @@ -316,13 +323,21 @@ module.exports = (common) => {
const mhBuf = new Buffer(bs58.decode(hash))
ipfs.files.get(mhBuf, (err, stream) => {
expect(err).to.not.exist
stream.pipe(concat((files) => {
expect(files).to.be.length(1)
expect(files[0].path).to.deep.equal(hash)
files[0].content.pipe(concat((content) => {
expect(content.toString()).to.contain('Plz add me!')
done()

let files = []
stream.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].path).to.be.eql(hash)
expect(files[0].content.toString()).to.contain('Plz add me!')
done()
}))
})
})
Expand Down Expand Up @@ -412,37 +427,41 @@ module.exports = (common) => {
})

describe('promise', () => {
it('with a base58 encoded string', (done) => {
it('with a base58 encoded string', () => {
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
ipfs.files.get(hash)
.then((stream) => {
stream.pipe(concat((files) => {
return ipfs.files.get(hash).then((stream) => {
let files = []
return new Promise((resolve, reject) => {
stream.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].path).to.equal(hash)
files[0].content.pipe(concat((content) => {
expect(content.toString()).to.contain('Plz add me!')
done()
}))
expect(files[0].content.toString()).to.contain('Plz add me!')
resolve()
}))
})
.catch((err) => {
expect(err).to.not.exist
})
})
})

it('errors on invalid key', () => {
const hash = 'somethingNotMultihash'
return ipfs.files.get(hash)
.catch((err) => {
expect(err).to.exist
const errString = err.toString()
if (errString === 'Error: invalid ipfs ref path') {
expect(err.toString()).to.contain('Error: invalid ipfs ref path')
}
if (errString === 'Error: Invalid Key') {
expect(err.toString()).to.contain('Error: Invalid Key')
}
})
return ipfs.files.get(hash).catch((err) => {
expect(err).to.exist
const errString = err.toString()
if (errString === 'Error: invalid ipfs ref path') {
expect(err.toString()).to.contain('Error: invalid ipfs ref path')
}
if (errString === 'Error: Invalid Key') {
expect(err.toString()).to.contain('Error: Invalid Key')
}
})
})
})
})
Expand Down