bittorrent-tracker/test/request-handler.js
renovate[bot] b72d226ed8
chore(deps): update webtorrent (#445)
* chore(deps): update webtorrent

* fix: dependencies (#446)

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Cas <6506529+ThaUnknown@users.noreply.github.com>
2023-02-01 03:43:44 +01:00

74 lines
1.9 KiB
JavaScript

import Client from '../index.js'
import common from './common.js'
import fixtures from 'webtorrent-fixtures'
import test from 'tape'
import Server from '../server.js'
const peerId = Buffer.from('01234567890123456789')
function testRequestHandler (t, serverType) {
t.plan(5)
const opts = { serverType } // this is test-suite-only option
class Swarm extends Server.Swarm {
announce (params, cb) {
super.announce(params, (err, response) => {
if (cb && err) return cb(response)
response.complete = 246
response.extraData = 'hi'
if (cb) cb(null, response)
})
}
}
// Use a custom Swarm implementation for this test only
const OldSwarm = Server.Swarm
Server.Swarm = Swarm
t.on('end', () => {
Server.Swarm = OldSwarm
})
common.createServer(t, opts, (server, announceUrl) => {
const client1 = new Client({
infoHash: fixtures.alice.parsedTorrent.infoHash,
announce: announceUrl,
peerId,
port: 6881,
wrtc: {}
})
client1.on('error', err => { t.error(err) })
if (serverType === 'ws') common.mockWebsocketTracker(client1)
server.once('start', () => {
t.pass('got start message from client1')
})
client1.once('update', data => {
t.equal(data.complete, 246)
t.equal(Buffer.from(data.extraData).toString(), 'hi')
client1.destroy(() => {
t.pass('client1 destroyed')
})
server.close(() => {
t.pass('server destroyed')
})
})
client1.start()
})
}
test('http: request handler option intercepts announce requests and responses', t => {
testRequestHandler(t, 'http')
})
test('ws: request handler option intercepts announce requests and responses', 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!