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

Added in a test for the opts parameter. #506

Merged
merged 7 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions SPEC/OBJECT.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ A great source of [examples][] can be found in the tests for this API.

`options` is a optional argument of type object, that can contain the following properties:

- `enc`, the encoding of multihash (base58, base64, etc), if any.
- `enc`, The encoding type the output should beencoded with (json, xml, or text), if any.
- `timeout`, A timeout to pass to the IPFS daemon so the request expires after a certain amount of time.

`callback` must follow `function (err, stats) {}` signature, where `err` is an error if the operation was not successful and `stats` is an Object with following format:

Expand All @@ -220,7 +221,7 @@ If no `callback` is passed, a [promise][] is returned.
```JavaScript
const multihash = 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD'

ipfs.object.stat(multihash, (err, stats) => {
ipfs.object.stat(multihash, {timeout: '10s'}, (err, stats) => {
if (err) {
throw err
}
Expand Down
30 changes: 27 additions & 3 deletions src/object/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = (createCommon, options) => {
}

await ipfs.object.put(testObj)
const stats = await ipfs.object.stat('QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', { enc: 'base58' })
const stats = await ipfs.object.stat('QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ')

const expected = {
Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ',
Expand All @@ -80,6 +80,30 @@ module.exports = (createCommon, options) => {

expect(expected).to.deep.equal(stats)
})

it('should pass on the opts', (done) => {
const testObj = {
Data: Buffer.from('get test object'),
Links: []
}

ipfs.object.put(testObj, (err, cid) => {
expect(err).to.not.exist()
const timeout = 2;
const startTime = new Date();
const badCid = 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ';

//we can test that we are passing in opts by testing the timeout option for a CID that doesn't exist
ipfs.object.stat(badCid, {timeout: `${timeout}s`}, (err, stats) => {
let timeForRequest = (new Date () - startTime)/1000;
console.log(err);
expect(err.message).to.equal("failed to get block for QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ: context deadline exceeded");
expect(stats).to.not.exist()
expect(timeForRequest).to.not.lessThan(timeout);
done()
})
})
})

it('should get stats for object with links by multihash', (done) => {
let node1a
Expand Down Expand Up @@ -151,7 +175,7 @@ module.exports = (createCommon, options) => {
ipfs.object.put(testObj, (err, cid) => {
expect(err).to.not.exist()

ipfs.object.stat(cid.buffer, { enc: 'base58' }, (err, stats) => {
ipfs.object.stat(cid.buffer, (err, stats) => {
expect(err).to.not.exist()
const expected = {
Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ',
Expand All @@ -176,7 +200,7 @@ module.exports = (createCommon, options) => {
ipfs.object.put(testObj, (err, cid) => {
expect(err).to.not.exist()

ipfs.object.stat(cid.toBaseEncodedString(), { enc: 'base58' }, (err, stats) => {
ipfs.object.stat(cid.toBaseEncodedString(), (err, stats) => {
expect(err).to.not.exist()
const expected = {
Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ',
Expand Down