2014-06-07 22:15:00 +00:00
|
|
|
var Client = require('../')
|
2014-05-14 19:45:48 +00:00
|
|
|
var magnet = require('magnet-uri')
|
2014-05-24 04:02:53 +00:00
|
|
|
var Server = require('../').Server
|
2014-05-14 19:45:48 +00:00
|
|
|
var test = require('tape')
|
|
|
|
|
|
|
|
var uri = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80'
|
|
|
|
var parsedTorrent = magnet(uri)
|
|
|
|
var peerId = new Buffer('01234567890123456789')
|
|
|
|
|
|
|
|
test('magnet + udp: client.start/update/stop()', function (t) {
|
2015-01-29 22:59:08 +00:00
|
|
|
t.plan(11)
|
2014-05-14 19:45:48 +00:00
|
|
|
|
2014-05-24 04:02:53 +00:00
|
|
|
var server = new Server({ http: false })
|
2014-05-14 19:45:48 +00:00
|
|
|
|
2014-05-24 04:02:53 +00:00
|
|
|
server.on('error', function (err) {
|
|
|
|
t.fail(err.message)
|
2014-05-14 19:45:48 +00:00
|
|
|
})
|
|
|
|
|
2014-05-24 04:02:53 +00:00
|
|
|
server.on('warning', function (err) {
|
|
|
|
t.fail(err.message)
|
2014-05-14 19:45:48 +00:00
|
|
|
})
|
|
|
|
|
2015-01-29 22:59:08 +00:00
|
|
|
server.listen(0, function () {
|
|
|
|
var port = server.udp.address().port
|
2014-07-22 05:58:13 +00:00
|
|
|
var announceUrl = 'udp://127.0.0.1:' + port
|
2014-05-24 04:02:53 +00:00
|
|
|
|
|
|
|
// remove all tracker servers except a single UDP one, for now
|
|
|
|
parsedTorrent.announce = [ announceUrl ]
|
|
|
|
|
|
|
|
var client = new Client(peerId, 6881, parsedTorrent)
|
|
|
|
|
|
|
|
client.on('error', function (err) {
|
|
|
|
t.error(err)
|
|
|
|
})
|
2014-05-14 19:45:48 +00:00
|
|
|
|
|
|
|
client.once('update', function (data) {
|
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(typeof data.complete, 'number')
|
|
|
|
t.equal(typeof data.incomplete, 'number')
|
2014-05-24 04:02:53 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
client.start()
|
|
|
|
|
2014-11-26 12:11:49 +00:00
|
|
|
client.once('peer', function () {
|
|
|
|
t.pass('there is at least one peer')
|
2014-05-24 04:02:53 +00:00
|
|
|
|
|
|
|
client.update()
|
2014-05-14 19:45:48 +00:00
|
|
|
|
|
|
|
client.once('update', function (data) {
|
2014-05-24 04:02:53 +00:00
|
|
|
// receive one final update after calling stop
|
2014-05-14 19:45:48 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(typeof data.complete, 'number')
|
|
|
|
t.equal(typeof data.incomplete, 'number')
|
|
|
|
|
2014-05-24 04:02:53 +00:00
|
|
|
client.stop()
|
2014-05-14 19:45:48 +00:00
|
|
|
|
2014-05-24 04:02:53 +00:00
|
|
|
client.once('update', function (data) {
|
|
|
|
// received an update!
|
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(typeof data.complete, 'number')
|
|
|
|
t.equal(typeof data.incomplete, 'number')
|
2014-05-14 19:45:48 +00:00
|
|
|
|
2014-05-24 04:02:53 +00:00
|
|
|
server.close(function () {
|
|
|
|
t.pass('server closed')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-05-14 19:45:48 +00:00
|
|
|
})
|