bittorrent-tracker/test/common.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

import { Server } from '../index.js'
export const createServer = (t, opts, cb) => {
if (typeof opts === 'string') opts = { serverType: opts }
opts.http = (opts.serverType === 'http')
opts.udp = (opts.serverType === 'udp')
opts.ws = (opts.serverType === 'ws')
2020-10-29 04:57:47 +00:00
const server = new Server(opts)
2021-06-15 01:54:41 +00:00
server.on('error', err => { t.error(err) })
server.on('warning', err => { t.error(err) })
2021-06-15 01:54:41 +00:00
server.listen(0, () => {
2020-10-29 04:57:47 +00:00
const port = server[opts.serverType].address().port
let announceUrl
if (opts.serverType === 'http') {
2021-06-15 01:54:41 +00:00
announceUrl = `http://127.0.0.1:${port}/announce`
} else if (opts.serverType === 'udp') {
2021-06-15 01:54:41 +00:00
announceUrl = `udp://127.0.0.1:${port}`
} else if (opts.serverType === 'ws') {
2021-06-15 01:54:41 +00:00
announceUrl = `ws://127.0.0.1:${port}`
2016-02-29 22:48:23 +00:00
}
cb(server, announceUrl)
})
}
export const mockWebsocketTracker = client => {
2021-06-15 01:54:41 +00:00
client._trackers[0]._generateOffers = (numwant, cb) => {
2020-10-29 04:57:47 +00:00
const offers = []
for (let i = 0; i < numwant; i++) {
2021-06-15 01:54:41 +00:00
offers.push({ fake_offer: `fake_offer_${i}` })
}
2021-06-15 01:54:41 +00:00
process.nextTick(() => {
cb(offers)
})
}
}
export default { mockWebsocketTracker, createServer }