test: change request handler test to new approach

This commit is contained in:
Feross Aboukhadijeh 2017-03-10 13:39:12 -08:00
parent 51b57f66cf
commit 0c683dfe64

View File

@ -3,26 +3,33 @@ var Client = require('../')
var common = require('./common') var common = require('./common')
var fixtures = require('webtorrent-fixtures') var fixtures = require('webtorrent-fixtures')
var test = require('tape') var test = require('tape')
var Server = require('../server')
var peerId = Buffer.from('01234567890123456789') var peerId = Buffer.from('01234567890123456789')
function testRequestHandler (t, serverType) { function testRequestHandler (t, serverType) {
t.plan(4) t.plan(5)
var opts = { serverType: serverType } // this is test-suite-only option var opts = { serverType: serverType } // this is test-suite-only option
opts.requestHandler = {
getParams: function (params) { class Swarm extends Server.Swarm {
params.extra = 123 announce (params, cb) {
return params super.announce(params, function (err, response) {
}, if (err) return cb(response)
getResponse: function (params, cb) { response.complete = 246
return function (err, response) { response.extraData = 'hi'
response.complete = params.extra * 2 cb(null, response)
cb(err, response) })
}
} }
} }
// Use a custom Swarm implementation for this test only
var OldSwarm = Server.Swarm
Server.Swarm = Swarm
t.on('end', function () {
Server.Swarm = OldSwarm
})
common.createServer(t, opts, function (server, announceUrl) { common.createServer(t, opts, function (server, announceUrl) {
var client1 = new Client({ var client1 = new Client({
infoHash: fixtures.alice.parsedTorrent.infoHash, infoHash: fixtures.alice.parsedTorrent.infoHash,
@ -41,6 +48,7 @@ function testRequestHandler (t, serverType) {
client1.once('update', function (data) { client1.once('update', function (data) {
t.equal(data.complete, 246) t.equal(data.complete, 246)
t.equal(data.extraData.toString(), 'hi')
client1.destroy(function () { client1.destroy(function () {
t.pass('client1 destroyed') t.pass('client1 destroyed')