2022-12-05 22:06:54 +00:00
|
|
|
import Client from '../index.js'
|
|
|
|
import common from './common.js'
|
|
|
|
import fixtures from 'webtorrent-fixtures'
|
|
|
|
import test from 'tape'
|
|
|
|
import Server from '../server.js'
|
2017-02-24 15:18:32 +00:00
|
|
|
|
2020-10-29 04:57:47 +00:00
|
|
|
const peerId = Buffer.from('01234567890123456789')
|
2017-02-24 15:18:32 +00:00
|
|
|
|
|
|
|
function testRequestHandler (t, serverType) {
|
2017-03-10 21:39:12 +00:00
|
|
|
t.plan(5)
|
2017-02-24 15:18:32 +00:00
|
|
|
|
2020-10-29 04:57:47 +00:00
|
|
|
const opts = { serverType } // this is test-suite-only option
|
2017-03-10 21:39:12 +00:00
|
|
|
|
|
|
|
class Swarm extends Server.Swarm {
|
|
|
|
announce (params, cb) {
|
2021-06-15 01:54:41 +00:00
|
|
|
super.announce(params, (err, response) => {
|
2021-07-22 01:07:06 +00:00
|
|
|
if (cb && err) return cb(response)
|
2017-03-10 21:39:12 +00:00
|
|
|
response.complete = 246
|
|
|
|
response.extraData = 'hi'
|
2021-07-22 01:07:06 +00:00
|
|
|
if (cb) cb(null, response)
|
2017-03-10 21:39:12 +00:00
|
|
|
})
|
2017-02-24 15:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 21:39:12 +00:00
|
|
|
// Use a custom Swarm implementation for this test only
|
2020-10-29 04:57:47 +00:00
|
|
|
const OldSwarm = Server.Swarm
|
2017-03-10 21:39:12 +00:00
|
|
|
Server.Swarm = Swarm
|
2021-06-15 01:54:41 +00:00
|
|
|
t.on('end', () => {
|
2017-03-10 21:39:12 +00:00
|
|
|
Server.Swarm = OldSwarm
|
|
|
|
})
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
common.createServer(t, opts, (server, announceUrl) => {
|
2020-10-29 04:57:47 +00:00
|
|
|
const client1 = new Client({
|
2017-02-24 15:18:32 +00:00
|
|
|
infoHash: fixtures.alice.parsedTorrent.infoHash,
|
|
|
|
announce: announceUrl,
|
2020-04-25 09:34:40 +00:00
|
|
|
peerId,
|
2017-02-24 15:18:32 +00:00
|
|
|
port: 6881,
|
|
|
|
wrtc: {}
|
|
|
|
})
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
client1.on('error', err => { t.error(err) })
|
2017-02-24 15:18:32 +00:00
|
|
|
if (serverType === 'ws') common.mockWebsocketTracker(client1)
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
server.once('start', () => {
|
2017-02-24 15:18:32 +00:00
|
|
|
t.pass('got start message from client1')
|
|
|
|
})
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
client1.once('update', data => {
|
2017-02-24 15:18:32 +00:00
|
|
|
t.equal(data.complete, 246)
|
2023-02-01 02:43:44 +00:00
|
|
|
t.equal(Buffer.from(data.extraData).toString(), 'hi')
|
2017-02-24 15:18:32 +00:00
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
client1.destroy(() => {
|
2017-02-24 15:18:32 +00:00
|
|
|
t.pass('client1 destroyed')
|
|
|
|
})
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
server.close(() => {
|
2017-02-24 15:18:32 +00:00
|
|
|
t.pass('server destroyed')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
client1.start()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
test('http: request handler option intercepts announce requests and responses', t => {
|
2017-02-24 15:18:32 +00:00
|
|
|
testRequestHandler(t, 'http')
|
|
|
|
})
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
test('ws: request handler option intercepts announce requests and responses', t => {
|
2017-02-24 15:18:32 +00:00
|
|
|
testRequestHandler(t, 'ws')
|
|
|
|
})
|
2017-03-10 21:39:02 +00:00
|
|
|
|
|
|
|
// 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!
|