bittorrent-tracker/test/request-handler.js
2017-03-10 13:39:12 -08:00

75 lines
2.0 KiB
JavaScript

var Buffer = require('safe-buffer').Buffer
var Client = require('../')
var common = require('./common')
var fixtures = require('webtorrent-fixtures')
var test = require('tape')
var Server = require('../server')
var peerId = Buffer.from('01234567890123456789')
function testRequestHandler (t, serverType) {
t.plan(5)
var opts = { serverType: serverType } // this is test-suite-only option
class Swarm extends Server.Swarm {
announce (params, cb) {
super.announce(params, function (err, response) {
if (err) return cb(response)
response.complete = 246
response.extraData = 'hi'
cb(null, 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) {
var client1 = new Client({
infoHash: fixtures.alice.parsedTorrent.infoHash,
announce: announceUrl,
peerId: peerId,
port: 6881,
wrtc: {}
})
client1.on('error', function (err) { t.error(err) })
if (serverType === 'ws') common.mockWebsocketTracker(client1)
server.once('start', function () {
t.pass('got start message from client1')
})
client1.once('update', function (data) {
t.equal(data.complete, 246)
t.equal(data.extraData.toString(), 'hi')
client1.destroy(function () {
t.pass('client1 destroyed')
})
server.close(function () {
t.pass('server destroyed')
})
})
client1.start()
})
}
test('http: request handler option intercepts announce requests and responses', function (t) {
testRequestHandler(t, 'http')
})
test('ws: request handler option intercepts announce requests and responses', function (t) {
testRequestHandler(t, 'ws')
})
// NOTE: it's not possible to include extra data in a UDP response, because it's compact and accepts only params that are in the spec!