Skip to content

Commit 894cb4f

Browse files
committed
test: add error case tests
1 parent ae2505c commit 894cb4f

File tree

2 files changed

+100
-59
lines changed

2 files changed

+100
-59
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"wrtc": "~0.1.6"
8282
},
8383
"resolutions": {
84-
"ipfs-api": "github:ipfs/js-ipfs-api#test/easier-mocking"
84+
"ipfs-api": "github:ipfs/js-ipfs-api#master"
8585
},
8686
"contributors": [
8787
"Alan Shaw <[email protected]>",

test/content-routing.node.js

Lines changed: 99 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -159,75 +159,116 @@ describe('.contentRouting', () => {
159159

160160
afterEach(() => nock.cleanAll)
161161

162-
it('should use the delegate router to provide', (done) => {
163-
const stub = sinon.stub(delegate, 'provide').callsFake(() => {
164-
stub.restore()
165-
done()
162+
describe('provide', () => {
163+
it('should use the delegate router to provide', (done) => {
164+
const stub = sinon.stub(delegate, 'provide').callsFake(() => {
165+
stub.restore()
166+
done()
167+
})
168+
nodeA.contentRouting.provide()
166169
})
167-
nodeA.contentRouting.provide()
168-
})
169170

170-
it('should use the delegate router to find providers', (done) => {
171-
const stub = sinon.stub(delegate, 'findProviders').callsFake(() => {
172-
stub.restore()
173-
done()
174-
})
175-
nodeA.contentRouting.findProviders()
176-
})
171+
it('should be able to register as a provider', (done) => {
172+
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
173+
const mockApi = nock('http://0.0.0.0:60197')
174+
// mock the swarm connect
175+
.post('/api/v0/swarm/connect')
176+
.query({
177+
arg: `/ip4/0.0.0.0/tcp/60194/p2p-circuit/ipfs/${nodeA.peerInfo.id.toB58String()}`,
178+
'stream-channels': true
179+
})
180+
.reply(200, {
181+
Strings: [`connect ${nodeA.peerInfo.id.toB58String()} success`]
182+
}, ['Content-Type', 'application/json'])
183+
// mock the refs call
184+
.post('/api/v0/refs')
185+
.query({
186+
recursive: true,
187+
arg: cid.toBaseEncodedString(),
188+
'stream-channels': true
189+
})
190+
.reply(200, null, [
191+
'Content-Type', 'application/json',
192+
'X-Chunked-Output', '1'
193+
])
177194

178-
it('should be able to register as a provider', (done) => {
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
195+
nodeA.contentRouting.provide(cid, (err) => {
196+
expect(err).to.not.exist()
197+
expect(mockApi.isDone()).to.equal(true)
198+
done()
196199
})
197-
.reply(200, null, [
198-
'Content-Type', 'application/json',
199-
'X-Chunked-Output', '1'
200-
])
200+
})
201201

202-
nodeA.contentRouting.provide(cid, (err) => {
203-
expect(err).to.not.exist()
204-
expect(mockApi.isDone()).to.equal(true)
205-
done()
202+
it('should handle errors when registering as a provider', (done) => {
203+
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
204+
const mockApi = nock('http://0.0.0.0:60197')
205+
// mock the swarm connect
206+
.post('/api/v0/swarm/connect')
207+
.query({
208+
arg: `/ip4/0.0.0.0/tcp/60194/p2p-circuit/ipfs/${nodeA.peerInfo.id.toB58String()}`,
209+
'stream-channels': true
210+
})
211+
.reply(502, 'Bad Gateway', ['Content-Type', 'application/json'])
212+
213+
nodeA.contentRouting.provide(cid, (err) => {
214+
expect(err).to.exist()
215+
expect(mockApi.isDone()).to.equal(true)
216+
done()
217+
})
206218
})
207219
})
208220

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
221+
describe('find providers', () => {
222+
it('should use the delegate router to find providers', (done) => {
223+
const stub = sinon.stub(delegate, 'findProviders').callsFake(() => {
224+
stub.restore()
225+
done()
217226
})
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-
])
227+
nodeA.contentRouting.findProviders()
228+
})
222229

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
230+
it('should be able to find providers', (done) => {
231+
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
232+
const provider = 'QmZNgCqZCvTsi3B4Vt7gsSqpkqDpE7M2Y9TDmEhbDb4ceF'
233+
const mockApi = nock('http://0.0.0.0:60197')
234+
.post('/api/v0/dht/findprovs')
235+
.query({
236+
arg: cid.toBaseEncodedString(),
237+
'stream-channels': true
238+
})
239+
.reply(200, `{"Extra":"","ID":"QmWKqWXCtRXEeCQTo3FoZ7g4AfnGiauYYiczvNxFCHicbB","Responses":[{"Addrs":["/ip4/0.0.0.0/tcp/0"],"ID":"${provider}"}],"Type":1}\n`, [
240+
'Content-Type', 'application/json',
241+
'X-Chunked-Output', '1'
242+
])
243+
244+
nodeA.contentRouting.findProviders(cid, (err, response) => {
245+
expect(err).to.not.exist()
246+
expect(response).to.have.length(1)
247+
expect(response[0]).to.include({
248+
id: provider
249+
})
250+
expect(mockApi.isDone()).to.equal(true)
251+
done()
252+
})
253+
})
254+
255+
it('should handle errors when finding providers', (done) => {
256+
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
257+
const mockApi = nock('http://0.0.0.0:60197')
258+
.post('/api/v0/dht/findprovs')
259+
.query({
260+
arg: cid.toBaseEncodedString(),
261+
'stream-channels': true
262+
})
263+
.reply(502, 'Bad Gateway', [
264+
'X-Chunked-Output', '1'
265+
])
266+
267+
nodeA.contentRouting.findProviders(cid, (err) => {
268+
expect(err).to.exist()
269+
expect(mockApi.isDone()).to.equal(true)
270+
done()
228271
})
229-
expect(mockApi.isDone()).to.equal(true)
230-
done()
231272
})
232273
})
233274
})

0 commit comments

Comments
 (0)