Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
chore: add tests to the ping http API
Browse files Browse the repository at this point in the history
  • Loading branch information
JGAntunes committed May 6, 2018
1 parent 89c61e3 commit fc06d59
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/http/api/resources/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ 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) => {
const ipfs = request.server.app.ipfs
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))
Expand Down
10 changes: 5 additions & 5 deletions test/cli/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down Expand Up @@ -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)
})
})
Expand All @@ -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$/)
Expand Down
52 changes: 52 additions & 0 deletions test/http-api/inject/ping.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
})
}

0 comments on commit fc06d59

Please sign in to comment.