test: add tests for getAnnounceOpts (#107)

This commit is contained in:
Feross Aboukhadijeh 2016-02-29 17:02:55 -08:00
parent 7893d5c5bf
commit e15125ca80
2 changed files with 91 additions and 2 deletions

View File

@ -273,6 +273,5 @@ Client.prototype._defaultAnnounceOpts = function (opts) {
}
if (self._getAnnounceOpts) opts = extend(opts, self._getAnnounceOpts())
return opts
}

View File

@ -16,7 +16,7 @@ function mockWebSocketTracker (client) {
client._trackers[0]._generateOffers = function (numwant, cb) {
var offers = []
for (var i = 0; i < numwant; i++) {
offers.push('fake_offer_' + i)
offers.push({ fake_offer: 'fake_offer_' + i })
}
process.nextTick(function () {
cb(offers)
@ -188,6 +188,96 @@ test('udp: client.scrape()', function (t) {
// testClientScrape(t, 'ws')
// })
function testClientAnnounceWithParams (t, serverType) {
t.plan(5)
common.createServer(t, serverType, function (server, announceUrl) {
parsedTorrent.announce = [ announceUrl ]
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
server.on('start', function (peer, params) {
t.equal(params.testParam, 'this is a test')
})
if (serverType === 'ws') mockWebSocketTracker(client)
client.on('error', function (err) { t.error(err) })
client.on('warning', function (err) { t.error(err) })
client.once('update', function (data) {
t.equal(data.announce, announceUrl)
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')
client.stop()
client.once('update', function () {
t.pass('got response to stop')
server.close()
client.destroy()
})
})
client.start({
testParam: 'this is a test'
})
})
}
test('http: client.announce() with params', function (t) {
testClientAnnounceWithParams(t, 'http')
})
test('ws: client.announce() with params', function (t) {
testClientAnnounceWithParams(t, 'ws')
})
function testClientGetAnnounceOpts (t, serverType) {
t.plan(5)
common.createServer(t, serverType, function (server, announceUrl) {
parsedTorrent.announce = [ announceUrl ]
var opts = {
getAnnounceOpts: function () {
return {
testParam: 'this is a test'
}
},
wrtc: {}
}
var client = new Client(peerId1, port, parsedTorrent, opts)
server.on('start', function (peer, params) {
t.equal(params.testParam, 'this is a test')
})
if (serverType === 'ws') mockWebSocketTracker(client)
client.on('error', function (err) { t.error(err) })
client.on('warning', function (err) { t.error(err) })
client.once('update', function (data) {
t.equal(data.announce, announceUrl)
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')
client.stop()
client.once('update', function () {
t.pass('got response to stop')
server.close()
client.destroy()
})
})
client.start()
})
}
test('http: client `opts.getAnnounceOpts`', function (t) {
testClientGetAnnounceOpts(t, 'http')
})
test('ws: client `opts.getAnnounceOpts`', function (t) {
testClientGetAnnounceOpts(t, 'ws')
})
function testClientAnnounceWithNumWant (t, serverType) {
t.plan(4)
common.createServer(t, serverType, function (server, announceUrl) {