Skip to content

Commit ae2505c

Browse files
committed
test: add tests for content routing
1 parent 038203c commit ae2505c

File tree

2 files changed

+70
-68
lines changed

2 files changed

+70
-68
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
"cids": "~0.5.3",
6060
"dirty-chai": "^2.0.1",
6161
"electron-webrtc": "~0.3.0",
62-
"libp2p-circuit": "^0.2.1",
63-
"libp2p-delegated-content-routing": "github:libp2p/js-libp2p-delegated-content-routing#master",
62+
"libp2p-circuit": "~0.2.1",
63+
"libp2p-delegated-content-routing": "github:libp2p/js-libp2p-delegated-content-routing#fix/tests",
6464
"libp2p-delegated-peer-routing": "github:libp2p/js-libp2p-delegated-peer-routing#master",
6565
"libp2p-kad-dht": "~0.10.1",
6666
"libp2p-mdns": "~0.12.0",

test/content-routing.node.js

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const CID = require('cids')
1313
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
1414
const sinon = require('sinon')
1515
const nock = require('nock')
16+
const ma = require('multiaddr')
17+
const Node = require('./utils/bundle-nodejs')
1618

1719
const createNode = require('./utils/create-node')
1820
const createPeerInfo = createNode.createPeerInfo
@@ -131,25 +133,31 @@ describe('.contentRouting', () => {
131133
delegate = new DelegatedContentRouter(peerInfo.id, {
132134
host: '0.0.0.0',
133135
protocol: 'http',
134-
port: '50082'
135-
})
136-
createNode('/ip4/0.0.0.0/tcp/0', {
136+
port: 60197
137+
}, [
138+
ma('/ip4/0.0.0.0/tcp/60194')
139+
])
140+
nodeA = new Node({
141+
peerInfo,
137142
modules: {
138143
contentRouting: delegate
144+
},
145+
config: {
146+
relay: {
147+
enabled: true,
148+
hop: {
149+
enabled: true,
150+
active: false
151+
}
152+
}
139153
}
140-
}, (err, node) => {
141-
expect(err).to.not.exist()
142-
nodeA = node
143-
nodeA.start(cb)
144154
})
155+
nodeA.start(cb)
145156
}
146157
], done)
147158
})
148159

149-
afterEach(() => {
150-
nock.cleanAll()
151-
nock.restore()
152-
})
160+
afterEach(() => nock.cleanAll)
153161

154162
it('should use the delegate router to provide', (done) => {
155163
const stub = sinon.stub(delegate, 'provide').callsFake(() => {
@@ -168,65 +176,59 @@ describe('.contentRouting', () => {
168176
})
169177

170178
it('should be able to register as a provider', (done) => {
171-
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL')
172-
nock.recorder.rec()
173-
// const mockApi = nock('https://ipfs.io')
174-
// .post('/api/v0/dht/findpeer')
175-
// .query({
176-
// arg: peerKey,
177-
// 'stream-channels': true
178-
// })
179-
// .reply(200, `{"Extra":"","ID":"some other id","Responses":null,"Type":0}\n{"Extra":"","ID":"","Responses":[{"Addrs":["/ip4/127.0.0.1/tcp/4001"],"ID":"${peerKey}"}],"Type":2}\n`, [
180-
// 'Content-Type', 'application/json',
181-
// 'X-Chunked-Output', '1'
182-
// ])
183-
184-
nodeA.contentRouting.provide(cid, (err, data) => {
179+
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
180+
const mockApi = nock('http://0.0.0.0:60197')
181+
// mock the swarm connect
182+
.post('/api/v0/swarm/connect')
183+
.query({
184+
arg: `/ip4/0.0.0.0/tcp/60194/p2p-circuit/ipfs/${nodeA.peerInfo.id.toB58String()}`,
185+
'stream-channels': true
186+
})
187+
.reply(200, {
188+
Strings: [`connect ${nodeA.peerInfo.id.toB58String()} success`]
189+
}, ['Content-Type', 'application/json'])
190+
// mock the refs call
191+
.post('/api/v0/refs')
192+
.query({
193+
recursive: true,
194+
arg: cid.toBaseEncodedString(),
195+
'stream-channels': true
196+
})
197+
.reply(200, null, [
198+
'Content-Type', 'application/json',
199+
'X-Chunked-Output', '1'
200+
])
201+
202+
nodeA.contentRouting.provide(cid, (err) => {
185203
expect(err).to.not.exist()
186-
expect(data).to.equal({})
187-
nock.restore()
188-
// expect(mockApi.isDone()).to.equal(true)
204+
expect(mockApi.isDone()).to.equal(true)
189205
done()
190206
})
191207
})
192208

193-
// it('should error when a peer cannot be found', (done) => {
194-
// const peerKey = 'key of a peer not on the network'
195-
// const mockApi = nock('https://ipfs.io')
196-
// .post('/api/v0/dht/findpeer')
197-
// .query({
198-
// arg: peerKey,
199-
// 'stream-channels': true
200-
// })
201-
// .reply(200, `{"Extra":"","ID":"some other id","Responses":null,"Type":6}\n{"Extra":"","ID":"yet another id","Responses":null,"Type":0}\n{"Extra":"routing:not found","ID":"","Responses":null,"Type":3}\n`, [
202-
// 'Content-Type', 'application/json',
203-
// 'X-Chunked-Output', '1'
204-
// ])
205-
206-
// nodeA.peerRouting.findPeer(peerKey, (err, peerInfo) => {
207-
// expect(err).to.exist()
208-
// expect(peerInfo).to.not.exist()
209-
// expect(mockApi.isDone()).to.equal(true)
210-
// done()
211-
// })
212-
// })
213-
214-
// it('should handle errors from the api', (done) => {
215-
// const peerKey = 'key of a peer not on the network'
216-
// const mockApi = nock('https://ipfs.io')
217-
// .post('/api/v0/dht/findpeer')
218-
// .query({
219-
// arg: peerKey,
220-
// 'stream-channels': true
221-
// })
222-
// .reply(502)
223-
224-
// nodeA.peerRouting.findPeer(peerKey, (err, peerInfo) => {
225-
// expect(err).to.exist()
226-
// expect(peerInfo).to.not.exist()
227-
// expect(mockApi.isDone()).to.equal(true)
228-
// done()
229-
// })
230-
// })
209+
it('should be able to find providers', (done) => {
210+
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
211+
const provider = 'QmZNgCqZCvTsi3B4Vt7gsSqpkqDpE7M2Y9TDmEhbDb4ceF'
212+
const mockApi = nock('http://0.0.0.0:60197')
213+
.post('/api/v0/dht/findprovs')
214+
.query({
215+
arg: cid.toBaseEncodedString(),
216+
'stream-channels': true
217+
})
218+
.reply(200, `{"Extra":"","ID":"QmWKqWXCtRXEeCQTo3FoZ7g4AfnGiauYYiczvNxFCHicbB","Responses":[{"Addrs":["/ip4/0.0.0.0/tcp/0"],"ID":"${provider}"}],"Type":1}\n`, [
219+
'Content-Type', 'application/json',
220+
'X-Chunked-Output', '1'
221+
])
222+
223+
nodeA.contentRouting.findProviders(cid.toBaseEncodedString(), (err, response) => {
224+
expect(err).to.not.exist()
225+
expect(response).to.have.length(1)
226+
expect(response[0]).to.include({
227+
id: provider
228+
})
229+
expect(mockApi.isDone()).to.equal(true)
230+
done()
231+
})
232+
})
231233
})
232234
})

0 commit comments

Comments
 (0)