mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2024-12-13 19:06:27 +00:00
test: add test for ws tracker for filter tests
This commit is contained in:
parent
5dcdf9c4c1
commit
3342c20b6b
@ -1,23 +1,25 @@
|
|||||||
var Server = require('../').Server
|
var Server = require('../').Server
|
||||||
|
|
||||||
exports.createServer = function (t, serverType, cb) {
|
exports.createServer = function (t, opts, cb) {
|
||||||
var server = new Server({
|
if (typeof opts === 'string') opts = { serverType: opts }
|
||||||
http: serverType === 'http',
|
|
||||||
udp: serverType === 'udp',
|
opts.http = (opts.serverType === 'http')
|
||||||
ws: serverType === 'ws'
|
opts.udp = (opts.serverType === 'udp')
|
||||||
})
|
opts.ws = (opts.serverType === 'ws')
|
||||||
|
|
||||||
|
var server = new Server(opts)
|
||||||
|
|
||||||
server.on('error', function (err) { t.error(err) })
|
server.on('error', function (err) { t.error(err) })
|
||||||
server.on('warning', function (err) { t.error(err) })
|
server.on('warning', function (err) { t.error(err) })
|
||||||
|
|
||||||
server.listen(0, function () {
|
server.listen(0, function () {
|
||||||
var port = server[serverType].address().port
|
var port = server[opts.serverType].address().port
|
||||||
var announceUrl
|
var announceUrl
|
||||||
if (serverType === 'http') {
|
if (opts.serverType === 'http') {
|
||||||
announceUrl = 'http://127.0.0.1:' + port + '/announce'
|
announceUrl = 'http://127.0.0.1:' + port + '/announce'
|
||||||
} else if (serverType === 'udp') {
|
} else if (opts.serverType === 'udp') {
|
||||||
announceUrl = 'udp://127.0.0.1:' + port
|
announceUrl = 'udp://127.0.0.1:' + port
|
||||||
} else if (serverType === 'ws') {
|
} else if (opts.serverType === 'ws') {
|
||||||
announceUrl = 'ws://127.0.0.1:' + port
|
announceUrl = 'ws://127.0.0.1:' + port
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
var Client = require('../')
|
var Client = require('../')
|
||||||
|
var common = require('./common')
|
||||||
var fs = require('fs')
|
var fs = require('fs')
|
||||||
var parseTorrent = require('parse-torrent')
|
var parseTorrent = require('parse-torrent')
|
||||||
var path = require('path')
|
var path = require('path')
|
||||||
var Server = require('../').Server
|
|
||||||
var test = require('tape')
|
var test = require('tape')
|
||||||
|
|
||||||
var bitlove = fs.readFileSync(path.join(__dirname, 'torrents/bitlove-intro.torrent'))
|
var bitlove = fs.readFileSync(path.join(__dirname, 'torrents/bitlove-intro.torrent'))
|
||||||
@ -15,55 +15,38 @@ var peerId = new Buffer('01234567890123456789')
|
|||||||
|
|
||||||
function testFilterOption (t, serverType) {
|
function testFilterOption (t, serverType) {
|
||||||
t.plan(8)
|
t.plan(8)
|
||||||
var opts = serverType === 'http' ? { udp: false, ws: false } : { http: false, ws: false }
|
|
||||||
|
var opts = { serverType: serverType } // this is test-suite-only option
|
||||||
opts.filter = function (infoHash, params, cb) {
|
opts.filter = function (infoHash, params, cb) {
|
||||||
process.nextTick(function () {
|
process.nextTick(function () {
|
||||||
cb(infoHash !== parsedBitlove.infoHash)
|
cb(infoHash !== parsedBitlove.infoHash)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
var server = new Server(opts)
|
|
||||||
|
|
||||||
server.on('error', function (err) {
|
|
||||||
t.error(err)
|
|
||||||
})
|
|
||||||
|
|
||||||
server.listen(0, function () {
|
|
||||||
var port = server[serverType].address().port
|
|
||||||
var announceUrl = serverType === 'http'
|
|
||||||
? 'http://127.0.0.1:' + port + '/announce'
|
|
||||||
: 'udp://127.0.0.1:' + port
|
|
||||||
|
|
||||||
|
common.createServer(t, opts, function (server, announceUrl) {
|
||||||
parsedBitlove.announce = [ announceUrl ]
|
parsedBitlove.announce = [ announceUrl ]
|
||||||
parsedLeaves.announce = [ announceUrl ]
|
parsedLeaves.announce = [ announceUrl ]
|
||||||
|
|
||||||
var client = new Client(peerId, port, parsedBitlove)
|
var client = new Client(peerId, 6881, parsedBitlove, { wrtc: {} })
|
||||||
|
|
||||||
client.on('error', function (err) {
|
client.on('error', function (err) { t.error(err) })
|
||||||
t.error(err)
|
if (serverType === 'ws') common.mockWebsocketTracker(client)
|
||||||
})
|
|
||||||
|
|
||||||
client.once('warning', function (err) {
|
client.once('warning', function (err) {
|
||||||
t.ok(/disallowed info_hash/.test(err.message), 'got client warning')
|
t.ok(/disallowed info_hash/.test(err.message), 'got client warning')
|
||||||
|
|
||||||
client.destroy(function () {
|
client.destroy(function () {
|
||||||
t.pass('client destroyed')
|
t.pass('client destroyed')
|
||||||
client = new Client(peerId, port, parsedLeaves)
|
client = new Client(peerId, 6881, parsedLeaves, { wrtc: {} })
|
||||||
|
if (serverType === 'ws') common.mockWebsocketTracker(client)
|
||||||
|
|
||||||
client.on('error', function (err) {
|
client.on('error', function (err) { t.error(err) })
|
||||||
t.error(err)
|
client.on('warning', function (err) { t.error(err) })
|
||||||
})
|
|
||||||
client.on('warning', function (err) {
|
|
||||||
t.error(err)
|
|
||||||
})
|
|
||||||
|
|
||||||
client.on('update', function () {
|
client.on('update', function () {
|
||||||
t.pass('got announce')
|
t.pass('got announce')
|
||||||
client.destroy(function () {
|
client.destroy(function () { t.pass('client destroyed') })
|
||||||
t.pass('client destroyed')
|
server.close(function () { t.pass('server closed') })
|
||||||
})
|
|
||||||
server.close(function () {
|
|
||||||
t.pass('server closed')
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
server.on('start', function () {
|
server.on('start', function () {
|
||||||
@ -74,6 +57,7 @@ function testFilterOption (t, serverType) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
server.removeAllListeners('warning')
|
||||||
server.once('warning', function (err) {
|
server.once('warning', function (err) {
|
||||||
t.ok(/disallowed info_hash/.test(err.message), 'got server warning')
|
t.ok(/disallowed info_hash/.test(err.message), 'got server warning')
|
||||||
t.equal(Object.keys(server.torrents).length, 0)
|
t.equal(Object.keys(server.torrents).length, 0)
|
||||||
@ -91,58 +75,45 @@ test('udp: filter option blocks tracker from tracking torrent', function (t) {
|
|||||||
testFilterOption(t, 'udp')
|
testFilterOption(t, 'udp')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('ws: filter option blocks tracker from tracking torrent', function (t) {
|
||||||
|
testFilterOption(t, 'ws')
|
||||||
|
})
|
||||||
|
|
||||||
function testFilterCustomError (t, serverType) {
|
function testFilterCustomError (t, serverType) {
|
||||||
t.plan(8)
|
t.plan(8)
|
||||||
var opts = serverType === 'http' ? { udp: false, ws: false } : { http: false, ws: false }
|
|
||||||
|
var opts = { serverType: serverType } // this is test-suite-only option
|
||||||
opts.filter = function (infoHash, params, cb) {
|
opts.filter = function (infoHash, params, cb) {
|
||||||
process.nextTick(function () {
|
process.nextTick(function () {
|
||||||
if (infoHash === parsedBitlove.infoHash) cb(new Error('bitlove blocked'))
|
if (infoHash === parsedBitlove.infoHash) cb(new Error('bitlove blocked'))
|
||||||
else cb(true)
|
else cb(true)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
var server = new Server(opts)
|
|
||||||
|
|
||||||
server.on('error', function (err) {
|
|
||||||
t.error(err)
|
|
||||||
})
|
|
||||||
|
|
||||||
server.listen(0, function () {
|
|
||||||
var port = server[serverType].address().port
|
|
||||||
var announceUrl = serverType === 'http'
|
|
||||||
? 'http://127.0.0.1:' + port + '/announce'
|
|
||||||
: 'udp://127.0.0.1:' + port
|
|
||||||
|
|
||||||
|
common.createServer(t, opts, function (server, announceUrl) {
|
||||||
parsedBitlove.announce = [ announceUrl ]
|
parsedBitlove.announce = [ announceUrl ]
|
||||||
parsedLeaves.announce = [ announceUrl ]
|
parsedLeaves.announce = [ announceUrl ]
|
||||||
|
|
||||||
var client = new Client(peerId, port, parsedBitlove)
|
var client = new Client(peerId, 6881, parsedBitlove, { wrtc: {} })
|
||||||
|
|
||||||
client.on('error', function (err) {
|
client.on('error', function (err) { t.error(err) })
|
||||||
t.error(err)
|
if (serverType === 'ws') common.mockWebsocketTracker(client)
|
||||||
})
|
|
||||||
|
|
||||||
client.once('warning', function (err) {
|
client.once('warning', function (err) {
|
||||||
t.ok(/bitlove blocked/.test(err.message), 'got client warning')
|
t.ok(/bitlove blocked/.test(err.message), 'got client warning')
|
||||||
|
|
||||||
client.destroy(function () {
|
client.destroy(function () {
|
||||||
t.pass('client destroyed')
|
t.pass('client destroyed')
|
||||||
client = new Client(peerId, port, parsedLeaves)
|
client = new Client(peerId, 6881, parsedLeaves, { wrtc: {} })
|
||||||
|
if (serverType === 'ws') common.mockWebsocketTracker(client)
|
||||||
|
|
||||||
client.on('error', function (err) {
|
client.on('error', function (err) { t.error(err) })
|
||||||
t.error(err)
|
client.on('warning', function (err) { t.error(err) })
|
||||||
})
|
|
||||||
client.on('warning', function (err) {
|
|
||||||
t.error(err)
|
|
||||||
})
|
|
||||||
|
|
||||||
client.on('update', function () {
|
client.on('update', function () {
|
||||||
t.pass('got announce')
|
t.pass('got announce')
|
||||||
client.destroy(function () {
|
client.destroy(function () { t.pass('client destroyed') })
|
||||||
t.pass('client destroyed')
|
server.close(function () { t.pass('server closed') })
|
||||||
})
|
|
||||||
server.close(function () {
|
|
||||||
t.pass('server closed')
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
server.on('start', function () {
|
server.on('start', function () {
|
||||||
@ -153,6 +124,7 @@ function testFilterCustomError (t, serverType) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
server.removeAllListeners('warning')
|
||||||
server.once('warning', function (err) {
|
server.once('warning', function (err) {
|
||||||
t.ok(/bitlove blocked/.test(err.message), 'got server warning')
|
t.ok(/bitlove blocked/.test(err.message), 'got server warning')
|
||||||
t.equal(Object.keys(server.torrents).length, 0)
|
t.equal(Object.keys(server.torrents).length, 0)
|
||||||
@ -169,3 +141,7 @@ test('http: filter option with custom error', function (t) {
|
|||||||
test('udp: filter option filter option with custom error', function (t) {
|
test('udp: filter option filter option with custom error', function (t) {
|
||||||
testFilterCustomError(t, 'udp')
|
testFilterCustomError(t, 'udp')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('ws: filter option filter option with custom error', function (t) {
|
||||||
|
testFilterCustomError(t, 'ws')
|
||||||
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user