Skip to content

Commit e15125c

Browse files
committed
test: add tests for getAnnounceOpts (webtorrent#107)
1 parent 7893d5c commit e15125c

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

client.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,5 @@ Client.prototype._defaultAnnounceOpts = function (opts) {
273273
}
274274

275275
if (self._getAnnounceOpts) opts = extend(opts, self._getAnnounceOpts())
276-
277276
return opts
278277
}

test/client.js

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function mockWebSocketTracker (client) {
1616
client._trackers[0]._generateOffers = function (numwant, cb) {
1717
var offers = []
1818
for (var i = 0; i < numwant; i++) {
19-
offers.push('fake_offer_' + i)
19+
offers.push({ fake_offer: 'fake_offer_' + i })
2020
}
2121
process.nextTick(function () {
2222
cb(offers)
@@ -188,6 +188,96 @@ test('udp: client.scrape()', function (t) {
188188
// testClientScrape(t, 'ws')
189189
// })
190190

191+
function testClientAnnounceWithParams (t, serverType) {
192+
t.plan(5)
193+
common.createServer(t, serverType, function (server, announceUrl) {
194+
parsedTorrent.announce = [ announceUrl ]
195+
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
196+
197+
server.on('start', function (peer, params) {
198+
t.equal(params.testParam, 'this is a test')
199+
})
200+
201+
if (serverType === 'ws') mockWebSocketTracker(client)
202+
client.on('error', function (err) { t.error(err) })
203+
client.on('warning', function (err) { t.error(err) })
204+
205+
client.once('update', function (data) {
206+
t.equal(data.announce, announceUrl)
207+
t.equal(typeof data.complete, 'number')
208+
t.equal(typeof data.incomplete, 'number')
209+
210+
client.stop()
211+
212+
client.once('update', function () {
213+
t.pass('got response to stop')
214+
server.close()
215+
client.destroy()
216+
})
217+
})
218+
219+
client.start({
220+
testParam: 'this is a test'
221+
})
222+
})
223+
}
224+
225+
test('http: client.announce() with params', function (t) {
226+
testClientAnnounceWithParams(t, 'http')
227+
})
228+
229+
test('ws: client.announce() with params', function (t) {
230+
testClientAnnounceWithParams(t, 'ws')
231+
})
232+
233+
function testClientGetAnnounceOpts (t, serverType) {
234+
t.plan(5)
235+
common.createServer(t, serverType, function (server, announceUrl) {
236+
parsedTorrent.announce = [ announceUrl ]
237+
var opts = {
238+
getAnnounceOpts: function () {
239+
return {
240+
testParam: 'this is a test'
241+
}
242+
},
243+
wrtc: {}
244+
}
245+
var client = new Client(peerId1, port, parsedTorrent, opts)
246+
247+
server.on('start', function (peer, params) {
248+
t.equal(params.testParam, 'this is a test')
249+
})
250+
251+
if (serverType === 'ws') mockWebSocketTracker(client)
252+
client.on('error', function (err) { t.error(err) })
253+
client.on('warning', function (err) { t.error(err) })
254+
255+
client.once('update', function (data) {
256+
t.equal(data.announce, announceUrl)
257+
t.equal(typeof data.complete, 'number')
258+
t.equal(typeof data.incomplete, 'number')
259+
260+
client.stop()
261+
262+
client.once('update', function () {
263+
t.pass('got response to stop')
264+
server.close()
265+
client.destroy()
266+
})
267+
})
268+
269+
client.start()
270+
})
271+
}
272+
273+
test('http: client `opts.getAnnounceOpts`', function (t) {
274+
testClientGetAnnounceOpts(t, 'http')
275+
})
276+
277+
test('ws: client `opts.getAnnounceOpts`', function (t) {
278+
testClientGetAnnounceOpts(t, 'ws')
279+
})
280+
191281
function testClientAnnounceWithNumWant (t, serverType) {
192282
t.plan(4)
193283
common.createServer(t, serverType, function (server, announceUrl) {

0 commit comments

Comments
 (0)