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

Commit fc06d59

Browse files
committed
chore: add tests to the ping http API
1 parent 89c61e3 commit fc06d59

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

src/http/api/resources/ping.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@ exports.get = {
1313
query: Joi.object().keys({
1414
n: Joi.alternatives()
1515
.when('count', {
16-
is: true,
16+
is: Joi.any().exist(),
1717
then: Joi.any().forbidden(),
1818
otherwise: Joi.number().greater(0)
1919
}),
2020
count: Joi.number().greater(0),
21-
arg: Joi.string()
21+
arg: Joi.string().required()
2222
}).unknown()
2323
},
2424
handler: (request, reply) => {
2525
const ipfs = request.server.app.ipfs
2626
const peerId = request.query.arg
2727
// Default count to 10
2828
const count = request.query.n || request.query.count || 10
29-
3029
ipfs.ping(peerId, count, (err, pullStream) => {
3130
if (err) {
3231
return reply(boom.badRequest(err))

test/cli/ping.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
const chai = require('chai')
66
const dirtyChai = require('dirty-chai')
7-
const expect = chai.expect
8-
chai.use(dirtyChai)
9-
const delay = require('delay')
107
const series = require('async/series')
8+
const DaemonFactory = require('ipfsd-ctl')
119
const ipfsExec = require('../utils/ipfs-exec')
1210

13-
const DaemonFactory = require('ipfsd-ctl')
1411
const df = DaemonFactory.create({ type: 'js' })
12+
const expect = chai.expect
13+
chai.use(dirtyChai)
1514

1615
const config = {
1716
Bootstrap: [],
@@ -66,6 +65,7 @@ describe('ping', function () {
6665
}, (err, _ipfsd) => {
6766
expect(err).to.not.exist()
6867
ipfsdA = _ipfsd
68+
// Without DHT we need to have an already established connection
6969
ipfsdA.api.swarm.connect(bMultiaddr, done)
7070
})
7171
})
@@ -91,7 +91,7 @@ describe('ping', function () {
9191
ping.stdout.on('end', () => {
9292
expect(result).to.have.lengthOf(12)
9393
expect(result[0]).to.equal(`PING ${ipfsdBId}`)
94-
for(let i = 1; i < 11; i++) {
94+
for (let i = 1; i < 11; i++) {
9595
expect(result[i]).to.match(/^Pong received: time=\d+ ms$/)
9696
}
9797
expect(result[11]).to.match(/^Average latency: \d+(.\d+)?ms$/)

test/http-api/inject/ping.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* eslint max-nested-callbacks: ["error", 8] */
2+
/* eslint-env mocha */
3+
'use strict'
4+
5+
const chai = require('chai')
6+
const dirtyChai = require('dirty-chai')
7+
8+
const expect = chai.expect
9+
chai.use(dirtyChai)
10+
11+
module.exports = (http) => {
12+
describe('/ping', function () {
13+
let api
14+
15+
before(() => {
16+
api = http.api.server.select('API')
17+
})
18+
19+
it('returns 400 if both n and count are provided', (done) => {
20+
api.inject({
21+
method: 'GET',
22+
url: `/api/v0/ping?arg=someRandomId&n=1&count=1`
23+
}, (res) => {
24+
expect(res.statusCode).to.equal(400)
25+
done()
26+
})
27+
})
28+
29+
it('returns 400 if arg is not provided', (done) => {
30+
api.inject({
31+
method: 'GET',
32+
url: `/api/v0/ping?count=1`
33+
}, (res) => {
34+
expect(res.statusCode).to.equal(400)
35+
done()
36+
})
37+
})
38+
39+
it('returns 200 and the response stream with the ping result', (done) => {
40+
api.inject({
41+
method: 'GET',
42+
url: `/api/v0/ping?arg=someRandomId`
43+
}, (res) => {
44+
expect(res.statusCode).to.equal(200)
45+
expect(res.headers['x-chunked-output']).to.equal('1')
46+
expect(res.headers['transfer-encoding']).to.equal('chunked')
47+
expect(res.headers['content-type']).to.include('application/json')
48+
done()
49+
})
50+
})
51+
})
52+
}

0 commit comments

Comments
 (0)