diff --git a/src/http/api/resources/ping.js b/src/http/api/resources/ping.js index 77a75003f4..35c65ad818 100644 --- a/src/http/api/resources/ping.js +++ b/src/http/api/resources/ping.js @@ -13,12 +13,12 @@ exports.get = { query: Joi.object().keys({ n: Joi.alternatives() .when('count', { - is: true, + is: Joi.any().exist(), then: Joi.any().forbidden(), otherwise: Joi.number().greater(0) }), count: Joi.number().greater(0), - arg: Joi.string() + arg: Joi.string().required() }).unknown() }, handler: (request, reply) => { @@ -26,7 +26,6 @@ exports.get = { const peerId = request.query.arg // Default count to 10 const count = request.query.n || request.query.count || 10 - ipfs.ping(peerId, count, (err, pullStream) => { if (err) { return reply(boom.badRequest(err)) diff --git a/test/cli/ping.js b/test/cli/ping.js index f52c3c29c1..a571b2e380 100644 --- a/test/cli/ping.js +++ b/test/cli/ping.js @@ -4,14 +4,13 @@ const chai = require('chai') const dirtyChai = require('dirty-chai') -const expect = chai.expect -chai.use(dirtyChai) -const delay = require('delay') const series = require('async/series') +const DaemonFactory = require('ipfsd-ctl') const ipfsExec = require('../utils/ipfs-exec') -const DaemonFactory = require('ipfsd-ctl') const df = DaemonFactory.create({ type: 'js' }) +const expect = chai.expect +chai.use(dirtyChai) const config = { Bootstrap: [], @@ -66,6 +65,7 @@ describe('ping', function () { }, (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) }) }) @@ -91,7 +91,7 @@ describe('ping', function () { ping.stdout.on('end', () => { expect(result).to.have.lengthOf(12) expect(result[0]).to.equal(`PING ${ipfsdBId}`) - for(let i = 1; i < 11; i++) { + 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$/) diff --git a/test/http-api/inject/ping.js b/test/http-api/inject/ping.js new file mode 100644 index 0000000000..8cda3836c3 --- /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=someRandomId&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=someRandomId` + }, (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() + }) + }) + }) +}