From b8171b1629399cb8024094993108e49ac3d8be32 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 28 May 2018 05:49:12 +0100 Subject: [PATCH] feat: ipfs.ping cli, http-api and core (#1342) --- circle.yml | 5 +- package.json | 10 +- src/cli/commands/ping.js | 35 +++ src/core/components/index.js | 2 + src/core/components/ping-pull-stream.js | 104 ++++++++ src/core/components/ping-readable-stream.js | 7 + src/core/components/ping.js | 8 +- src/core/index.js | 2 + src/http/api/resources/index.js | 1 + src/http/api/resources/ping.js | 48 ++++ src/http/api/routes/index.js | 1 + src/http/api/routes/ping.js | 16 ++ test/cli/commands.js | 3 +- test/cli/ping.js | 156 ++++++++++++ test/core/interface/interface.spec.js | 1 + test/core/interface/ping.js | 35 +++ test/core/ping.spec.js | 252 ++++++++++++++++++++ test/http-api/inject/ping.js | 52 ++++ 18 files changed, 726 insertions(+), 12 deletions(-) create mode 100644 src/cli/commands/ping.js create mode 100644 src/core/components/ping-pull-stream.js create mode 100644 src/core/components/ping-readable-stream.js create mode 100644 src/http/api/resources/ping.js create mode 100644 src/http/api/routes/ping.js create mode 100644 test/cli/ping.js create mode 100644 test/core/interface/ping.js create mode 100644 test/core/ping.spec.js create mode 100644 test/http-api/inject/ping.js diff --git a/circle.yml b/circle.yml index 55886db205..0c02b8f7d6 100644 --- a/circle.yml +++ b/circle.yml @@ -1,13 +1,10 @@ # Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories. machine: node: - version: 8 + version: 8.11.1 test: - pre: - - npm run lint post: - - make test - npm run coverage -- --upload --providers coveralls dependencies: diff --git a/package.json b/package.json index b9658e3f22..d98dabb3fd 100644 --- a/package.json +++ b/package.json @@ -72,9 +72,9 @@ "execa": "~0.10.0", "expose-loader": "~0.7.5", "form-data": "^2.3.2", - "hat": "~0.0.3", - "interface-ipfs-core": "~0.65.7", - "ipfsd-ctl": "~0.34.0", + "hat": "0.0.3", + "interface-ipfs-core": "~0.66.2", + "ipfsd-ctl": "~0.37.0", "lodash": "^4.17.10", "mocha": "^5.1.1", "ncp": "^2.0.0", @@ -105,7 +105,7 @@ "hapi-set-header": "^1.0.2", "hoek": "^5.0.3", "human-to-milliseconds": "^1.0.0", - "ipfs-api": "^21.0.0", + "ipfs-api": "^22.0.0", "ipfs-bitswap": "~0.20.0", "ipfs-block": "~0.7.1", "ipfs-block-service": "~0.14.0", @@ -148,7 +148,7 @@ "multihashes": "~0.4.13", "once": "^1.4.0", "path-exists": "^3.0.0", - "peer-book": "~0.7.0", + "peer-book": "~0.8.0", "peer-id": "~0.10.7", "peer-info": "~0.14.1", "progress": "^2.0.0", diff --git a/src/cli/commands/ping.js b/src/cli/commands/ping.js new file mode 100644 index 0000000000..3760b33b75 --- /dev/null +++ b/src/cli/commands/ping.js @@ -0,0 +1,35 @@ +'use strict' + +const pull = require('pull-stream') +const print = require('../utils').print + +module.exports = { + command: 'ping ', + + description: 'Measure the latency of a connection', + + builder: { + count: { + alias: 'n', + type: 'integer', + default: 10 + } + }, + + handler (argv) { + const peerId = argv.peerId + const count = argv.count || 10 + pull( + argv.ipfs.pingPullStream(peerId, { count }), + pull.drain(({ success, time, text }) => { + // Check if it's a pong + if (success && !text) { + print(`Pong received: time=${time} ms`) + // Status response + } else { + print(text) + } + }) + ) + } +} diff --git a/src/core/components/index.js b/src/core/components/index.js index 4f4e410e43..ce95b27a53 100644 --- a/src/core/components/index.js +++ b/src/core/components/index.js @@ -16,6 +16,8 @@ exports.dag = require('./dag') exports.libp2p = require('./libp2p') exports.swarm = require('./swarm') exports.ping = require('./ping') +exports.pingPullStream = require('./ping-pull-stream') +exports.pingReadableStream = require('./ping-readable-stream') exports.files = require('./files') exports.bitswap = require('./bitswap') exports.pubsub = require('./pubsub') diff --git a/src/core/components/ping-pull-stream.js b/src/core/components/ping-pull-stream.js new file mode 100644 index 0000000000..c4a2acfeae --- /dev/null +++ b/src/core/components/ping-pull-stream.js @@ -0,0 +1,104 @@ +'use strict' + +const debug = require('debug') +const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR +const PeerId = require('peer-id') +const pull = require('pull-stream') +const Pushable = require('pull-pushable') +const waterfall = require('async/waterfall') + +const log = debug('jsipfs:pingPullStream') +log.error = debug('jsipfs:pingPullStream:error') + +module.exports = function pingPullStream (self) { + return (peerId, opts) => { + if (!self.isOnline()) { + return pull.error(new Error(OFFLINE_ERROR)) + } + + opts = Object.assign({ count: 10 }, opts) + + const source = Pushable() + + waterfall([ + (cb) => getPeer(self._libp2pNode, source, peerId, cb), + (peer, cb) => runPing(self._libp2pNode, source, opts.count, peer, cb) + ], (err) => { + if (err) { + log.error(err) + source.push(getPacket({ success: false, text: err.toString() })) + source.end(err) + } + }) + + return source + } +} + +function getPacket (msg) { + // Default msg + const basePacket = { success: true, time: 0, text: '' } + return Object.assign(basePacket, msg) +} + +function getPeer (libp2pNode, statusStream, peerId, cb) { + let peer + + try { + peer = libp2pNode.peerBook.get(peerId) + } catch (err) { + log('Peer not found in peer book, trying peer routing') + // Share lookup status just as in the go implemmentation + statusStream.push(getPacket({ text: `Looking up peer ${peerId}` })) + + // Try to use peerRouting + try { + peerId = PeerId.createFromB58String(peerId) + } catch (err) { + return cb(Object.assign(err, { + message: `failed to parse peer address '${peerId}': input isn't valid multihash` + })) + } + + return libp2pNode.peerRouting.findPeer(peerId, cb) + } + + cb(null, peer) +} + +function runPing (libp2pNode, statusStream, count, peer, cb) { + libp2pNode.ping(peer, (err, p) => { + if (err) { + return cb(err) + } + + log('Got peer', peer) + + let packetCount = 0 + let totalTime = 0 + statusStream.push(getPacket({ text: `PING ${peer.id.toB58String()}` })) + + p.on('ping', (time) => { + statusStream.push(getPacket({ time: time })) + totalTime += time + packetCount++ + if (packetCount >= count) { + const average = totalTime / count + p.stop() + statusStream.push(getPacket({ text: `Average latency: ${average}ms` })) + statusStream.end() + } + }) + + p.on('error', (err) => { + log.error(err) + p.stop() + statusStream.push(getPacket({ success: false, text: err.toString() })) + statusStream.end(err) + }) + + p.start() + + return cb() + }) +} diff --git a/src/core/components/ping-readable-stream.js b/src/core/components/ping-readable-stream.js new file mode 100644 index 0000000000..b6809ffb48 --- /dev/null +++ b/src/core/components/ping-readable-stream.js @@ -0,0 +1,7 @@ +'use strict' + +const toStream = require('pull-stream-to-stream') + +module.exports = function pingReadableStream (self) { + return (peerId, opts) => toStream.source(self.pingPullStream(peerId, opts)) +} diff --git a/src/core/components/ping.js b/src/core/components/ping.js index eb7a45b34f..c5bfba9134 100644 --- a/src/core/components/ping.js +++ b/src/core/components/ping.js @@ -1,9 +1,13 @@ 'use strict' const promisify = require('promisify-es6') +const pull = require('pull-stream/pull') module.exports = function ping (self) { - return promisify((callback) => { - callback(new Error('Not implemented')) + return promisify((peerId, opts, cb) => { + pull( + self.pingPullStream(peerId, opts), + pull.collect(cb) + ) }) } diff --git a/src/core/index.js b/src/core/index.js index 46f8c9bb2c..0b19160429 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -102,6 +102,8 @@ class IPFS extends EventEmitter { this.files = components.files(this) this.bitswap = components.bitswap(this) this.ping = components.ping(this) + this.pingPullStream = components.pingPullStream(this) + this.pingReadableStream = components.pingReadableStream(this) this.pubsub = components.pubsub(this) this.dht = components.dht(this) this.dns = components.dns(this) diff --git a/src/http/api/resources/index.js b/src/http/api/resources/index.js index 08d8d7f2a1..37f38f246b 100644 --- a/src/http/api/resources/index.js +++ b/src/http/api/resources/index.js @@ -3,6 +3,7 @@ exports.version = require('./version') exports.shutdown = require('./shutdown') exports.id = require('./id') +exports.ping = require('./ping') exports.bootstrap = require('./bootstrap') exports.repo = require('./repo') exports.object = require('./object') diff --git a/src/http/api/resources/ping.js b/src/http/api/resources/ping.js new file mode 100644 index 0000000000..56824db629 --- /dev/null +++ b/src/http/api/resources/ping.js @@ -0,0 +1,48 @@ +'use strict' + +const Joi = require('joi') +const pull = require('pull-stream') +const toStream = require('pull-stream-to-stream') +const ndjson = require('pull-ndjson') +const PassThrough = require('readable-stream').PassThrough +const pump = require('pump') + +exports.get = { + validate: { + query: Joi.object().keys({ + n: Joi.alternatives() + .when('count', { + is: Joi.any().exist(), + then: Joi.any().forbidden(), + otherwise: Joi.number().integer().greater(0) + }), + count: Joi.number().integer().greater(0), + arg: Joi.string().required() + }).unknown() + }, + handler: (request, reply) => { + const ipfs = request.server.app.ipfs + const peerId = request.query.arg + // Default count to 10 + const count = request.query.n || request.query.count || 10 + + const source = pull( + ipfs.pingPullStream(peerId, { count: count }), + pull.map((chunk) => ({ + Success: chunk.success, + Time: chunk.time, + Text: chunk.text + })), + ndjson.serialize() + ) + + // Streams from pull-stream-to-stream don't seem to be compatible + // with the stream2 readable interface + // see: https://github.com/hapijs/hapi/blob/c23070a3de1b328876d5e64e679a147fafb04b38/lib/response.js#L533 + // and: https://github.com/pull-stream/pull-stream-to-stream/blob/e436acee18b71af8e71d1b5d32eee642351517c7/index.js#L28 + const responseStream = toStream.source(source) + const stream2 = new PassThrough() + pump(responseStream, stream2) + return reply(stream2).type('application/json').header('X-Chunked-Output', '1') + } +} diff --git a/src/http/api/routes/index.js b/src/http/api/routes/index.js index 2eeb4b137a..d7c30851f7 100644 --- a/src/http/api/routes/index.js +++ b/src/http/api/routes/index.js @@ -9,6 +9,7 @@ module.exports = (server) => { require('./object')(server) require('./repo')(server) require('./config')(server) + require('./ping')(server) require('./swarm')(server) require('./bitswap')(server) require('./file')(server) diff --git a/src/http/api/routes/ping.js b/src/http/api/routes/ping.js new file mode 100644 index 0000000000..92b081862f --- /dev/null +++ b/src/http/api/routes/ping.js @@ -0,0 +1,16 @@ +'use strict' + +const resources = require('./../resources') + +module.exports = (server) => { + const api = server.select('API') + + api.route({ + method: '*', + path: '/api/v0/ping', + config: { + handler: resources.ping.get.handler, + validate: resources.ping.get.validate + } + }) +} diff --git a/test/cli/commands.js b/test/cli/commands.js index 06154cfac6..1e194eb143 100644 --- a/test/cli/commands.js +++ b/test/cli/commands.js @@ -4,7 +4,8 @@ const expect = require('chai').expect const runOnAndOff = require('../utils/on-and-off') -const commandCount = 73 +const commandCount = 74 + describe('commands', () => runOnAndOff((thing) => { let ipfs diff --git a/test/cli/ping.js b/test/cli/ping.js new file mode 100644 index 0000000000..170a1fc3b9 --- /dev/null +++ b/test/cli/ping.js @@ -0,0 +1,156 @@ +/* eslint max-nested-callbacks: ["error", 8] */ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const series = require('async/series') +const DaemonFactory = require('ipfsd-ctl') +const ipfsExec = require('../utils/ipfs-exec') + +const df = DaemonFactory.create({ type: 'js' }) +const expect = chai.expect +chai.use(dirtyChai) + +const config = { + Bootstrap: [], + Discovery: { + MDNS: { + Enabled: + false + } + } +} + +describe('ping', function () { + this.timeout(60 * 1000) + let ipfsdA + let ipfsdB + let bMultiaddr + let ipfsdBId + let cli + + before((done) => { + this.timeout(60 * 1000) + series([ + (cb) => { + df.spawn({ + exec: `./src/cli/bin.js`, + config, + initOptions: { bits: 512 } + }, (err, _ipfsd) => { + expect(err).to.not.exist() + ipfsdB = _ipfsd + cb() + }) + }, + (cb) => { + ipfsdB.api.id((err, peerInfo) => { + expect(err).to.not.exist() + ipfsdBId = peerInfo.id + bMultiaddr = peerInfo.addresses[0] + cb() + }) + } + ], done) + }) + + before(function (done) { + this.timeout(60 * 1000) + + df.spawn({ + exec: './src/cli/bin.js', + config, + initoptions: { bits: 512 } + }, (err, _ipfsd) => { + expect(err).to.not.exist() + ipfsdA = _ipfsd + // Without DHT we need to have an already established connection + ipfsdA.api.swarm.connect(bMultiaddr, done) + }) + }) + + before((done) => { + this.timeout(60 * 1000) + cli = ipfsExec(ipfsdA.repoPath) + done() + }) + + after((done) => { + if (!ipfsdA) return done() + ipfsdA.stop(done) + }) + + after((done) => { + if (!ipfsdB) return done() + ipfsdB.stop(done) + }) + + it('ping host', (done) => { + this.timeout(60 * 1000) + const ping = cli(`ping ${ipfsdBId}`) + const result = [] + ping.stdout.on('data', (output) => { + const packets = output.toString().split('\n').slice(0, -1) + result.push(...packets) + }) + + ping.stdout.on('end', () => { + expect(result).to.have.lengthOf(12) + expect(result[0]).to.equal(`PING ${ipfsdBId}`) + for (let i = 1; i < 11; i++) { + expect(result[i]).to.match(/^Pong received: time=\d+ ms$/) + } + expect(result[11]).to.match(/^Average latency: \d+(.\d+)?ms$/) + done() + }) + + ping.catch((err) => { + expect(err).to.not.exist() + }) + }) + + it('ping host with --n option', (done) => { + this.timeout(60 * 1000) + const ping = cli(`ping --n 1 ${ipfsdBId}`) + const result = [] + ping.stdout.on('data', (output) => { + const packets = output.toString().split('\n').slice(0, -1) + result.push(...packets) + }) + + ping.stdout.on('end', () => { + expect(result).to.have.lengthOf(3) + expect(result[0]).to.equal(`PING ${ipfsdBId}`) + expect(result[1]).to.match(/^Pong received: time=\d+ ms$/) + expect(result[2]).to.match(/^Average latency: \d+(.\d+)?ms$/) + done() + }) + + ping.catch((err) => { + expect(err).to.not.exist() + }) + }) + + it('ping host with --count option', (done) => { + this.timeout(60 * 1000) + const ping = cli(`ping --count 1 ${ipfsdBId}`) + const result = [] + ping.stdout.on('data', (output) => { + const packets = output.toString().split('\n').slice(0, -1) + result.push(...packets) + }) + + ping.stdout.on('end', () => { + expect(result).to.have.lengthOf(3) + expect(result[0]).to.equal(`PING ${ipfsdBId}`) + expect(result[1]).to.match(/^Pong received: time=\d+ ms$/) + expect(result[2]).to.match(/^Average latency: \d+(.\d+)?ms$/) + done() + }) + + ping.catch((err) => { + expect(err).to.not.exist() + }) + }) +}) diff --git a/test/core/interface/interface.spec.js b/test/core/interface/interface.spec.js index 810b7f2198..356036adeb 100644 --- a/test/core/interface/interface.spec.js +++ b/test/core/interface/interface.spec.js @@ -16,6 +16,7 @@ describe('interface-ipfs-core tests', () => { require('./key') if (isNode) { require('./swarm') + require('./ping') require('./pubsub') require('./dht') } diff --git a/test/core/interface/ping.js b/test/core/interface/ping.js new file mode 100644 index 0000000000..cc0f85b9c1 --- /dev/null +++ b/test/core/interface/ping.js @@ -0,0 +1,35 @@ +/* eslint-env mocha */ +'use strict' + +const test = require('interface-ipfs-core') +const parallel = require('async/parallel') + +const IPFS = require('../../../src') + +const DaemonFactory = require('ipfsd-ctl') +const df = DaemonFactory.create({ type: 'proc', exec: IPFS }) + +const nodes = [] +const common = { + setup: function (callback) { + callback(null, { + spawnNode: (cb) => { + df.spawn({ + initOptions: { bits: 512 } + }, (err, _ipfsd) => { + if (err) { + return cb(err) + } + + nodes.push(_ipfsd) + cb(null, _ipfsd.api) + }) + } + }) + }, + teardown: function (callback) { + parallel(nodes.map((node) => (cb) => node.stop(cb)), callback) + } +} + +test.ping(common) diff --git a/test/core/ping.spec.js b/test/core/ping.spec.js new file mode 100644 index 0000000000..583ff6c964 --- /dev/null +++ b/test/core/ping.spec.js @@ -0,0 +1,252 @@ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const pull = require('pull-stream/pull') +const drain = require('pull-stream/sinks/drain') +const parallel = require('async/parallel') +const series = require('async/series') +const DaemonFactory = require('ipfsd-ctl') +const isNode = require('detect-node') + +const expect = chai.expect +chai.use(dirtyChai) +const df = DaemonFactory.create({ exec: 'src/cli/bin.js' }) + +const config = { + Bootstrap: [], + Discovery: { + MDNS: { + Enabled: + false + } + } +} + +function spawnNode ({ dht = false }, cb) { + const args = dht ? ['--enable-dht-experiment'] : [] + df.spawn({ + args, + config, + initOptions: { bits: 512 } + }, cb) +} + +// Determine if a ping response object is a pong, or something else, like a status message +function isPong (pingResponse) { + return Boolean(pingResponse && pingResponse.success && !pingResponse.text) +} + +describe('ping', function () { + this.timeout(60 * 1000) + + if (!isNode) return + + describe('DHT disabled', function () { + // Without DHT nodes need to be previously connected + let ipfsdA + let ipfsdB + let bMultiaddr + let ipfsdBId + + // Spawn nodes + before(function (done) { + this.timeout(60 * 1000) + + series([ + spawnNode.bind(null, { dht: false }), + spawnNode.bind(null, { dht: false }) + ], (err, ipfsd) => { + expect(err).to.not.exist() + ipfsdA = ipfsd[0] + ipfsdB = ipfsd[1] + done() + }) + }) + + // Get the peer info object + before(function (done) { + this.timeout(60 * 1000) + + ipfsdB.api.id((err, peerInfo) => { + expect(err).to.not.exist() + ipfsdBId = peerInfo.id + bMultiaddr = peerInfo.addresses[0] + done() + }) + }) + + // Connect the nodes + before(function (done) { + this.timeout(60 * 1000) + ipfsdA.api.swarm.connect(bMultiaddr, done) + }) + + after((done) => { + if (!ipfsdA) return done() + ipfsdA.stop(done) + }) + + after((done) => { + if (!ipfsdB) return done() + ipfsdB.stop(done) + }) + + it('sends the specified number of packets', (done) => { + let packetNum = 0 + const count = 3 + pull( + ipfsdA.api.pingPullStream(ipfsdBId, { count }), + drain((res) => { + expect(res.success).to.be.true() + // It's a pong + if (isPong(res)) { + packetNum++ + } + }, (err) => { + expect(err).to.not.exist() + expect(packetNum).to.equal(count) + done() + }) + ) + }) + + it('pinging an unknown peer will fail accordingly', (done) => { + const unknownPeerId = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn' + let messageNum = 0 + const count = 2 + pull( + ipfsdA.api.pingPullStream(unknownPeerId, { count }), + drain(({ success, time, text }) => { + messageNum++ + // Assert that the ping command falls back to the peerRouting + if (messageNum === 1) { + expect(text).to.include('Looking up') + } + + // Fails accordingly while trying to use peerRouting + if (messageNum === 2) { + expect(success).to.be.false() + } + }, (err) => { + expect(err).to.exist() + expect(err.message).to.include('DHT is not available') + expect(messageNum).to.equal(count) + done() + }) + ) + }) + }) + + describe('DHT enabled', function () { + // Our bootstrap process will run 3 IPFS daemons where + // A ----> B ----> C + // Allowing us to test the ping command using the DHT peer routing + let ipfsdA + let ipfsdB + let ipfsdC + let bMultiaddr + let cMultiaddr + let ipfsdCId + + // Spawn nodes + before(function (done) { + this.timeout(60 * 1000) + + series([ + spawnNode.bind(null, { dht: true }), + spawnNode.bind(null, { dht: true }), + spawnNode.bind(null, { dht: true }) + ], (err, ipfsd) => { + expect(err).to.not.exist() + ipfsdA = ipfsd[0] + ipfsdB = ipfsd[1] + ipfsdC = ipfsd[2] + done() + }) + }) + + // Get the peer info objects + before(function (done) { + this.timeout(60 * 1000) + + parallel([ + ipfsdB.api.id.bind(ipfsdB.api), + ipfsdC.api.id.bind(ipfsdC.api) + ], (err, peerInfo) => { + expect(err).to.not.exist() + bMultiaddr = peerInfo[0].addresses[0] + ipfsdCId = peerInfo[1].id + cMultiaddr = peerInfo[1].addresses[0] + done() + }) + }) + + // Connect the nodes + before(function (done) { + this.timeout(30 * 1000) + let interval + + // Check to see if peers are already connected + const checkConnections = () => { + ipfsdB.api.swarm.peers((err, peerInfos) => { + if (err) return done(err) + + if (peerInfos.length > 1) { + clearInterval(interval) + return done() + } + }) + } + + parallel([ + ipfsdA.api.swarm.connect.bind(ipfsdA.api, bMultiaddr), + ipfsdB.api.swarm.connect.bind(ipfsdB.api, cMultiaddr) + ], (err) => { + if (err) return done(err) + interval = setInterval(checkConnections, 300) + }) + }) + + after((done) => { + if (!ipfsdA) return done() + ipfsdA.stop(done) + }) + + after((done) => { + if (!ipfsdB) return done() + ipfsdB.stop(done) + }) + + after((done) => { + if (!ipfsdC) return done() + ipfsdC.stop(done) + }) + + it('if enabled uses the DHT peer routing to find peer', (done) => { + let messageNum = 0 + let packetNum = 0 + const count = 3 + pull( + ipfsdA.api.pingPullStream(ipfsdCId, { count }), + drain((res) => { + messageNum++ + expect(res.success).to.be.true() + // Assert that the ping command falls back to the peerRouting + if (messageNum === 1) { + expect(res.text).to.include('Looking up') + } + // It's a pong + if (isPong(res)) { + packetNum++ + } + }, (err) => { + expect(err).to.not.exist() + expect(packetNum).to.equal(count) + done() + }) + ) + }) + }) +}) diff --git a/test/http-api/inject/ping.js b/test/http-api/inject/ping.js new file mode 100644 index 0000000000..7ccf747816 --- /dev/null +++ b/test/http-api/inject/ping.js @@ -0,0 +1,52 @@ +/* eslint max-nested-callbacks: ["error", 8] */ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') + +const expect = chai.expect +chai.use(dirtyChai) + +module.exports = (http) => { + describe('/ping', function () { + let api + + before(() => { + api = http.api.server.select('API') + }) + + it('returns 400 if both n and count are provided', (done) => { + api.inject({ + method: 'GET', + url: `/api/v0/ping?arg=peerid&n=1&count=1` + }, (res) => { + expect(res.statusCode).to.equal(400) + done() + }) + }) + + it('returns 400 if arg is not provided', (done) => { + api.inject({ + method: 'GET', + url: `/api/v0/ping?count=1` + }, (res) => { + expect(res.statusCode).to.equal(400) + done() + }) + }) + + it('returns 200 and the response stream with the ping result', (done) => { + api.inject({ + method: 'GET', + url: `/api/v0/ping?arg=peerid` + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.headers['x-chunked-output']).to.equal('1') + expect(res.headers['transfer-encoding']).to.equal('chunked') + expect(res.headers['content-type']).to.include('application/json') + done() + }) + }) + }) +}