diff --git a/client.js b/client.js index 1e9d974..254a63c 100644 --- a/client.js +++ b/client.js @@ -273,6 +273,5 @@ Client.prototype._defaultAnnounceOpts = function (opts) { } if (self._getAnnounceOpts) opts = extend(opts, self._getAnnounceOpts()) - return opts } diff --git a/test/client.js b/test/client.js index 9c5ff4f..d4e334d 100644 --- a/test/client.js +++ b/test/client.js @@ -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) {