diff options
author | Olivier Lalonde <olalonde@gmail.com> | 2015-05-27 10:03:28 +0300 |
---|---|---|
committer | Olivier Lalonde <olalonde@gmail.com> | 2015-05-27 10:03:28 +0300 |
commit | d924bbe21131224ab505c8d3d3aa3daa550f1811 (patch) | |
tree | 1d6d644c57eb7fd30304ef95121a75b05e6a6825 /test | |
parent | 513e6fa11de121d083da60ed3cb959dad2257b23 (diff) |
Webseed: wrote simple webseed test
Diffstat (limited to 'test')
-rw-r--r-- | test/download-webseed-torrent.js | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/test/download-webseed-torrent.js b/test/download-webseed-torrent.js new file mode 100644 index 0000000..b2e8501 --- /dev/null +++ b/test/download-webseed-torrent.js @@ -0,0 +1,79 @@ +var auto = require('run-auto') +var DHT = require('bittorrent-dht/server') +var fs = require('fs') +var parseTorrent = require('parse-torrent') +var test = require('tape') +var WebTorrent = require('../') + +var http = require('http') +var serveStatic = require('serve-static') +var finalhandler = require('finalhandler') +var path = require('path') + +var leavesPath = __dirname + '/content/Leaves of Grass by Walt Whitman.epub' +var leavesFilename = 'Leaves of Grass by Walt Whitman.epub' +var leavesFile = fs.readFileSync(leavesPath) +var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent') +var leavesParsed = parseTorrent(leavesTorrent) + +// remove trackers from .torrent file +leavesParsed.announce = [] + +test('Download using webseed (via .torrent file)', function (t) { + t.plan(5) + + var serve = serveStatic(path.join(__dirname, './content')); + var httpServer = http.createServer(function(req, res){ + var done = finalhandler(req, res) + serve(req, res, done) + }) + + httpServer.on('error', function (err) { + t.fail(err) + }) + + auto({ + httpPort: function (cb) { + httpServer.listen(function () { + var port = httpServer.address().port + cb(null, port) + }) + }, + client1: ['httpPort', function (cb, r) { + + leavesParsed.urlList.push('http://localhost:' + r.httpPort + '/' + leavesFilename); + + var client1 = new WebTorrent({ + tracker: false, + dht: { bootstrap: false } + }) + + client1.on('error', function (err) { t.fail(err) }) + + client1.on('torrent', function (torrent) { + torrent.files.forEach(function (file) { + file.getBuffer(function (err, buf) { + if (err) throw err + t.deepEqual(buf, leavesFile, 'downloaded correct content') + }) + }) + + torrent.once('done', function () { + t.pass('client1 downloaded torrent from webseed') + cb(null, client1) + }) + }); + + client1.add(leavesParsed) + + }] + }, function (err, r) { + t.error(err) + r.client1.destroy(function () { + t.pass('client1 destroyed') + }) + httpServer.close(function () { + t.pass('http server closed') + }) + }) +}) |