mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-05 22:10:11 +00:00
add websocket tracker tests
This commit is contained in:
parent
b34fc100a1
commit
7a0fe31ba5
118
test/client.js
118
test/client.js
@ -12,28 +12,33 @@ var peerId2 = new Buffer('12345678901234567890')
|
|||||||
var peerId3 = new Buffer('23456789012345678901')
|
var peerId3 = new Buffer('23456789012345678901')
|
||||||
var port = 6881
|
var port = 6881
|
||||||
|
|
||||||
|
function mockWebSocketTracker (client) {
|
||||||
|
client._trackers[0]._generateOffers = function (numwant, cb) {
|
||||||
|
var offers = []
|
||||||
|
for (var i = 0; i < numwant; i++) {
|
||||||
|
offers.push('fake_offer_' + i)
|
||||||
|
}
|
||||||
|
process.nextTick(function () {
|
||||||
|
cb(offers)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function testClientStart (t, serverType) {
|
function testClientStart (t, serverType) {
|
||||||
t.plan(5)
|
t.plan(4)
|
||||||
common.createServer(t, serverType, function (server, announceUrl) {
|
common.createServer(t, serverType, function (server, announceUrl) {
|
||||||
parsedTorrent.announce = [ announceUrl ]
|
parsedTorrent.announce = [ announceUrl ]
|
||||||
var client = new Client(peerId1, port, parsedTorrent)
|
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
|
||||||
|
|
||||||
client.on('error', function (err) {
|
if (serverType === 'ws') mockWebSocketTracker(client)
|
||||||
t.error(err)
|
client.on('error', function (err) { t.error(err) })
|
||||||
})
|
client.on('warning', function (err) { t.error(err) })
|
||||||
|
|
||||||
client.on('warning', function (err) {
|
|
||||||
t.error(err)
|
|
||||||
})
|
|
||||||
|
|
||||||
client.once('update', function (data) {
|
client.once('update', function (data) {
|
||||||
t.equal(data.announce, announceUrl)
|
t.equal(data.announce, announceUrl)
|
||||||
t.equal(typeof data.complete, 'number')
|
t.equal(typeof data.complete, 'number')
|
||||||
t.equal(typeof data.incomplete, 'number')
|
t.equal(typeof data.incomplete, 'number')
|
||||||
})
|
|
||||||
|
|
||||||
client.once('peer', function () {
|
|
||||||
t.pass('there is at least one peer')
|
|
||||||
client.stop()
|
client.stop()
|
||||||
|
|
||||||
client.once('update', function () {
|
client.once('update', function () {
|
||||||
@ -55,19 +60,19 @@ test('udp: client.start()', function (t) {
|
|||||||
testClientStart(t, 'udp')
|
testClientStart(t, 'udp')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('ws: client.start()', function (t) {
|
||||||
|
testClientStart(t, 'ws')
|
||||||
|
})
|
||||||
|
|
||||||
function testClientStop (t, serverType) {
|
function testClientStop (t, serverType) {
|
||||||
t.plan(3)
|
t.plan(3)
|
||||||
common.createServer(t, serverType, function (server, announceUrl) {
|
common.createServer(t, serverType, function (server, announceUrl) {
|
||||||
parsedTorrent.announce = [ announceUrl ]
|
parsedTorrent.announce = [ announceUrl ]
|
||||||
var client = new Client(peerId1, port, parsedTorrent)
|
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
|
||||||
|
|
||||||
client.on('error', function (err) {
|
if (serverType === 'ws') mockWebSocketTracker(client)
|
||||||
t.error(err)
|
client.on('error', function (err) { t.error(err) })
|
||||||
})
|
client.on('warning', function (err) { t.error(err) })
|
||||||
|
|
||||||
client.on('warning', function (err) {
|
|
||||||
t.error(err)
|
|
||||||
})
|
|
||||||
|
|
||||||
client.start()
|
client.start()
|
||||||
|
|
||||||
@ -95,21 +100,22 @@ test('udp: client.stop()', function (t) {
|
|||||||
testClientStop(t, 'udp')
|
testClientStop(t, 'udp')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('ws: client.stop()', function (t) {
|
||||||
|
testClientStop(t, 'ws')
|
||||||
|
})
|
||||||
|
|
||||||
function testClientUpdate (t, serverType) {
|
function testClientUpdate (t, serverType) {
|
||||||
t.plan(4)
|
t.plan(4)
|
||||||
common.createServer(t, serverType, function (server, announceUrl) {
|
common.createServer(t, serverType, function (server, announceUrl) {
|
||||||
parsedTorrent.announce = [ announceUrl ]
|
parsedTorrent.announce = [ announceUrl ]
|
||||||
var client = new Client(peerId1, port, parsedTorrent)
|
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
|
||||||
|
|
||||||
|
if (serverType === 'ws') mockWebSocketTracker(client)
|
||||||
|
client.on('error', function (err) { t.error(err) })
|
||||||
|
client.on('warning', function (err) { t.error(err) })
|
||||||
|
|
||||||
client.setInterval(2000)
|
client.setInterval(2000)
|
||||||
|
|
||||||
client.on('error', function (err) {
|
|
||||||
t.error(err)
|
|
||||||
})
|
|
||||||
|
|
||||||
client.on('warning', function (err) {
|
|
||||||
t.error(err)
|
|
||||||
})
|
|
||||||
|
|
||||||
client.start()
|
client.start()
|
||||||
|
|
||||||
client.once('update', function () {
|
client.once('update', function () {
|
||||||
@ -141,19 +147,19 @@ test('udp: client.update()', function (t) {
|
|||||||
testClientUpdate(t, 'udp')
|
testClientUpdate(t, 'udp')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('ws: client.update()', function (t) {
|
||||||
|
testClientUpdate(t, 'ws')
|
||||||
|
})
|
||||||
|
|
||||||
function testClientScrape (t, serverType) {
|
function testClientScrape (t, serverType) {
|
||||||
t.plan(4)
|
t.plan(4)
|
||||||
common.createServer(t, serverType, function (server, announceUrl) {
|
common.createServer(t, serverType, function (server, announceUrl) {
|
||||||
parsedTorrent.announce = [ announceUrl ]
|
parsedTorrent.announce = [ announceUrl ]
|
||||||
var client = new Client(peerId1, port, parsedTorrent)
|
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
|
||||||
|
|
||||||
client.on('error', function (err) {
|
if (serverType === 'ws') mockWebSocketTracker(client)
|
||||||
t.error(err)
|
client.on('error', function (err) { t.error(err) })
|
||||||
})
|
client.on('warning', function (err) { t.error(err) })
|
||||||
|
|
||||||
client.on('warning', function (err) {
|
|
||||||
t.error(err)
|
|
||||||
})
|
|
||||||
|
|
||||||
client.once('scrape', function (data) {
|
client.once('scrape', function (data) {
|
||||||
t.equal(data.announce, announceUrl)
|
t.equal(data.announce, announceUrl)
|
||||||
@ -177,27 +183,37 @@ test('udp: client.scrape()', function (t) {
|
|||||||
testClientScrape(t, 'udp')
|
testClientScrape(t, 'udp')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// TODO: uncomment once scrape is supported on WebSocket trackers
|
||||||
|
// test('ws: client.scrape()', function (t) {
|
||||||
|
// testClientScrape(t, 'ws')
|
||||||
|
// })
|
||||||
|
|
||||||
function testClientAnnounceWithNumWant (t, serverType) {
|
function testClientAnnounceWithNumWant (t, serverType) {
|
||||||
t.plan(4)
|
t.plan(4)
|
||||||
common.createServer(t, serverType, function (server, announceUrl) {
|
common.createServer(t, serverType, function (server, announceUrl) {
|
||||||
parsedTorrent.announce = [ announceUrl ]
|
parsedTorrent.announce = [ announceUrl ]
|
||||||
var client1 = new Client(peerId1, port, parsedTorrent)
|
var client1 = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
|
||||||
client1.on('error', function (err) {
|
|
||||||
t.error(err)
|
if (serverType === 'ws') mockWebSocketTracker(client1)
|
||||||
})
|
client1.on('error', function (err) { t.error(err) })
|
||||||
|
client1.on('warning', function (err) { t.error(err) })
|
||||||
|
|
||||||
client1.start()
|
client1.start()
|
||||||
client1.once('update', function () {
|
client1.once('update', function () {
|
||||||
var client2 = new Client(peerId2, port + 1, parsedTorrent)
|
var client2 = new Client(peerId2, port + 1, parsedTorrent, { wrtc: {} })
|
||||||
client2.on('error', function (err) {
|
|
||||||
t.error(err)
|
if (serverType === 'ws') mockWebSocketTracker(client2)
|
||||||
})
|
client2.on('error', function (err) { t.error(err) })
|
||||||
|
client2.on('warning', function (err) { t.error(err) })
|
||||||
|
|
||||||
client2.start()
|
client2.start()
|
||||||
client2.once('update', function () {
|
client2.once('update', function () {
|
||||||
var client3 = new Client(peerId3, port + 2, parsedTorrent)
|
var client3 = new Client(peerId3, port + 2, parsedTorrent, { wrtc: {} })
|
||||||
client3.on('error', function (err) {
|
|
||||||
t.error(err)
|
if (serverType === 'ws') mockWebSocketTracker(client3)
|
||||||
})
|
client3.on('error', function (err) { t.error(err) })
|
||||||
|
client3.on('warning', function (err) { t.error(err) })
|
||||||
|
|
||||||
client3.start({ numwant: 1 })
|
client3.start({ numwant: 1 })
|
||||||
client3.on('peer', function () {
|
client3.on('peer', function () {
|
||||||
t.pass('got one peer (this should only fire once)')
|
t.pass('got one peer (this should only fire once)')
|
||||||
@ -239,3 +255,7 @@ test('http: client announce with numwant', function (t) {
|
|||||||
test('udp: client announce with numwant', function (t) {
|
test('udp: client announce with numwant', function (t) {
|
||||||
testClientAnnounceWithNumWant(t, 'udp')
|
testClientAnnounceWithNumWant(t, 'udp')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('ws: client announce with numwant', function (t) {
|
||||||
|
testClientAnnounceWithNumWant(t, 'ws')
|
||||||
|
})
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
var Server = require('../').Server
|
var Server = require('../').Server
|
||||||
|
|
||||||
exports.createServer = function (t, serverType, cb) {
|
exports.createServer = function (t, serverType, cb) {
|
||||||
var opts = serverType === 'http' ? { udp: false, ws: false } : { http: false, ws: false }
|
var server = new Server({
|
||||||
var server = new Server(opts)
|
http: serverType === 'http',
|
||||||
|
udp: serverType === 'udp',
|
||||||
|
ws: serverType === 'ws'
|
||||||
|
})
|
||||||
|
|
||||||
server.on('error', function (err) {
|
server.on('error', function (err) {
|
||||||
t.error(err)
|
t.error(err)
|
||||||
@ -14,9 +17,14 @@ exports.createServer = function (t, serverType, cb) {
|
|||||||
|
|
||||||
server.listen(0, function () {
|
server.listen(0, function () {
|
||||||
var port = server[serverType].address().port
|
var port = server[serverType].address().port
|
||||||
var announceUrl = serverType === 'http'
|
var announceUrl
|
||||||
? 'http://127.0.0.1:' + port + '/announce'
|
if (serverType === 'http') {
|
||||||
: 'udp://127.0.0.1:' + port
|
announceUrl = 'http://127.0.0.1:' + port + '/announce'
|
||||||
|
} else if (serverType === 'udp') {
|
||||||
|
announceUrl = 'udp://127.0.0.1:' + port
|
||||||
|
} else if (serverType === 'ws') {
|
||||||
|
announceUrl = 'ws://127.0.0.1:' + port
|
||||||
|
}
|
||||||
|
|
||||||
cb(server, announceUrl)
|
cb(server, announceUrl)
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user