@@ -13,6 +13,8 @@ const CID = require('cids')
13
13
const DelegatedContentRouter = require ( 'libp2p-delegated-content-routing' )
14
14
const sinon = require ( 'sinon' )
15
15
const nock = require ( 'nock' )
16
+ const ma = require ( 'multiaddr' )
17
+ const Node = require ( './utils/bundle-nodejs' )
16
18
17
19
const createNode = require ( './utils/create-node' )
18
20
const createPeerInfo = createNode . createPeerInfo
@@ -131,25 +133,31 @@ describe('.contentRouting', () => {
131
133
delegate = new DelegatedContentRouter ( peerInfo . id , {
132
134
host : '0.0.0.0' ,
133
135
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,
137
142
modules : {
138
143
contentRouting : delegate
144
+ } ,
145
+ config : {
146
+ relay : {
147
+ enabled : true ,
148
+ hop : {
149
+ enabled : true ,
150
+ active : false
151
+ }
152
+ }
139
153
}
140
- } , ( err , node ) => {
141
- expect ( err ) . to . not . exist ( )
142
- nodeA = node
143
- nodeA . start ( cb )
144
154
} )
155
+ nodeA . start ( cb )
145
156
}
146
157
] , done )
147
158
} )
148
159
149
- afterEach ( ( ) => {
150
- nock . cleanAll ( )
151
- nock . restore ( )
152
- } )
160
+ afterEach ( ( ) => nock . cleanAll )
153
161
154
162
it ( 'should use the delegate router to provide' , ( done ) => {
155
163
const stub = sinon . stub ( delegate , 'provide' ) . callsFake ( ( ) => {
@@ -168,65 +176,59 @@ describe('.contentRouting', () => {
168
176
} )
169
177
170
178
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 ) => {
185
203
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 )
189
205
done ( )
190
206
} )
191
207
} )
192
208
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
+ } )
231
233
} )
232
234
} )
0 commit comments