diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2015-01-04 00:15:41 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2015-01-04 00:15:41 +0300 |
commit | fcf1912c93c1d2d53a489323ac15e6babd566875 (patch) | |
tree | 38fca0d99964ac272f8f6ed17733cde1e2d67693 /test/download-dht-torrent.js | |
parent | 07fd715c2518e0bdb801cb622c2c977c2cd716d7 (diff) |
Add DHT magnet uri test, udp tracker magnet uri test
Diffstat (limited to 'test/download-dht-torrent.js')
-rw-r--r-- | test/download-dht-torrent.js | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/test/download-dht-torrent.js b/test/download-dht-torrent.js new file mode 100644 index 0000000..74f8971 --- /dev/null +++ b/test/download-dht-torrent.js @@ -0,0 +1,97 @@ +var auto = require('run-auto') +var WebTorrent = require('../') +var DHT = require('bittorrent-dht/server') +var fs = require('fs') +var parseTorrent = require('parse-torrent') +var test = require('tape') + +var leavesFile = __dirname + '/content/Leaves of Grass by Walt Whitman.epub' +var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent') +var leavesParsed = parseTorrent(leavesTorrent) + +test('Download using DHT (via .torrent file)', function (t) { + t.plan(7) + + // remove trackers from .torrent file + leavesParsed.announce = [] + leavesParsed.announceList = [] + + var dhtServer = new DHT({ bootstrap: false }) + + dhtServer.on('error', function (err) { + t.fail(err) + }) + + auto({ + dhtPort: function (cb) { + dhtServer.listen(function (port) { + cb(null, port) + }) + }, + client1: ['dhtPort', function (cb, r) { + var client1 = new WebTorrent({ + tracker: false, + dht: { bootstrap: '127.0.0.1:' + r.dhtPort } + }) + client1.on('error', function (err) { t.fail(err) }) + + client1.add(leavesParsed) + + var announced, wroteStorage + function maybeDone (err) { + if ((announced && wroteStorage) || err) cb(err, client1) + } + + client1.on('torrent', function (torrent) { + // torrent metadata has been fetched -- sanity check it + t.equal(torrent.name, 'Leaves of Grass by Walt Whitman.epub') + + var names = [ 'Leaves of Grass by Walt Whitman.epub' ] + t.deepEqual(torrent.files.map(function (file) { return file.name }), names) + + torrent.on('dhtAnnounce', function () { + announced = true + maybeDone(null) + }) + + torrent.storage.load(fs.createReadStream(leavesFile), function (err) { + wroteStorage = true + maybeDone(err) + }) + }) + }], + + client2: ['client1', function (cb, r) { + var client2 = new WebTorrent({ + tracker: false, + dht: { bootstrap: '127.0.0.1:' + r.dhtPort } + }) + client2.on('error', function (err) { t.fail(err) }) + + client2.add(leavesParsed) + + client2.on('torrent', function (torrent) { + torrent.files.forEach(function (file) { + file.createReadStream() + }) + + torrent.once('done', function () { + t.pass('client2 downloaded torrent from client1') + cb(null, client2) + }) + }) + }], + + }, function (err, r) { + t.error(err) + r.client1.destroy(function () { + t.pass('client1 destroyed') + }) + r.client2.destroy(function () { + t.pass('client2 destroyed') + }) + dhtServer.destroy(function () { + t.pass('dht server destroyed') + }) + }) +}) |