|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const nock = require('nock') |
| 5 | +const chai = require('chai') |
| 6 | +const dirtyChai = require('dirty-chai') |
| 7 | +const expect = chai.expect |
| 8 | +chai.use(dirtyChai) |
| 9 | + |
| 10 | +const IPFSApi = require('../../src') |
| 11 | + |
| 12 | +describe('.swarm.peers', function () { |
| 13 | + this.timeout(50 * 1000) // slow CI |
| 14 | + |
| 15 | + const ipfs = IPFSApi('/ip4/127.0.0.1/tcp/5001') |
| 16 | + const apiUrl = 'http://127.0.0.1:5001' |
| 17 | + |
| 18 | + it('handles a peer response', (done) => { |
| 19 | + const response = { Peers: [{ Addr: '/ip4/104.131.131.82/tcp/4001', Peer: 'QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ', Latency: '', Muxer: '', Streams: null }] } |
| 20 | + |
| 21 | + const scope = nock(apiUrl) |
| 22 | + .post('/api/v0/swarm/peers') |
| 23 | + .query(true) |
| 24 | + .reply(200, response) |
| 25 | + |
| 26 | + ipfs.swarm.peers((err, res) => { |
| 27 | + expect(err).to.not.exist() |
| 28 | + expect(res).to.be.a('array') |
| 29 | + expect(res.length).to.equal(1) |
| 30 | + expect(res[0].error).to.not.exist() |
| 31 | + expect(res[0].addr.toString()).to.equal(response.Peers[0].Addr) |
| 32 | + expect(res[0].peer.toB58String()).to.equal(response.Peers[0].Peer) |
| 33 | + expect(scope.isDone()).to.equal(true) |
| 34 | + done() |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + it('handles a go-ipfs <= 0.4.4 peer response', (done) => { |
| 39 | + const response = { Strings: ['/ip4/73.109.217.59/tcp/49311/ipfs/QmWjxEGC7BthJrCf7QTModrcsRweHbupdPTY4oGMVoDZXm'] } |
| 40 | + |
| 41 | + const scope = nock(apiUrl) |
| 42 | + .post('/api/v0/swarm/peers') |
| 43 | + .query(true) |
| 44 | + .reply(200, response) |
| 45 | + |
| 46 | + ipfs.swarm.peers((err, res) => { |
| 47 | + expect(err).to.not.exist() |
| 48 | + expect(res).to.be.a('array') |
| 49 | + expect(res.length).to.equal(1) |
| 50 | + expect(res[0].error).to.not.exist() |
| 51 | + expect(res[0].addr.toString()).to.equal('/ip4/73.109.217.59/tcp/49311/ipfs/QmWjxEGC7BthJrCf7QTModrcsRweHbupdPTY4oGMVoDZXm') |
| 52 | + expect(res[0].peer.toB58String()).to.equal('QmWjxEGC7BthJrCf7QTModrcsRweHbupdPTY4oGMVoDZXm') |
| 53 | + expect(scope.isDone()).to.equal(true) |
| 54 | + done() |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + it('handles an ip6 quic peer', (done) => { |
| 59 | + const response = { Peers: [{ Addr: '/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/udp/4001/quic', Peer: 'QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC', Latency: '', Muxer: '', Streams: null }] } |
| 60 | + |
| 61 | + const scope = nock(apiUrl) |
| 62 | + .post('/api/v0/swarm/peers') |
| 63 | + .query(true) |
| 64 | + .reply(200, response) |
| 65 | + |
| 66 | + ipfs.swarm.peers((err, res) => { |
| 67 | + expect(err).to.not.exist() |
| 68 | + expect(res).to.be.a('array') |
| 69 | + expect(res.length).to.equal(1) |
| 70 | + expect(res[0].error).to.not.exist() |
| 71 | + expect(res[0].addr.toString()).to.equal(response.Peers[0].Addr) |
| 72 | + expect(res[0].peer.toB58String()).to.equal(response.Peers[0].Peer) |
| 73 | + expect(scope.isDone()).to.equal(true) |
| 74 | + done() |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + it('handles unvalidatable peer addr', (done) => { |
| 79 | + const response = { Peers: [{ Addr: '/ip4/104.131.131.82/future-tech', Peer: 'QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC', Latency: '', Muxer: '', Streams: null }] } |
| 80 | + |
| 81 | + const scope = nock(apiUrl) |
| 82 | + .post('/api/v0/swarm/peers') |
| 83 | + .query(true) |
| 84 | + .reply(200, response) |
| 85 | + |
| 86 | + ipfs.swarm.peers((err, res) => { |
| 87 | + expect(err).to.not.exist() |
| 88 | + expect(res).to.be.a('array') |
| 89 | + expect(res.length).to.equal(1) |
| 90 | + expect(res[0].error).to.exist() |
| 91 | + expect(res[0].rawPeerInfo).to.deep.equal(response.Peers[0]) |
| 92 | + expect(scope.isDone()).to.equal(true) |
| 93 | + done() |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + it('handles an error response', (done) => { |
| 98 | + const scope = nock(apiUrl) |
| 99 | + .post('/api/v0/swarm/peers') |
| 100 | + .query(true) |
| 101 | + .replyWithError('something awful happened') |
| 102 | + |
| 103 | + ipfs.swarm.peers((err, res) => { |
| 104 | + expect(err.message).to.equal('something awful happened') |
| 105 | + expect(res).to.not.exist() |
| 106 | + expect(scope.isDone()).to.equal(true) |
| 107 | + done() |
| 108 | + }) |
| 109 | + }) |
| 110 | +}) |
0 commit comments