diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2015-07-03 05:20:33 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2015-07-03 05:20:33 +0300 |
commit | 9400efe10f02b36c6bb10862bacbbc88b932cddb (patch) | |
tree | 87f0cb03c5782350137d5143e93437b5cd8b90ac /test | |
parent | 05932cd6f9bed3c908a9c318824bb66f5039bf84 (diff) | |
parent | 9df26f6fcd8e6fb32a81127cfd6bb84192604fa5 (diff) |
Merge branch 'webseed' of https://github.com/olalonde/webtorrent into olalonde-webseed
Diffstat (limited to 'test')
-rw-r--r-- | test/download-webseed-torrent.js | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/test/download-webseed-torrent.js b/test/download-webseed-torrent.js new file mode 100644 index 0000000..5c048f8 --- /dev/null +++ b/test/download-webseed-torrent.js @@ -0,0 +1,78 @@ +var auto = require('run-auto') +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') + }) + }) +}) |